ChatGPT API + Google Sheets boost results [GPT4]

The easiest way to use ChatGPT is the chat interface. But there is another way to use ChatGPT API. It’s called the application programming interface of API. OpenAI made the ChatGPT API accessible to everyone and you can take some time to experiment with it.

chatgpt api google sheets gpt4

Usually, developers create apps and send their information through ChatGPT API and get back the response they will show in their app.

For this you need to know how to write code. Or at least you would have to be willing to work with ChatGPT and tease out the code you can use in your application. This is time consuming, and, in many cases, you will not be able to create a working app.

But there is a middle ground.

It is possible to connect your Google Sheets to the ChatGPT API. This way you can send information from your spreadsheet to ChatGPT and get the result back in your table.

To do that you will need to create a function in Google Sheets. For this purpose, there’s a way to write code for Google Sheets, it’s called App Script. In App Script you need to write a bit of code to connect to GPT API.

If you want to work with the ChatGPT web interface but get more functionality you should check out ChatGPT Chrome extensions.

Access ChatGPT API in Google Sheets

Don’t worry, I created a Google Sheet you can copy and use the API connection without ever taking a look at the code. You can use this spreadsheet as any other, you just have a new function that can access GPT and answer your questions. The only thing you need is an OpenAI API key.

Getting access to ChatGPT API and GPT API key

First, you need to create an account to access the ChatGPT API. You can do it here.

Next, you need to go here and create your API key for accessing GPT language models. You can have as many API keys as you want. For example, different keys for various projects. But once you create the key you have to save it somewhere. If you misplace your API key, you need to make a new one.

chatgpt api keys

Click this Google Sheet link, make a copy, and follow the instructions in the file.

The function works as follows: gpt4api ( prompt , data , temperature , model )

  1. Prompt is the question you ask from ChatGPT
  2. Data is any additional information you want to pass to the AI (optional)
  3. Temperature is the creativity of the model from 0 to 1
  4. Model is the version of the GPT you want to use for your query. Here you can select gpt-4 or gpt-3.5-turbo-16k from the dropdown menu. The default value is “No model” so you will not waste your money when the Sheets automatically recalculates cell values. Read more about this in the “Caution!” section below.

In the Google Sheets file, I have this example:

  1. In the first cell I have a prompt “Please write a tweet, maximum 45 words. Topic”
  2. In the second cell I add specific data “How to write code with ChatGPT?” This cell is optional.
  3. In the third cell I have set the temperature to 0.7. This means the tweet would be more on the creative side.
  4. Finally, the fourth cell contains the gpt4api function where you will get the output of the GPT model you selected.
google sheets chatgpt api gpt4

This is just one simple example. You can create countless rows of data by just copying the example and changing the contents of the cells. Think about your business and marketing processes, you will certainly find things you can optimize with this tool.

Caution!!! Automatic recalculation in Google Sheets

One important thing to remember is that Google Sheets will recalculate every cell when you make a change on the sheet. If you have 100 cells sending API calls to GPT then the price could get high, especially when using GPT4 model.

While the price of the API is not too steep, it can add up when you use the Google Sheets tool.

Just to protect against automatic recalculation, I made the model selection include a “No model” option. If you want to make absolutely sure you don’t waste any money on useless API queries, follow these steps:

  1. Make sure the “No model” is selected in the B4 cell. “GPT Output” column will display “#ERROR!” as long as the correct model is not selected.
  2. Fill out all the fields in the table with the data you want to send to GPT through the API.
  3. Now, select the model you want to use.
  4. After you get the results from OpenAI API, copy the values of the cells to a separate table or text document. (Then answers to your queries will be different after every automatic recalculation).

Using this process ensures automatic recalculations will not waste your money.

OpenAI API Price

Accessing OpenAI API is a paid service. You can find detailed prices here. The price is different for GPT3 and GPT4 and depends on other variables. But as a rule of thumb, you can calculate the price as follows:

In GPT4 model 3000 words of input costs 12 cents and output is 24 cents.

In GPT3 model 3000 words of input costs 1.2 cents and output is 1.6 cents.

As you can see the older version is 10 times cheaper than the current one. You will have an almost real-time overview of how much money you have spent using the API.

chatgpt api usage

You can set usage limits on your account, so you don’t spend all your money on AI. There’s a hard limit at which the system will stop accepting any new requests. And there’s a soft limit at which you will get an email that you have reached a certain threshold.

chatgpt api usage limits

In the beginning, when you create your account, you will get some free credit to experiment with.

Tweaking the ChatGPT API code

If you want to tweak the code, then you can find it here:

  1. From Google Sheets menu “Extensions” select “App Script.”
  2. In the window that opens you will see the code that connects Sheets to GPT.
  3. You can find out more about how to use API here.

Now you are ready to experiment with the ChatGPT API. In some cases, you can 10x your productivity by creating an automated system that helps you tackle tedious and repetitive tasks.

Let us know in the comments what use cases are working for you.