This is a beginner's introduction to OpenAI's API in Postman. Make your first API request to simulate a ChatGPT query.
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.
+ Create new secret key
, and then copy the key to your clipboard for the next step. token
and paste the value of your API key. 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.
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:
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?"
}
]
}
{
"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.
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.
You can now paste this code into your own applications or bots.
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.
To continue your journey with OpenAI, here are some additional resources to check out.