How to Create & Use OpenAI API Key in 2026: Step-by-Step Guide
Jul 23, 2026 4 Min Read 5860 Views
(Last Updated)
If you have ever wanted ChatGPT running inside your own app instead of a browser tab, the OpenAI API is where that starts. You get a OpenAI ChatGPT API key, you write a few lines of code, and your project can talk to ChatGPT directly.
This guide walks you through creating that key in 2026, using it in a real script, and keeping it safe. No prior API experience needed.
Table of contents
- TL;DR Summary
- What is an OpenAI ChatGPT API Key?
- ChatGPT Account vs OpenAI API Account
- How to Create an OpenAI API Key in 2026 (Step by Step)
- Step 1: Sign up on the API platform
- Step 2: Set up your organization and project
- Step 3: Open the API keys page
- Step 4: Generate your key
- Step 5: Add billing
- How to Use Your API Key in Python?
- OpenAI API Pricing 2026: How Much Does It Cost?
- How to Secure Your OpenAI API Key?
- Common Mistakes Beginners Make
- Conclusion
- FAQs
- Is the OpenAI ChatGPT API key free?
- Should I use GPT-4o or GPT-5?
- Can I use one key across multiple projects?
- What happens if my key gets exposed?
- Why is my brand new key giving me errors already?
- Can I restrict what a key is allowed to do?
TL;DR Summary
- An OpenAI API key lets your own code send requests to ChatGPT models and get responses back.
- Create one at platform.openai.com under Settings → API keys → Create new secret key. It’s separate from your regular ChatGPT login.
- You need a payment method on file before the key works for anything beyond a tiny free trial credit.
- Store the key as an environment variable, never inside your code.
- GPT-4o is still supported for existing integrations, but GPT-5 is now OpenAI’s recommended default for new projects.
What is an OpenAI ChatGPT API Key?

An API key is a unique string that identifies your account every time your code contacts OpenAI’s servers. Think of it as a digital ID card. Without it, OpenAI has no way to know who is asking or who to bill.
Every request your app sends carries this key in the background. OpenAI checks it, confirms your account has active billing, and returns a response.
ChatGPT Account vs OpenAI API Account
This trips up almost everyone at first. Here’s the distinction:
- ChatGPT (chat.openai.com) is the consumer website where you type and get replies.
- The OpenAI API (platform.openai.com) is for developers. It powers your own apps and scripts.
- A ChatGPT Plus subscription does not include API credit. You’re billed separately for each.
- You can use the same email for both, but they run as two independent accounts.
This confuses almost everyone when they first start. The short answer is no, they are two completely separate things.
Take your ChatGPT skills to the next level at absolutely no cost!
Through HCL GUVI’s Bharat AI Initiative, powered by OpenAI, you can now learn advanced ChatGPT skills, like better prompting techniques, in English, Hindi, Tamil, Telugu, and Marathi.
How to Create an OpenAI API Key in 2026 (Step by Step)

