Skip to content
,

How to Get an OpenAI API Key (2026): Step by Step, Billing, Projects and Safety

7–11 minutes
Smiling red-haired woman in a knit sweater on a park bench holding a card with the OpenAI logo, with the text Get Your Key in 5 Minutes

An OpenAI API key is what lets your own code, scripts and apps (and tools like n8n, Cursor or Claude Code plugins) talk to OpenAI’s models.

Getting one takes about five minutes, but a few things trip people up: the key is shown only once, a ChatGPT subscription does not include API credit, and a brand-new key returns errors until you add prepaid credit.

This guide walks you through getting an OpenAI API key step by step in 2026, adding credit, testing the key with a real request, setting it up on Windows, Mac and Linux, keeping it safe, and fixing the errors you are most likely to see.

TL;DR

  • Where: sign in at platform.openai.com → Settings → API keys → Create new secret key.
  • Copy it immediately. The full key is shown once only. Lost it? Create a new one.
  • Add credit: Settings → Billing. Minimum purchase $5 (default $10); credits expire after one year.
  • ChatGPT Plus or Pro does not cover API usage. They are billed separately.
  • Store it in the OPENAI_API_KEY environment variable; official SDKs read it automatically.
  • Stay safe: never put the key in browser or mobile code or in git; set an expiry, spend limits and rotate keys.

Before You Start

  • An OpenAI account. Your ChatGPT login works on the platform site; or create a new account.
  • A supported country. The API only works in OpenAI’s supported regions. Requests from elsewhere fail with a 403 error.
  • A payment card for prepaid credit. API calls are not free.
  • Know the structure: your account belongs to an organization (which holds billing and limits), and work is grouped into projects. A Default project is created for you and cannot be deleted.

Step 1: Create Your OpenAI API Key

  1. Sign in at platform.openai.com (not chatgpt.com).
  2. Open Settings → API keys. The page lives under your organization settings.
  3. Click Create new secret key.
  4. Choose the owner. Pick You for a personal key, or a service account for servers and CI so the key is not tied to one person.
  5. Pick a project. The Default project is fine for a first test. Usage and limits are tracked per project. Only organization owners can create new projects.
  6. Set permissions. All (default), Restricted (choose None, Read or Write per endpoint) or Read Only.
  7. Set an expiry date. OpenAI recommends expiring and rotating keys. Your admin may enforce a maximum lifetime.
  8. Copy the key and store it safely, for example in a password manager. You will not be able to see it again.
What does a key look like? Project keys usually start with sk-proj- followed by a long random string. OpenAI does not promise a fixed format, so never validate keys by pattern in your code. Treat the whole string like a password.

Step 2: Add Credit (Billing)

A new key works only after your organization has credit. Without it, every request returns a 429 error with the code credit_balance_exhausted.

  1. Go to Settings → Billing.
  2. Add a payment method.
  3. Buy credit. The minimum is $5 and the default suggestion is $10. Start small while you test.
  4. Decide on auto-reload. It is on by default during setup; turn it off if you want a hard stop when credit runs out.
  5. Set limits. On the Limits page, add an email alert at a dollar amount and, if you like, a monthly spend limit. Per-project spend limits are available too.
  • Credit can take a few minutes to appear.
  • Purchased credits expire after one year and are generally non-refundable.
  • Any free promotional credits are used before purchased ones, but new accounts are not guaranteed any.

Coming from ChatGPT? Your Plus, Go or Pro subscription does not include API credit. See our ChatGPT plans comparison for what the app plans cover.

Step 3: Save the Key as an Environment Variable

Do not paste the key into your code. Store it in the OPENAI_API_KEY environment variable; the official Python and Node SDKs read it automatically.

System Command Notes
macOS / Linux (current terminal) export OPENAI_API_KEY="sk-proj-..." Add to ~/.zshrc or ~/.bashrc to keep it
Windows (permanent) setx OPENAI_API_KEY "sk-proj-..." Open a new terminal afterwards
Windows PowerShell (current session) $env:OPENAI_API_KEY="sk-proj-..." Lasts until you close the window
Project .env file OPENAI_API_KEY=sk-proj-... Add .env to .gitignore

Step 4: Test Your Key

With curl

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 JSON with the reply in output and token counts in usage. GPT-6 Luna is OpenAI’s cheapest current model, so tests cost a fraction of a cent.

With Python

pip install openai
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY
resp = client.responses.create(model="gpt-6-luna", input="Say hello in five words.")
print(resp.output_text)

If you get an error instead, jump to the troubleshooting table below or our full OpenAI API errors guide.

Key Options: Projects, Permissions and Service Accounts

