How to Call OpenAI API from postman

By now, it's likely that the majority of people have come across the terms 'ChatGPT' and 'AI' in various contexts. OpenAI has truly pioneered a revolutionary technology by introducing generative Artificial Intelligence, turning these once-specialized terms into nearly household names. Moreover, it seems that the forthcoming years will unveil even more remarkable surprises, further solidifying OpenAI's role as the leader in the realm of artificial intelligence.

That is why it’s becoming more and more necessary for us to take advantage of this technology, regardless of our backgrounds.

OpenAI API is a very useful way to integrate the functionality of ChatGPT into our applications. In this blog post, which will hopefully be part of a series on AI, we're going to get a taste of ChatGPT from the view point of a programmer by directly calling the API version of ChatGPT from postman. Here is the step by step guide:

Account setup

  1. Visit OpenAI website at https://www.openai.com/.

  2. Login or create an account by clicking on 'Log in' button.

Retrieve API key

  1. Once logged in, you will be presented with two options: ChatGPT and API. Select the latter.

    ChatGPT vs API sections

  2. Navigate to the API keys section in the top navigation menu.

  3. The API key is a unique identifier used to authenticate requests. Click on 'Create new secret key' to generate a new key and give it a name. Once generated, copy and keep the key secure to use later to authenticate your requests.

  4. Obtain the request body:

    • Navigate to Playground and select Chat mode.

    • Select the desired model (for instance, gpt-3.5-turbo) then type any desired query and click on 'Submit'.

    • Navigate to 'View Code' section and copy the displayed curl command.

Postman setup

  1. Launch postman.

  2. Select 'import' and paste the curl command you copied from OpenAI website. Postman automatically parses the command and extracts information like headers, request body and URL as shown in examples below and populates them into their respective fields.

    Sample URL if you've chosen gpt-3.5-turbo:

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

    Sample request body:

  3. In the Headers tab, you'll notice the Authorization field with a given value of Bearer $OPENAI_API_KEY. Replace $OPENAI_API_KEY with the actual API key you copied earlier as shown below:

    This step is crucial because without valid API key, the server will reject the call as it is not be able to verify the authenticity of the request:

  4. Send the request and you will receive a response from OpenAI API similar to the following example:

That's it. We have sent a request to ChatGPT using OpenAI API from postman. The response we're more interested in is the "content" field, which contains the actual reply generated by ChatGPT.

Summary

To recap, we have obtained the API key from OpenAI website, configured an HTTP request body, URL and headers, including the API key, in postman. successfully sent a request to ChatGPT and received a response which contains, among other things, an id, model and ChatGPT generated content.

If you're interested to dive deep and learn more, head over to OpenAI's Quickstart Tutorial or explore ChatGPT interface and have fun.