How to Get an OpenAI API Key (2026): Create, Fund, Test and Secure It
On this page
Short answer: sign in at platform.openai.com, go to Settings → API keys, click Create new secret key, choose a project, set an expiration, and copy the key immediately. It starts with sk-proj-. Then add prepaid credits under Settings → Billing so the key can do more than a test request.
The error messages on this page are real responses from the OpenAI API, recorded on October 15, 2026.
API key vs ChatGPT subscription
An API key is for software: your own code, scripts and developer tools calling OpenAI models directly, billed per token from prepaid credits on the OpenAI Platform.
A ChatGPT plan (Free, Go, Plus, Pro, Business) is for the ChatGPT apps. It also covers Codex when you sign in with ChatGPT. It doesn't give you API credits, and the two are billed separately.
If all you want is Codex, a ChatGPT Plus plan or higher may be simpler than an API key. See Install Codex CLI.
Step 1: Sign in to the OpenAI Platform
Go to platform.openai.com and sign in with the same account you use for ChatGPT, or create one. Your account belongs to an organization, which holds billing and limits, and work is grouped into projects. A default project is created for you.
The API is only available in OpenAI's supported countries. From anywhere else, requests fail with a 403 (see errors).
Step 2: Create the key
- Open Settings → API keys (
platform.openai.com/settings/organization/api-keys). - Click Create new secret key.
- Choose the owner:
- You: a user-owned project key for your own development.
- Service account: for servers, CI and anything that shouldn't depend on one person's account.
- Pick the project the key belongs to. Usage and limits are tracked per project.
- Set an expiration date. OpenAI recommends expiring keys and rotating them on a schedule. Your admin may also enforce a maximum key lifetime.
- Click create and copy the key now. You won't be able to see it again.
Step 3: Add credits
Open Settings → Billing and add prepaid credits. Your organization's usage tier then rises automatically as total purchases grow, and the tier sets both your rate limits and your monthly usage cap:
| Tier | Qualification | Monthly usage limit |
|---|---|---|
| Free | Account in a supported country | $100 |
| Build | $5 in total credit purchases | $500 |
| Launch | $100 in total credit purchases | $5,000 |
| Grow | $500 in total credit purchases | $200,000 |
On the Limits page, set an email alert at a dollar amount you'd notice, and a hard spend limit if you want a firm monthly ceiling. For per-model prices, see OpenAI API pricing.
Step 4: Test the key
Put the key in an environment variable, so it stays out of your code and shell history:
export OPENAI_API_KEY="sk-proj-..." # macOS / Linux
setx OPENAI_API_KEY "sk-proj-..." # Windows (then open a new terminal)
Then send one request to the Responses API with the cheapest current model:
curl https://api.openai.com/v1/responses \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-6-luna","input":"Say hello in five words."}'
A working key returns a JSON response with the model's text in output and token counts in usage. With GPT-6 Luna at $0.10 per million input tokens, the request costs a tiny fraction of a cent.
Here's what we got with a deliberately invalid key:
{
"error": {
"message": "Incorrect API key provided: sk-proj-*******************g123. You can find your API key at https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"code": "invalid_api_key",
"param": null
}
}
And with no key at all:
{"error":{"message":"Missing bearer or basic authentication in header","type":"invalid_request_error","param":null,"code":null}}
The masked key in the first message (sk-proj-***…g123) shows the first and last characters of what OpenAI actually received. Compare it with your key to spot a truncated paste.
Keep the key safe
- Never ship it in client-side code (browser JavaScript, mobile apps) or commit it to a repository. Call OpenAI from your server.
- One key per app and environment. Separate projects for staging and production let you revoke one without breaking the other, and give each its own spend limit.
- Set an expiration and rotate. Create the replacement, deploy it, confirm it works, then revoke the old key.
- Lock it down further with an IP allowlist (organization security settings) and per-project spend limits.
- If a key leaks, revoke it immediately on the API keys page. Usage can pile up fast.
Common errors
| Status | Message or code | What it means |
|---|---|---|
| 401 | Incorrect API key provided |
Wrong, revoked or mangled key |
| 401 | You didn't provide an API key… (the error docs call this Missing bearer or basic authentication in header) |
No key was sent. Check the Authorization: Bearer … header or that your environment variable is set |
| 401 | You must be a member of an organization to use the API |
Your account isn't in an organization yet |
| 401 | IP not authorized |
The request came from outside your project's IP allowlist |
| 403 | Country, region, or territory not supported |
You're calling from an unsupported location |
| 429 | credit_balance_exhausted |
No prepaid credits left. Add credits; retrying won't help |
| 429 | organization_spend_limit_exceeded / project_spend_limit_exceeded |
You hit a spend limit you set |
| 429 | Rate limit reached for requests / slow_down |
Too many requests too quickly. Back off and follow Retry-After |
| 503 | server_is_overloaded |
The model is temporarily overloaded. Retry after a short wait |
For any 429, read error.code first. Billing and quota errors look similar to rate limits, but they need a top-up or a higher limit, not a retry.
Use the key in tools
- OpenAI SDKs read
OPENAI_API_KEYautomatically. - Codex CLI:
printenv OPENAI_API_KEY | codex login --with-api-key. Usage is then billed at API prices. See Install Codex CLI. - Desktop apps such as Cherry Studio: paste the key into the OpenAI provider.
Can't get a key?
If you're outside the supported countries, or can't add a payment method OpenAI accepts, the usual alternatives are Azure OpenAI (billed through an Azure account) or an API gateway that resells model access with its own keys. api.foo, which publishes these docs, is one of them. It accepts OpenAI-format requests at https://api.foo/v1. Whichever you choose, check how it handles your data and billing.
Related
FAQ
Is the OpenAI API key free?
Creating a key is free. OpenAI's quickstart lets you run a free test request, but real usage is billed per token from prepaid credits that you add on the Billing page. A ChatGPT subscription doesn't include API credits.
Where do I find my OpenAI API key?
Keys are managed at platform.openai.com under Settings → API keys. The full secret is shown only when you create it. If you lost it, create a new key and revoke the old one.
What does an OpenAI API key look like?
Project keys start with sk-proj- followed by a long random string. Treat the whole string as a password.
Is ChatGPT Plus the same as an OpenAI API key?
No. ChatGPT plans and the API are billed separately. A ChatGPT plan covers the ChatGPT apps and Codex usage when you sign in with ChatGPT, while API keys bill per token to your OpenAI Platform account.
Why do I get "Incorrect API key provided"?
The key is wrong, revoked, or copied with extra characters. OpenAI's error message shows a masked version of the key it received, so you can compare the first and last characters with the key you meant to use.
Why do I get a 429 error with a new key?
Either you're sending requests too fast for your usage tier, or your organization has no prepaid credits left (credit_balance_exhausted) or has hit a spend limit. Check error.code in the response to see which.
Found a mistake? Email [email protected].