Option What it does When to use it
Projects Separate usage, limits and keys One project per app, and separate staging and production
Personal (You) key Tied to your user, reaches only its project Local testing and personal scripts
Service account key Not tied to a person Servers, CI and team apps
Restricted permissions None, Read or Write per endpoint Apps that need only one API
Read Only Cannot create or change resources Dashboards and monitoring
Expiry Key stops working on a date Always; rotate before it expires
IP allowlist Requests only from listed IPs Production servers with fixed IPs

Usage Tiers and Limits

Your organization moves up tiers automatically as your total credit purchases grow. Tiers set your rate limits and monthly usage cap; they do not give free usage.

Tier How you qualify Monthly usage limit
Free Account in a supported country $100
Build $5 total credit purchased $500
Launch $100 total credit purchased $5,000
Grow $500 total credit purchased $200,000

Rate limits apply per organization and project. Check your exact limits on the Limits page.

What It Costs

Creating a key is free; you pay per token used. Standard prices per million tokens for the main models (as of late 2026):

Model Input Output Good for
gpt-6-luna $0.10 $0.50 Tests, simple tasks, high volume
gpt-6.1-sol $2.00 $10.00 Most apps
gpt-6-astra $10.00 $50.00 Hardest reasoning tasks

Batch and Flex processing cost half the standard rate. For every model and discount, see our OpenAI API pricing guide.

Keep Your Key Safe

  • Server side only. Never put the key in front-end JavaScript, mobile apps or browser extensions. Call OpenAI from your own server.
  • Never commit it. Keep keys in environment variables or a secrets manager, and add .env to .gitignore.
  • One key per app and environment, so you can revoke one without breaking everything.
  • Set an expiry and rotate: create the new key, deploy it, verify it, then revoke the old one.
  • Add limits: per-project spend limits, email alerts and an IP allowlist.
  • Leaked key? Revoke it on the API keys page immediately and create a new one.
  • Beware “free API key” offers. Shared or sold keys are against OpenAI’s terms and often stolen.

Using the Key in Popular Tools

Tool Where the key goes
Python / Node SDK OPENAI_API_KEY environment variable
n8n, Make, Zapier Create an OpenAI credential and paste the key
VS Code extensions and editors The extension’s settings or API key field
Open WebUI and other local front-ends Admin settings → Connections → OpenAI API
LangChain Reads OPENAI_API_KEY automatically

Need JSON-shaped responses in your app? See structured outputs for Claude, OpenAI and Gemini.

Troubleshooting Common Errors

Error Meaning Fix
401 Incorrect API key provided Wrong, revoked or mangled key Copy the key again; check for spaces or line breaks
401 missing authentication No key sent Check the Authorization: Bearer header or that the env variable is set in this terminal
401 must be a member of an organization Account not in an organization Accept the invite or create an organization
401 on one endpoint only Restricted key without that permission Give the key Write access to that endpoint
401 IP not authorized Request outside the IP allowlist Add your server IP to the allowlist
403 country not supported Unsupported region Use the API only from a supported country
429 credit_balance_exhausted No credit Add credit in Billing; retrying will not help
429 rate limit Too many requests Back off and follow the Retry-After header
503 server overloaded Temporary OpenAI load Retry after a short wait
Windows: key “not found” after setx Old terminal session Close and reopen the terminal

My Take

Create a project per app, use a service account key for anything that runs on a server, add $5 to $10 of credit with an email alert, and test with GPT-6 Luna. That setup costs almost nothing and keeps you safe from surprise bills.

The two mistakes I see most are expecting ChatGPT Plus to cover the API and pasting keys into front-end code. Avoid both and you are set.

FAQ

Is the OpenAI API key free?

Creating a key is free, but using it is not. You need prepaid credit, with a $5 minimum purchase.

Does ChatGPT Plus include API access?

No. ChatGPT subscriptions and the API are billed separately. You need API credit even if you pay for Plus or Pro.

Where do I find my OpenAI API key?

On platform.openai.com under Settings → API keys. You can see the key’s name and last characters, but the full key is shown only once when created.

I lost my API key. Can I see it again?

No. Revoke the old key and create a new one.

Why does my new key return a 429 error?

Usually because the organization has no credit (credit_balance_exhausted). Add credit in Billing and wait a few minutes.

How many API keys can I create?

You can create separate keys per project and per app. Use one key per app and environment so you can revoke them independently.

Do OpenAI API credits expire?

Yes. Purchased credits expire after one year and are generally non-refundable.

Is it safe to share my API key with a tool?

Only with tools you trust that run on your machine or your server. Never share it publicly, and set spend limits in case it leaks.

Sources

Free weekly email

Get new AI guides every week

One short weekly email with our newest local AI, ComfyUI and AI tool guides. No spam, no pop-ups, just the good stuff.

Free. Unsubscribe anytime in one click. We only use your email to send this newsletter. See our privacy policy.

Smiling red-haired woman in a knit sweater on a park bench holding a card with the OpenAI logo, with the text Get Your Key in 5 Minutes