How to make GPT-4 API call: Step-by-step guide

GPT-4 is the most recent and advanced language model released by OpenAI. Here, we will guide you step by step on how to call and use the OpenAI’s GPT-4 model’s API.

By using the GPT-4 API, you can customize it for your specific needs and seamlessly incorporate GPT-4 into your project as a developer.

How to access and call GPT-4 API?

To use a GPT-4 model via the OpenAI API, you’ll send a request containing the inputs and your API key, and receive a response containing the model’s output.

We will initially demonstrate this in Node.js, then we will explain how to do it in other programming languages; however, the processes will be quite similar to each other.

You can also refer to OpenAI’s API documentation to gain further insights and information.

Let’s take a look at how to send request GPT-4 API and receive a response.

1. Create a new project

First, let’s create a new folder for our source code.

$ mkdir gpt-4-api
$ cd gpt-4-api

After that, to create a new package.json file inside this project folder, use command as follows:

$ npm init -y

Then we need to add a package called openai. This package provides convenient access to the OpenAI API from Node.js applications.

$ npm i openai

2. Generate OpenAI API Key

To use GPT4 you need to use the Chat Completion endpoint and have been granted access to GPT4 model.

As of now, only all paying API customers have access to GPT-4 key. However, for test purposes you can generate other free GPT tokens without being a paying customer as below:

generate gpt-4 key to use gpt-4 api
  1. Go to
  2. If you don’t have an account, create a new one. If you already have an account, log in using your existing credentials.
  3. Click on the account icon in the top right corner of the screen and select ‘View API Keys’ from the dropdown menu.
  4. In the opened window, click on the ‘Create a new secret key’ button to generate your API key and save it.”

3. Import the dependencies

Now we can start writing the code to call the GPT-4 API.

First, create a file named index.js inside the folder we created at the beginning. You can choose any name for the file.

Inside this file, we can import the OpenAI package that we installed earlier.

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

The Configuration and OpenAIApi classes are imported from the openai package, which is used to connect to and use the OpenAI API.

4. Setting up the configuration and api

Now, we can create our configuration variable that contains our API key, and then call the OpenAI function, passing the configuration file as a parameter to create our api variable.

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
const openai = new OpenAIApi(configuration);
  • A new Configuration instance is created with an API key obtained from the OPENAI_API_KEY environment variable. The value here is the OpenAI token we generated in step 2. Instead of directly writing this value here, it would be more appropriate to define it as an environment variable for security reasons.
  • An OpenAIApi instance is created using the Configuration instance.

To set environment variables, follow the steps below for each operating system:

  • macOS and Linux: use command “export OPENAI_API_KEY=GeneratedOpenAIToken'”
  • Windows: use command “set OPENAI_API_KEY=GeneratedOpenAIToken”

5. Create and Call GPT-4 Chat Completion Function

Chat models take a list of messages as input and return a model-generated message as output.

We will use the createChatCompletion method of the openai object we created and send an object with two parameters: model and messages.

