Article last updated on:
December 27, 2023

Every ChatGPT account gets an API Key — it is billed separately.

In this guide, we’ll teach you:

Let’s check it out.

How to use ChatGPT’s API

You can use the ChatGPT API by: getting the key, installing OpenAI’s Python package, and making a request via the API key.

Let’s go into detail.

1. Get API Key

Go to this page: platform.openai.com/account/api-keys

Creating a new API Key in OpenAI
  1. Go to the API Key page
  2. Click + Create new secret key
  3. Optional: Name your API key
  4. Copy the Key
  5. Paste it in a secure document

You’re only allowed to see the API Key once.

Our suggestion: Make sure nobody else ever sees your API Key. Once leaked, people can take advantage of the key, use it, and have you billed for all their usage.

To make sure that doesn’t happen, store the key in a safe place.

2. Install Client

This step includes two sub-steps:

  1. Getting Python
  2. Installing the OpenAI package

Let’s go.

2.1. Python

Get the Python client over here: python.org/downloads

Downloading Python from the official website
  1. Go to the Python Downloads section
  2. Select your Operation System
  3. Hit Download
  4. Install the app in your computer.

First part of this step: ✅

2.2. OpenAI package

Access the official package on this page: pypi.org/project/openai/#files

Downloading the openAI package for Python
  1. Open the page
  2. Select Download files (left side)
  3. Click on the latest version
  4. Install the package

There you go. You’ve now got the Python x OpenAI set-up.

3. Make a Request

  • Below is one of our simple Python examples.
  • We’re asking ChatGPT to translate text from English to French:
import openai

openai.api_key = "your-api-key-here"
response = openai.Completion.create(
  model="text-davinci-002",
  prompt="Translate the following English text to French: '{}'",
  max_tokens=60
)

Note: Add your own API Key where it says “your-api-key-here“.

  • Extract the generated text from the API response.
  • Use the generated text in your application.

With this, you can integrate ChatGPT into your app, website, or other services.

Conclusion

You’ve just learned how to get and use ChatGPT’s API.

Now you have the power of using ChatGPT with your own projects.

Learn how to become more productive with our guides on how to use AI.

Thank you for reading this,

Ch David and Daniel

About the author

David, the head editor at Guides.ai, has four years of experience in Artificial Intelligence and Machine Learning. Join David and the team and explore AI tools and contributing to the creation and curation of AI educational content.