How to call GPT-3 API in JavaScript


You can try out GPT-3 without knowing anything about artificial intelligence or machine learning. This article will teach you how to call the conversational endpoint, the most simple and easy to use API endpoint. It’s fast, easy, and anyone can do it.

Create an account at OpenAI

You’ll need to create an account at OpenAI. To do this, head to and click “Sign Up.” In the upper-right corner of your screen, you’ll see a link that reads “Create New Account.” Clicking this button will open up a form where you can provide an email address and password.

Once you have created an account, enter your name and address as well as your date of birth. You also have the option of providing a phone number so they can send verification codes via text message (if this is important to you). Finally, make sure that your username is unique so that no one else uses it!

Claim your GPT-3 API key

To get started, you’ll need to claim your GPT-3 API key.

To do this, follow these steps:

Click on the “API Key” tab at the top of the screen and then click on “Generate API Key.”

On this page, copy your new access token from your browser’s clipboard (right click > Copy).

Set up a node project

Create file called .env in the root directory of the project and add the following line


add your openai key that you copied above to the appropriate place in this place.

Install openai node package

To get started, you’ll need to install openai npm package.

npm install openai --save-dev

code for calling for gpt-3

Next, create a file named gpt3.js.

Type below code into the file:

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
const openai = new OpenAIApi(configuration);

const response = await openai.createCompletion("text-davinci-002", {
prompt: "Write a blog post on vadala onions",
temperature: 0.7,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,


running node gpt3.js will return a response from gpt-3 api.

Code explanation

The code above is a simple example of how to use the OpenAI API.

The first thing we do is import the OpenAI API and Configuration.

The Configuration object is used to set the API key.

The OpenAIApi object is used to make requests to the API.

The createCompletion method is used to create a completion.

The parameters are:

  • prompt: The prompt to complete.
  • temperature: The temperature parameter for the GPT-3 model.
  • max_tokens: The maximum number of tokens to return.
  • top_p: The top_p parameter for the GPT-3 model.
  • frequency_penalty: The frequency_penalty parameter for the GPT-3 model.
  • presence_penalty: The presence_penalty parameter for the GPT-3 model.

The response contains the completion.

If you want to convert this template into a full fledged app, you can use this NextJS/ReactJS code generator

I have used this code generator to build multiple apps for my clients as well as for myself. It comes with built-in auth, payments intgerations and loads of other features and will cut short your developmen time by months.


That’s it! You are now ready to display GPT-3 generated text in any JavaScript application. In this tutorial, we used the OpenAI API to call the completion endpoint. To learn more about other endpoints and use cases, check out other articles on this site.

Some of my other GPT-3 posts: