Sign in at platform.openai.com, open the API keys page in your organization settings and create a new secret key. Add prepaid credits in Billing (the minimum purchase is $5), save the key in an environment variable named OPENAI_API_KEY and call it only from server-side code. ChatGPT plans do not include API usage. Before anything goes live, set a hard spend limit, because an empty credit balance is not an instant cutoff.
To get an OpenAI API key, sign in at platform.openai.com, open the API keys page in your organization settings and create a new secret key. Then add prepaid credits (the minimum purchase is $5), store the key in an environment variable named OPENAI_API_KEY, and use it only from server-side code.
This guide follows OpenAI's own pages as read on 13 October 2026: the developer quickstart, the API reference, production best practices, the rate limits, spend limits and error codes guides, the pricing page and the help centre's prepaid billing article. Prices are US dollars. Some other help centre articles refused automated reading that day, so claims that rested on them have been removed.
- 1Sign in to the API platform
platform.openai.com. It is a separate product from ChatGPT with its own billing.
- 2Create a secret key
Organization settings, API keys. Name it after where it will run and set an expiration date.
- 3Add prepaid credits
Billing. The minimum purchase is $5 and auto-reload is on by default.
- 4Export it as OPENAI_API_KEY
The official SDKs read that variable, so the key never sits in your code.
- 5Send one test request, then cap spend
Use the Responses API, then enforce a hard monthly limit.
What you need first
- An OpenAI account. The sign-in page accepts an email address, Google, Apple, Microsoft or a phone number.
- A payment method for prepaid credits. A ChatGPT plan does not help here: OpenAI's plan docs say API token prices are separate from subscription usage.
- A supported country or territory. Requests from elsewhere return a 403 error.
New to the term? An API is how one program asks another to do something; the key is the password that says who is asking. See API in the glossary.
How to get an OpenAI API key, step by step
Step 1: Create the key
Sign in and open the API keys page. Create a new secret key and name it after where it will run, such as n8n-production. Copy it and store it somewhere safe straight away. OpenAI's best practices guide strongly recommends setting an expiration date when you create a project key and keeping a regular rotation routine.
If you cannot create one, an administrator may have restricted it. Organizations can allow only service-account keys, allow only user-owned project keys, or disable new key creation, and can enforce a maximum key lifetime.
Step 2: Add prepaid credits
New API accounts use prepaid billing. From OpenAI's prepaid billing article:
- Open the billing overview for the organization you want to fund.
- Select Add payment details and complete the payment step.
- Choose the first purchase. The minimum is $5 and the default is $10.
- Review Use auto-reload, which is turned on by default. Turn it off, or set a threshold and a monthly reload limit.
- Confirm. The balance can take a few minutes to update.
Purchased credits expire after one year and are non-refundable outside a short list of exceptions, so start small.
Step 3: Save the key as an environment variable
The quickstart exports the key so that each SDK reads it from the system environment:
# macOS / Linux
export OPENAI_API_KEY="your_api_key_here"
# Windows (PowerShell)
setx OPENAI_API_KEY "your_api_key_here"On Windows, open a new terminal after setx so the variable is visible. On macOS or Linux, add the export line to your shell profile to keep it between sessions.
Step 4: Test the key with one request
Install the SDK with pip install openai or npm install openai, then run the quickstart example. In Python:
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-6-astra",
input="Write a one-sentence bedtime story about a unicorn.",
)
print(response.output_text)Or in JavaScript, saved as example.mjs and run with node example.mjs:
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-6-astra",
input: "Write a one-sentence bedtime story about a unicorn.",
});
console.log(response.output_text);A reply means the key, billing and model access all work. Both examples are copied from the quickstart, which uses gpt-6-astra, the most expensive of the three featured models. For a test, swap in gpt-6-luna. If you see a 401 or a 429 instead, go to troubleshooting.
Which kind of credential you need
OpenAI's API reference lists three ways to authenticate, and its best practices guide adds service accounts. Most readers need the first row.
| Credential | What it reaches | Use it for |
|---|---|---|
| Standard API key | Application requests to models | Your app, script or automation |
| Service-account key | The same model requests, owned by a service account instead of a user | Production services and CI |
| Admin API key | Administration endpoints only: users, invites, projects, audit logs | Automating organization management |
| Workload identity federation | Short-lived access tokens in place of a stored key | Cloud workloads that should hold no long-lived secret |
If you belong to more than one organization, or use a legacy user key, pass the OpenAI-Organization and OpenAI-Project headers so usage is billed to the right place.
What the OpenAI API costs
You pay per token. Standard rates per million tokens for the three models OpenAI features on its models page, from the pricing page:
| Model | Input | Cached input | Output |
|---|---|---|---|
| gpt-6-astra | $10.00 | $1.00 | $50.00 |
| gpt-6.1-sol | $2.00 | $0.10 | $10.00 |
| gpt-6-luna | $0.10 | $0.01 | $0.50 |
Prompts with more than 272K input tokens are priced at twice the input rate and 1.5 times the output rate for the whole request. Batch and Flex processing cost half the standard rate when you can wait for results. For scale, a reply of 1,000 output tokens costs $0.0005 on GPT-6 Luna and $0.05 on GPT-6 Astra. To price your own prompt per request and per month, use the free OpenAI API cost calculator, which also lists Claude and Gemini models. Our LLM API pricing comparison sets these beside other vendors.
Usage tiers
Your organization's tier rises automatically as its total credit purchases grow, and higher tiers bring higher rate limits. OpenAI cut the paid tiers from five to three on 6 October 2026.
| Tier | Qualification | Monthly usage limit |
|---|---|---|
| Free | User in an allowed geography | $100 a month |
| Build | $5 in total credit purchases | $500 a month |
| Launch | $100 in total credit purchases | $5,000 a month |
| Grow | $500 in total credit purchases | $200,000 a month |
The usage limit is a ceiling OpenAI assigns, not credit it gives you. Rate limits apply per organization and per project, not per user, and vary by model: on the Build tier GPT-6 Luna allows 5,000 requests and 2,000,000 tokens a minute. Your own numbers are under Organization limits.
Set a hard spend limit before you ship
A spend alert only notifies you. A hard spend limit makes requests fail with a 429 once tracked spend reaches your cap, which bounds the cost of a runaway loop or a leaked key. From the spend limits guide:
- Go to Organization limits, or Limits in a project's settings.
- In Spend, select Edit spend limit and enter the monthly amount.
- Turn on Enforce a hard limit and select Save.
Security checklist
- Server only. OpenAI says never to expose a key in client-side code such as browsers or apps
- Load it from an environment variable or a secret management service, not from your codebase
- Keep it out of public repositories and out of any file you commit
- Set an expiration date and rotate: create the replacement, deploy it, verify it, then revoke the old key
- One key per app and environment, so you can revoke one without touching the rest
- Separate staging and production projects, each with its own rate and spend limits
- Enforce a hard spend limit and watch the Usage page
- For production, an IP allowlist rejects requests from other addresses even with a valid key
Troubleshooting common errors
The common failures are all in OpenAI's error codes guide. For anything billing-related, read error.code: the broader type can still say insufficient_quota.
| Response | Cause | Fix |
|---|---|---|
| 401 Incorrect API key provided | The key is wrong | Check for stray spaces, confirm the key, or generate a new one |
| 401 IP not authorized | Your IP is outside the allowlist | Send from an allowed IP or update the allowlist |
| 403 Country, region, or territory not supported | Unsupported location | See the supported countries list |
| 429 credit_balance_exhausted | No prepaid credits left | Add credits. Retrying will not help |
| 429 Rate limit reached | Requests or tokens too fast | Wait for Retry-After, or back off with jitter |
| 429 slow_down | Traffic grew too quickly | Reduce the rate, then ramp up gradually |
| 429 organization_spend_limit_exceeded | Your hard spend limit was reached | Raise or remove the limit, or wait for the monthly reset |
| 429 organization_usage_limit_exceeded | The usage limit OpenAI assigned was reached | Request a higher approved usage limit |
For rate limits, wait at least as long as the Retry-After header says. If it is missing, use exponential backoff with jitter and cap the number of retries. Building retries into a workflow? Our n8n error handling guide shows the pattern.
Using your key in n8n
In n8n the key lives in a reusable OpenAI credential, not in each node. Per the n8n docs, paste it into API Key and leave Organization ID blank unless you belong to several organizations. The same credential works for the OpenAI, Chat OpenAI and Embeddings OpenAI nodes. Give each client or workflow its own key, ideally in its own project, so you can cap or revoke one without affecting the others.
From there, connecting OpenAI to n8n covers full workflows, ChatGPT API workflows covers calling the API directly, and the free n8n cron expression generator helps you schedule them.
Where to go next
Many projects hold a key from each vendor and send each task to the model that suits it. The Anthropic side is in how to get a Claude API key, and Claude vs ChatGPT and Claude vs GPT compare the two. If you plan to build a product on a model API, AI SaaS Builder, one of our programs, does it on the Claude API with Supabase, Vercel and Stripe. The AI automation library has the rest of our guides.
What changes often
Model IDs, prices and tier names all moved in the weeks before this was written: GPT-6.1 Sol arrived on 29 September 2026 and the usage tiers were cut from five to three on 6 October. Dashboard labels change too. The steps and the security advice are stable. Check the linked page before you rely on any number here.
OpenAI API key: FAQ
Is the OpenAI API free?
Creating a key costs nothing; using models is paid. OpenAI's rate limits guide lists a Free usage tier for users in an allowed geography, and its quickstart calls the first call a free test request, but neither page states a free credit amount. New accounts use prepaid billing with a $5 minimum purchase. If an account does hold free credits, they are used before purchased ones.
Does ChatGPT Plus include API access?
No. OpenAI's plan docs say API token prices are separate from subscription usage. A ChatGPT plan covers the ChatGPT apps and Codex; the API has its own billing, funded with prepaid credits. You need credits on the API platform even if you already pay for ChatGPT.
How much does the OpenAI API cost?
On 13 October 2026 the standard rates per million tokens were $10 input and $50 output on GPT-6 Astra, $2 and $10 on GPT-6.1 Sol, and $0.10 and $0.50 on GPT-6 Luna. Prompts over 272K input tokens cost twice the input rate and 1.5 times the output rate. Batch and Flex processing cost half.
Why is my OpenAI API key not working?
A 401 means the key was rejected: it is mistyped, belongs to another organization, or your IP is outside an allowlist. A 403 means the request came from an unsupported country. A 429 is about limits or money: a rate limit, an empty credit balance, or a spend or usage limit. Read error.code in the response to tell them apart.
What does an insufficient_quota error mean?
It is a billing or limit problem, so retrying will not fix it. OpenAI's error guide says the type can read insufficient_quota while error.code gives the cause: credit_balance_exhausted means add credits, organization_usage_limit_exceeded means request a higher approved usage limit, and the two spend_limit_exceeded codes mean a hard limit you set was reached.
Where should I store my OpenAI API key?
In an environment variable or a secret management service on a server, never in code and never in a browser or mobile app. OpenAI's SDKs read the OPENAI_API_KEY environment variable automatically. Keep any file that holds the key out of version control, and use a separate key for each app and environment.
What should I do if my OpenAI API key leaks?
Revoke it on the API keys page straight away. OpenAI's API reference says revocations take effect within a few seconds. Then create a replacement, update your applications, and review the Usage page for activity you do not recognise. A hard spend limit set in advance caps what a leaked key can cost.