Step 1: Sign up on the API platform
Go to platform.openai.com and click Sign up. You can register with an email, or sign in with Google, Microsoft, or Apple. Confirm the verification email, and verify your phone number if asked.
Step 2: Set up your organization and project
On first login, OpenAI asks you to name an organization (a label for your account) and create a project (a folder that groups your keys and usage). Name the project something clear, like my-first-app.
Step 3: Open the API keys page
From the left sidebar, click Settings, then API keys. You can also go directly to platform.openai.com/api-keys. This page lists every key tied to your account.
Step 4: Generate your key
Click Create new secret key. You’ll be asked to:
- Name the key, for example test-key or portfolio-project
- Choose permissions (start with full access; you can restrict this later)
- Pick which project it belongs to
Click Create secret key. The key appears once, starting with something like sk-proj-.... Copy it immediately into a password manager or secure notes app. OpenAI will never show it to you again.
Step 5: Add billing
Go to Settings → Billing and add a payment method. New accounts get a small trial credit, but real usage needs an active card on file. While you’re there:
- Set a monthly spending limit so a bug in your code never surprises you with a large bill
- Turn on auto-recharge if you want your balance to top up automatically
How to Use Your API Key in Python?
Once your key exists, store it as an environment variable rather than pasting it into your script. Create a .env file in your project folder:
OPENAI_API_KEY=paste-your-key-here
Add .env to your .gitignore so it never gets pushed to GitHub. Then install the SDK:
pip install openai python-dotenv
Here is a working example using GPT-4o, still one of OpenAI’s supported multimodal models for existing integrations:
import os
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Explain what an API key is in simple terms."}]
)
print(response.choices[0].message.content)
If you’re starting a new project today, OpenAI recommends the newer Responses API with GPT-5, which handles more features natively:
response = client.responses.create(
model="gpt-5",
input=[{"role": "user", "content": "Explain what an API key is in simple terms."}]
)
print(response.output_text)
Run either file with python your_filename.py. You’ll see a reply within a couple of seconds.
OpenAI API Pricing 2026: How Much Does It Cost?
There’s no subscription fee for the API itself. You pay only for what you use, measured in tokens (roughly four characters of text each).
| Model | Best For | Input per 1M Tokens | Output per 1M Tokens |
|---|---|---|---|
| GPT-5 | Everyday tasks, strong quality and speed | $1.25 | $10.00 |
| GPT-5 Mini | High-volume, budget-friendly use | $0.25 | $2.00 |
| GPT-4o | Existing multimodal integrations | $2.50 | $10.00 |
| GPT-4o mini | Lightweight legacy projects | $0.15 | $0.60 |
For a beginner just experimenting, GPT-5 Mini or GPT-4o mini are the cheapest way to learn the API without worrying about cost.
How to Secure Your OpenAI API Key?
Your key is effectively a password to your billing account. A few rules keep it safe.
Never expose it in frontend code. If you write your key directly into JavaScript that runs in a browser, anyone can open dev tools and steal it. All API calls must go through a backend server you control, never client-side code.
Use environment variables, not hardcoded strings. Keep the key in .env locally and in a secrets manager (AWS Secrets Manager, Google Cloud Secret Manager, Doppler) in production.
Restrict permissions per key. A key used only for chat completions doesn’t need billing or model-training access.
Rotate and revoke fast. If you suspect a key leaked, for example after accidentally pushing it to GitHub, delete it from platform.openai.com/api-keys immediately and generate a fresh one. Bots scan public repositories for exposed keys around the clock.
Common Mistakes Beginners Make
- Skipping the spending limit: A stuck loop in your code can burn through credit fast. Set a cap before your first real test.
- Hardcoding the key in the script: This is the single most common way keys leak publicly. Use environment variables from day one.
- Confusing ChatGPT login with API access: A ChatGPT Plus subscription won’t unlock API calls. You need a separate platform account with billing set up.
- Forgetting the environment variable on deployment: Code that works locally often fails on a live server because the key was never added to the hosting platform’s settings.
Automated bots scan public GitHub repositories around the clock specifically looking for exposed API keys, and a leaked key can rack up charges within minutes of being found.
Do check out HCL GUVI’s AI/ML Course if you want to move beyond basics and learn AI and ML through real-world projects. The program focuses on core concepts like machine learning, deep learning, and model deployment with hands-on practice and mentorship, helping you build practical industry-ready skills.
Conclusion
Getting an OpenAI API key is genuinely a five-minute task once you know where to click. The harder part, and the more valuable one, is what you do after: storing it safely, picking the right model for your budget, and writing your first working call.
Set up your account, generate your key, run the Python example above, and you’ll have ChatGPT responding to your own code for the first time. From there, what you build with it is up to you.
FAQs
1. Is the OpenAI ChatGPT API key free?
New accounts get a small trial credit. After that, it’s pay-as-you-go with no monthly fee.
2. Should I use GPT-4o or GPT-5?
GPT-4o still works for existing projects, but GPT-5 is OpenAI’s current recommendation for anything new, with better reasoning and a larger context window.
3. Can I use one key across multiple projects?
You can, but a separate key per project makes usage tracking and revoking access much easier.
4. What happens if my key gets exposed?
Revoke it instantly at platform.openai.com/api-keys, generate a new one, and check your usage history for unfamiliar activity.
5. Why is my brand new key giving me errors already?
Most likely you’re still on the free tier with very low rate limits. Add $5 in credit to unlock a higher usage tier.
6. Can I restrict what a key is allowed to do?
Yes. When creating a key, you can limit its permissions so it can only perform specific actions, like chat completions but not billing changes.



Did you enjoy this article?