How to connect the OpenAI API to Google Sheets

OpenAI is an AI research company. We can use the OpenAI API to import data from OpenAI into Google Sheets. In this tutorial, we’ll show you how to connect the OpenAI REST API to Google Sheets in 5 steps:

  1. Install the Apipheny add-on
  2. Obtain your API key
  3. Choose a OpenAI endpoint
  4. Enter your OpenAI API request into Apipheny
  5. Run the OpenAI API request in your Google Sheet

Step 1.) Install and open the Apipheny add-on for Google Sheets

Apipheny is a free API connector for Google Sheets. You can use Apipheny to connect your Google Sheets to API data sources, easily.

1.) Install Apipheny by opening the following link on desktop and then clicking the Install button in the Google Marketplace: https://apipheny.io/install

2.) After you’ve installed Apipheny, open a Google Sheet and then click on the Extensions dropdown in the menu at the top.

In the dropdown list you should see Apipheny. Click Apipheny then click Import API to open the Apipheny sidebar in your Google Sheet.

open apipheny

Tip: you can open a new Google Sheet by entering this URL in your browser: sheet.new

Step 2.) Obtain an API key

In your OpenAI account, click the user icon, then go to the View API keys option:

OpenAI account
OpenAI account

In the API keys section, click the +Create new secret key button to generate an API key:

API Keys section
API Keys section

Your API key is now generated:

API key generated popup
API key generated popup

Copy it to a safe location because you’ll need it later. Click the OK button to return to the API keys section, where your key will be listed.

Step 3.) Choose a OpenAI API Endpoint URL

In this section, we’ll show you how to browse the OpenAI API documentation to find an endpoint that retrieves the information you need. If you already know your API URL, or you want to use the same example URL as us, just skip to Step 4.

First, open the OpenAI API documentation page: https://beta.openai.com/docs/introduction

The menu on the left contains general information about the OpenAI API and a list of categories for the OpenAI API endpoints:

OpenAI documentation page
OpenAI documentation page

If you scroll down to the API Reference section, you’ll find the endpoints’ categories. An example is Embeddings.

An example endpoint is Create embeddings, which creates an embedding vector representing the input text. The URL for this endpoint is:

https://api.openai.com/v1/embeddings

Each endpoint’s documentation contains a descriptive title, a paragraph describing the endpoint’s purpose, the request body parameters if it’s a POST. In a distinct section on the right, you will see the endpoint’s HTTP method (GET) and URL, as well as an example request and response:

Create embeddings endpoint documentation
Create embeddings endpoint documentation

All API calls to OpenAI should be made to the https://api.openai.com/v1/ base domain. All endpoints should be appended to this base URL.

Step 4.) Enter your OpenAI API request into Apipheny

Now, to get your OpenAI data in your Google Sheet, go back to your Google Sheet and make sure that you’ve installed the Apipheny add-on and you have it opened on the Import tab. In the Import tab of Apipheny, enter the following details into the add-on:

Method: At the top of the Apipheny sidebar, select the HTTP method (GET, POST, PUT, PATCH or DELETE) required by your OpenAI API endpoint. For this example, we are using the “POST” method.

API URL: In Step 4, we explained how you can find the OpenAI API endpoint that you need. Now copy your complete OpenAI API URL into the Apipheny add-on, in the field that says API URL Path, followed by any POST parameters required for your query (if applicable).

For this example, we are using the Create completion endpoint, that creates a completion for the provided prompt and parameters. The URL for this specific endpoint is:

https://api.openai.com/v1/completions

If you’re following the same example as us, just copy and paste the above URL into Apipheny.

Headers: In the Headers section of the Apipheny add-on, add one row with the following key and value:

Header

Key:
Authorization

Value:
Bearer your_API key

Make sure to replace your_key with the same value of the token that you’ve generated previously, in Step 2. Leave a literal space between “Bearer” and your token.

Body: In the Body section of Apipheny, add the JSON file containing the A.I. model you’d like to use, the text you need information for and the extraction parameters. We’re going to use the following example:

{
"model": "text-davinci-002",
"prompt": "Given a post, this program will generate 3 relevant hastags.\n\nPost:SimpleX AI-powered smart console that helps you quickly analyze and sort text data without coding.\n #ai #textdata #simplex\n/##\nPost: ProductDyno is a content delivery platform that helps you create and sell memberships, video courses, and digital products.\n#productdyno #digitalproducts #contentdelivery\n/##\nPost: RabbitLoader is a WordPress plugin that reduces website load times using content minification, lazy loading and CDN caching.\n#rabbitloader #wordpress #loadtimes\n##\nPost: Optimize load times for your webpages and assests to improve your user experience and Core Web Vitals on Google.\n/##\nPost: Blits.ai - Build chatboots wit conversational AI\n#blits #chatboots #conversationalai\n/##Post: {{{Sheet1!B3}}}",
 "temperature": 0.7,
 "max_tokens": 64,
 "top_p": 1,
 "frequency_penalty": 0,
 "presence_penalty": 0
}

In this case we are pulling data from Sheet 1, cell B3 using this cell reference: {{{Sheet1!B3}}}

We are running the request in another Google Sheet (e.g Sheet6) and the result will be displayed in Sheet6, but also referenced in Sheet1 cell C3. To establish this reference, we wrote a formula to take the Sheet6 cell E2 value to the Sheet1 B3 cell: =Sheet6!E2

Here’s what your OpenAI API request should look like when entered into Apipheny, if you’re following the same example as us:

OpenAI API request entered in the Apipheny add-on
OpenAI API request entered in the Apipheny add-on

Another example is the Create chat completion endpoint, which creates a model response for the given chat conversation. The URL for this specific endpoint is:

https://api.openai.com/v1/chat/completions

If you’re following the same example as us, just copy and paste the above URL into Apipheny.

Headers: In the Headers section of the Apipheny add-on, add one row with the following key and value:

Header

Key:
Authorization

Value:
Bearer your_API key

Make sure to replace your_key with the same value of the token that you’ve generated previously, in Step 2. Leave a literal space between “Bearer” and your token.

Body: In the Body section of Apipheny, add the ID of the model to use and the messages (a list of messages describing the conversation so far) :

{
	"model": "gpt-3.5-turbo",
	"messages": [{
		"role": "user",
		"content": "Hello!"
	}]
}

Here’s what your ChatGPT API request should look like when entered into Apipheny, if you’re following the same example as us:

ChatGPT API request entered in the Apipheny add-on
ChatGPT API request entered in the Apipheny add-on

Step 5.) Run the OpenAI API Request in Your Google Sheet

The last step is to click the Run button at the bottom of the Apipheny add-on and then your OpenAI data will be imported into your Google Sheets. Congratulations!

The result from Sheet6:

OpenAI API connected with Google Sheets and Open AI data imported into Google Sheets using the Apipheny add-on
OpenAI API connected with Google Sheets and Open AI data imported into Google Sheets using the Apipheny add-on
ChatGPT data imported into Google Sheets using the Apipheny add-on
ChatGPT data imported into Google Sheets using the Apipheny add-on

The result in Sheet1:

Extracting the result from Sheet6 to Sheet1 using a Google Sheets formula
Extracting the result from Sheet6 to Sheet1 using a Google Sheets formula


Popular API Tutorials


API Knowledge

What is an API?

What is an API URL?

What are parameters?

What is an endpoint?

What is an API key/token?

What is basic authentication?

What are headers?

What is a GET request?

What is a POST request?

Import JSON to Google Sheets


SEO by SearchCyrus