To get an OpenAI API key, sign in at platform.openai.com, open the API keys page (https://platform.openai.com/api-keys), create a new secret key and copy it immediately. Then store it in the OPENAI_API_KEY environment variable, and add credit under Billing once you move beyond a first test request.
The steps below follow OpenAI's own quickstart as of October 2026. The dashboard layout changes from time to time, so if a button has moved, the quickstart page links straight to the current key and billing pages.
Step 1: Create the key
- Go to
platform.openai.com, OpenAI's developer platform, and sign in or create an account. - Open the API keys page at
https://platform.openai.com/api-keys. The quickstart's "Create an API Key" button leads here. - Create a new secret key. Give it a name that says where it will be used, such as
laptop-devorbilling-service-prod, so you know what breaks if you delete it later. - Copy the key and store it straight away in a password manager or your team's secret store.
Create one key per app or environment rather than sharing one everywhere. When a key leaks or a project ends, you can delete that key alone.
Step 2: Add credit
The quickstart lets you make a free test request, then points you to Billing (https://platform.openai.com/account/billing/overview) to "Add credits to keep building" with higher limits. Without credit, requests fail with a 429 error even though the key itself is valid. How usage is charged per token is explained in how LLM API pricing works.
Step 3: Store the key in an environment variable
OpenAI's SDKs read the key from OPENAI_API_KEY automatically, so your code never needs to contain it.
macOS and Linux (add the line to ~/.zshrc or ~/.bashrc to keep it):
export OPENAI_API_KEY="your_api_key_here"
Windows, as given in the quickstart:
setx OPENAI_API_KEY "your_api_key_here"
setx saves the variable for future windows only. Microsoft's documentation is explicit that it is not available in the current window, so close the terminal and open a new one before testing. For the details on each OS, see how to set environment variables.
For a project, a .env file loaded by your framework is common. If you use one, add .env to .gitignore before your first commit.
Step 4: Test the key
The simplest test is listing models, which checks the key without generating any text. On macOS or Linux:
curl https://api.openai.com/v1/models -H "Authorization: Bearer $OPENAI_API_KEY"
In PowerShell:
Invoke-RestMethod https://api.openai.com/v1/models -Headers @{ Authorization = "Bearer $env:OPENAI_API_KEY" }
A working key returns a JSON list of models. The API authenticates with a standard HTTP Bearer header, Authorization: Bearer <key>, so any HTTP client works the same way.
To make a first real request, install the SDK (pip install openai or npm install openai) and run the example in the quickstart. It uses the Responses API:
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY
response = client.responses.create(
model="MODEL_NAME",
input="Write a one-sentence bedtime story about a unicorn.",
)
print(response.output_text)
Replace MODEL_NAME with a current model from OpenAI's models page; model names change more often than the rest of this code.
Errors a new key runs into
This is the real response to a request with an invalid key (HTTP 401):
{
"error": {
"message": "Incorrect API key provided: sk-inval**********-key. You can find your API key at https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}
| Error | Usual cause | Fix |
|---|---|---|
401 Incorrect API key provided | Typo, extra space or quote, a deleted key, or the variable not set in this terminal | Print the first few characters of the variable to check it is set; create a new key if needed |
401 You must be a member of an organization to use the API | The account is not in an organisation | Ask the organisation owner for an invite |
429 Credit balance exhausted (credit_balance_exhausted) | No prepaid credit left | Add credit under Billing |
429 Rate limit reached for requests | Too many requests too quickly | Slow down and honour the Retry-After header |
403 Country, region, or territory not supported | Calling from an unsupported location | See OpenAI's supported-countries page |
For handling rate limits in code, see how to handle LLM API 429 errors. If you belong to several organisations or projects, the API reference shows how to choose one per request with the OpenAI-Organization and OpenAI-Project headers.
Keeping the key safe
An API key is a password that spends money. OpenAI's API reference puts it directly: "Don't share it with others or expose it in any client-side code such as browsers or apps. Load API keys from an environment variable or key management service on the server."
- Never put it in front-end code. Anything shipped to a browser or a mobile app can be read by its users. Call the API from your own back end.
- Never commit it. Anyone who can read the repository, or its history, can use the key. Keep keys in environment variables and keep
.envout of Git. - Don't paste it into chats, tickets or screenshots. Error logs are a common leak, so avoid logging request headers.
- Delete keys you no longer use. Revocation takes effect within a few seconds.
- Watch usage. An unexpected jump in spend is often the first sign of a leaked key.
If a key does leak, delete it on the API keys page first and investigate second. The full response, including how to remove it from Git history, is in what to do if you leak an API key.
Frequently asked questions
- What if I lose my OpenAI API key?
- Create a new key on the API keys page, update the places that use it, and delete the old key. Copy every new key into a password manager or secret store as soon as you create it, so you don't depend on the dashboard to show it again.
- Why does a brand-new key return a 429 error?
- The most common reason is that the organisation has no prepaid credit. OpenAI's error code list includes 429 Credit balance exhausted, meaning no credits remain. Add credit under Billing; the key itself is fine.
- Can I put my OpenAI API key in front-end JavaScript?
- No. OpenAI's API reference says not to expose the key in client-side code such as browsers or apps, because anyone can read it there and spend your credit. Call the API from your own server and keep the key in an environment variable or key management service.
- How quickly does deleting a key take effect?
- OpenAI's API reference says revocations take effect within a few seconds. Most other changes that affect how a key authenticates propagate within 15 minutes, though they can take longer.