const completion = await openai.createChatCompletion({
  model: "gpt-4",
  messages: [{role: "user", content: "Hello world"}],
  • The openai.createChatCompletion method is utilized to generate a response from the OpenAI API.
  • The model parameter indicates the specific AI model to be used for generating the response.
  • The messages parameter offers context for the AI model to generate the response.

We can use await to call this function asynchronously, and then simply print the returned response using console.log


6. Run the application

We have implemented our code, and now the only thing left is to run it. To do this, you can execute the command node index.js in the console.

We had hardcoded ‘Hello world’ as the input parameter for GPT-4, but you can send a different value or make it dynamically accept input parameters when you run it

Customizing API calls with parameters

In the previous section, we only sent two parameters, model and messages, when creating the createChatCompletion function. However, the function accepts more parameters beyond these two.

To have control over the behavior of the GPT-4 model and customize its output according to your needs, you can use various parameters in your API calls. Below, we have provided a detailed explanation of additional parameters you can include in this function:

messagesAn array of messages in chat format, consisting of message objects with ‘role’ (“system,” “user,” or “assistant”) and ‘content’ (the content of the message).
temperatureControls the randomness of generated text.
top_pAn alternative to temperature, known as nucleus sampling.
nThe number of chat completion choices to generate for each input message.
streamIf set to true, partial message deltas will be sent.
stopSpecifies up to 4 sequences where the API should stop generating further tokens.
max_tokensThe maximum number of tokens to generate in the chat completion.
presence_penaltyA number between -2.0 and 2.0. Positive values penalize new tokens based on their appearance.
frequency_penaltyA number between -2.0 and 2.0. Positive values penalize new tokens based on their frequency.
logit_biasA JSON object mapping tokens to associated bias values.
userA unique identifier representing your end-user.
OpenAI GPT API Parameters

By customizing these parameters, you can guide the behavior of the GPT-4 model and generate text that aligns with your specific requirements.

Also read: All OpenAI’s GPT Models Explained

How to call GPT-4 API in Python

As previously mentioned, the fundamental principles of calling the GPT-4 API are the same across all programming languages, with only syntax differences. To reinforce the topic, let’s take a look at how to call the GPT-4 API in Python.

Install the OpenAI Python Package

You need to install the OpenAI Python package if you haven’t already. You can do this using pip:

pip install openai

Import the OpenAI Package

In your Python script, import the OpenAI package:

import openai

Set Your API Key

You’ll need to set your API key, which you can obtain from the OpenAI platform. Replace 'your-api-key' with your actual API key.

openai.api_key = 'your-api-key'

Make a Request to the API

Now, you can make a request to the GPT-4 API. Here’s a simple example where we provide a prompt and ask for a completion:

response = openai.Completion.create(
    prompt="Translate the following English text to French: 'Hello, how are you?'",
    max_tokens=50  # You can adjust the max_tokens as needed

In this example, we specify the engine as “davinci” (the most capable engine), provide a prompt to the model, and set the max_tokens to limit the response length.

Extract and Use the Response

You can access the model’s response from the response object and use it in your application:

translated_text = response.choices[0].text

This code extracts the text generated by the model and prints it.

Remember to adapt the engine, prompt, and other parameters to your specific use case. This is a basic example, and you can make more complex requests based on your requirements.

How to send image input to the GPT-4 API?

OpenAI introduced GPT-4 as a multimodal model, meaning it can accept input not only in the form of text but also as images.

On September 26, 2023, they announced the GPT-4 Vision model, which specifically focuses on handling image inputs. This means you can send images as input to GPT-4 and implement various tasks related to those images.

Learn more: How to send image inputs to GPT-4 model?

How to call GPT models online?

To use any GPT API, programming knowledge is typically required. However, OpenAI has developed an online platform called the “OpenAI Playground.” With the OpenAI Playground, you can call specified GPT models with input parameters without needing any tokens or coding. You can view the API’s results easily.

openai playground

This feature allows you to gain insights into how the API and its parameters work without coding. To use the Playground, you’ll need to have a ChatGPT or OpenAI account and log in.

How to use GPT-4 model in ChatGPT?

To use the GPT-4 model, you can make a function call as described in the explanation, using your programming knowledge.

However, there is another way to use the GPT-4 model without requiring coding skills. After introducing the GPT-4 model, OpenAI made it accessible for ChatGPT Plus members.

Learn more: How to use GPT-4 model in ChatGPT?

As a ChatGPT Plus member, you can directly use the GPT-4 model through ChatGPT without needing any programming knowledge.


To integrate the GPT-4 model into your own applications, you need to call its API. With the capability of GPT-4 to accept image inputs, there are countless different domains where you can integrate it.

In this rapidly evolving era of artificial intelligence, by implementing what’s explained in this article, you can make GPT-4 API calls yourself.