Every ChatGPT account gets an API Key — it is billed separately.
In this guide, we’ll teach you:
- How to get the OpenAI’s GPT API Key
- So you can use it further in other projects.
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
- Go to the API Key page
- Click + Create new secret key
- Optional: Name your API key
- Copy the Key
- 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:
- Getting Python
- Installing the OpenAI package
Let’s go.
2.1. Python
Get the Python client over here: python.org/downloads
- Go to the Python Downloads section
- Select your Operation System
- Hit Download
- 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
- Open the page
- Select Download files (left side)
- Click on the latest version
- 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