Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Testing OpenAI API with Postman

Introduction

This tutorial demonstrates how to use Postman to test the OpenAI API. Postman is a powerful tool for API testing and development, allowing you to easily send requests and view responses.

1. Setting Up Postman

Before you start, make sure you have Postman installed. You can download it from the Postman website.

2. Creating a New Request

Open Postman and create a new request. Select the HTTP method as POST and enter the following URL:

https://api.openai.com/v1/completions
                    

This URL is used to request text completions from the OpenAI API.

3. Setting Up Authorization

Go to the Authorization tab in Postman and select Bearer Token. Enter your OpenAI API key in the Token field.

4. Configuring the Request Body

Switch to the Body tab and select raw. Then, choose JSON as the format. Enter the following JSON data in the request body:

{
  "model": "text-davinci-003",
  "prompt": "Translate English to French: 'Hello, how are you?'",
  "max_tokens": 60
}
                    

5. Sending the Request

Click the Send button to send the request to the OpenAI API. You should see a response in the lower section of the Postman interface.

The response will contain the translated text from English to French.

6. Example Response

Here is an example of what the response might look like:

{
  "id": "cmpl-2dNXXbq2b9M8q",
  "object": "text_completion",
  "created": 1614639648,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "Bonjour, comment ça va?",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 7,
    "total_tokens": 16
  }
}
                    

In this example, the response contains the translated text "Bonjour, comment ça va?".