This is a beginner's introduction to OpenAI's API in Postman. Make your first API request to simulate a ChatGPT query.

OpenAI logo

Prerequisites

What You'll Learn

What You'll Build

  1. Sign in to your Postman account.
  2. Select the Run in Postman button below to fork the public collection to your own workspace.
    Fork the OpenAI collection
  3. Enter a label for your fork, and select a workspace to save it to. fork the collection modal

If you leave the checkbox selected to Watch original collection, you will receive notifications when new endpoints are added to the collection.

Update the collection variable token with your own OpenAI API key.

  1. On the API Keys page of the OpenAI website, create a new API key under API Keys > + Create new secret key, and then copy the key to your clipboard for the next step. openAI API key
  2. In Postman, select your fork of the OpenAI collection.
  3. Under the Variables tab, create a new collection variable token and paste the value of your API key. add API key
  4. Select Save.

You can look under the Authorization tab and see how Postman will now add an Authorization header with your API key to every request in the collection, unless otherwise specified. Look under the Headers tab of any request to see for yourself. You may need to un-hide the auto-generated headers.

auto-generated headers

To change the way Postman configures the authorization, you can update the settings under the Authorization tab of the request, folder, or collection.

This example uses the Chat API and the gpt-3.5-turbo artificial intelligence model to perform a single-turn query or turn-based chat, similar to what you can do on the ChatGPT website.

To create a query:

  1. In your Postman workspace, navigate to your fork of the OpenAI collection.
  2. Select Chat > Creates a completion for the chat message. Under the Body tab, add a request body formatted like the following example:
    {
      "model": "gpt-3.5-turbo",
      "messages": [
        {
          "role": "user",
          "content": "What is OpenAPI?"
        }
      ]
    }
    
  3. Click Send to see a response body like the following example:
    {
      "id": "chatcmpl-6sf37lXn5paUcuf8UaurpMIKRMsTe",
      "object": "chat.completion",
      "created": 1678485525,
      "model": "gpt-3.5-turbo-0301",
      "usage": {
        "prompt_tokens": 12,
        "completion_tokens": 99,
        "total_tokens": 111
      },
      "choices": [
        {
          "message": {
            "role": "assistant",
            "content": "\n\nOpenAPI (formerly called Swagger) is a specification for building APIs (Application Programming Interfaces). It describes the operations and parameters that an API can accept, as well as the data structures that are returned from API calls. The specification supports both JSON and YAML formats and provides a standard format for describing RESTful APIs. By using OpenAPI, developers can describe their APIs in a consistent, machine-readable format that can be easily consumed by API documentation tools, code generators, and other programming tools."
          },
          "finish_reason": "stop",
          "index": 0
        }
      ]
    }
    

Upon a successful 200 OK response, you can inspect the response body, including the property choices which contains text responding to the query.

successful API request

Now that you have the API request working the way you want it to, let's generate code so we can use the API in our own applications.

  1. Select the request you want to use for a code snippet, then select the code icon in the right pane.
  2. Select a language or framework from the dropdown list. code generation
  3. Select the copy icon to copy the code snippet to your clipboard.

You can now paste this code into your own applications or bots.

What we've covered

Now that you made your first call with the OpenAI API, continue exploring the other endpoints in the OpenAI collection and other artificial intelligence models.

Additional resources

To continue your journey with OpenAI, here are some additional resources to check out.