OpenAI API keys

How to get an OpenAI API key

To get an OpenAI API key, sign in to the OpenAI API platform, go to organization settings, select your project, open API Keys and click Create new secret key. Copy it right away: OpenAI shows the full secret only once, when you create it.

New API accounts are prepaid: the minimum credit purchase is $5, credits expire after 1 year, and every key in the organization draws on that one balance. Before the key goes into an app, give its project a monthly spend limit with Enforce a hard limit turned on.

Create an OpenAI API key, step by step

  1. Sign in to the OpenAI API platform.
  2. Go to organization settings and select the project the key is for. Every organization has a Default project, which can't be deleted. A separate project per app makes limits easier later.
  3. Open API Keys and select Create new secret key.
  4. Pick the permissions: All, Restricted or Read Only. More on those below.
  5. Copy the key and put it in a secret manager or an environment variable. You can't view it again.

Then export it in your shell. Each OpenAI SDK reads the key from the environment, so it never has to sit in your code:

export OPENAI_API_KEY="your_api_key_here"

Can't create a project? That takes the organization owner role. Owners can also create and view the keys in every project, so ask one if a project or its key page is missing for you.

Which OpenAI key type you need

OpenAI has three kinds of secret keys, and they are not interchangeable.

Key typeBelongs toUse it forCalls models
User-owned project keyYou, with your access in one projectYour own development workYes
Service account keyA service account in one projectAn app or automation with its own permissions and lifecycleYes
Admin API keyThe organizationOrganization management through the Admin APIsNo, administration endpoints only

A user-owned key acts with its owner's access. Don't share one developer's key across a team or make it the identity of a production app. OpenAI doesn't support sharing keys at all: invite teammates from the Members page and each gets their own key.

All, Restricted or Read Only

User-owned keys get All (full permissions, the default), Restricted (you choose None, Read or Write per resource or endpoint) or Read Only (read on every endpoint). A service account key starts with read and write on everything in its project. You change that afterwards in the project's API Keys settings.

Scopes only narrow a key. They never give it more than the user or service account behind it already has. Project keys can also get an expiration date, after which every request with them is rejected, and admins can set a maximum key lifetime for the organization or a project.

Where your key lives, and why you can't see it again

Keys live on the API key page of their project. OpenAI shows the full secret only when you create it. If you lost it or never saved it, create a new key and point your app at the new one.

To delete a key, click the trash can icon next to it on the API key page. Deleted keys can't be used, so any app still using one stops working.

Getting 401 Incorrect API key provided? Check for a typo or an extra space, a key from a different organization or project, or a key that was deleted or deactivated. An old, revoked key can also be cached locally. If in doubt, generate a new key.

Billing and the free tier

New API accounts use prepaid billing: you buy credits before you call the API. The minimum purchase is $5 and the default is $10. Purchased credits expire after 1 year. If your account has free credits, OpenAI spends those first.

Auto-reload is turned on by default during setup. Turn it off if you don't want automatic credit purchases. With it on, an empty balance no longer stops a runaway key.

When credits run out, requests fail with the error code credit_balance_exhausted. OpenAI warns not to treat the prepaid balance as an instant spending cutoff. The quota errors are explained in What "You exceeded your current quota" means.

Your usage tier adds a monthly usage limit on top. The Free tier, for users in an allowed geography, caps usage at $100 a month. The Build tier starts at $5 in total credit purchases and allows $500 a month. Model prices are in OpenAI API pricing.

Cap what the key can spend

OpenAI's monthly spend limits sit on the organization and on each project. So the cleanest cap for a key is a project of its own: one app, one project, one key, one limit.

A spend limit alone only monitors. Turn on Enforce a hard limit and requests fail once tracked spending reaches it. Enforcement isn't instantaneous, so recorded spending can slightly exceed the limit. A new project limit also creates an alert at 100% by default.

The full walkthrough, with the errors you get at the limit, is in How to set an OpenAI API spending limit. For email alerts before the limit, see OpenAI billing alerts.

For server-side apps, IP allowlisting goes one step further: only requests from the IP addresses or ranges you configure get through, and all others are rejected.

Keep the key out of code, git and browsers

  • Store it in a secret manager and give it to the workload on the server.
  • Keep it out of browser code, mobile apps, source control, logs and customer-facing responses.
  • Rotate on a schedule: issue a replacement, update the workload, check that it works, then revoke the old key.
  • Give production its own service account key, not a developer's personal key.

If you think a key leaked, rotate it right away from the API Keys page, then check usage for spend you don't recognize. What to do in the first hour after a key leaks covers the rest.

Watch what the key spends

OpenAI's alerts fire at the thresholds you set. Costbrake reads OpenAI's Usage and Costs APIs and also alerts on a spike: an hour that costs more than 3x your usual hour and at least $5 more.

It needs a read-only Admin key, not the project key your app uses, and it turns down project and user keys. Organization owners manage Admin keys on the Admin keys page in organization settings. On Pro and Team, Costbrake checks every 5 minutes, and every 2 minutes during a spike. Free checks hourly. How the API works is in the OpenAI Usage API guide, and setup steps are on the OpenAI integration page.

Pro and Team can also add a brake. It sets the project's or the organization's monthly hard spend limit to $0.01, so once OpenAI enforces it, paid requests fail with a 429 project_spend_limit_exceeded or organization_spend_limit_exceeded. The brake needs a second Admin key with All permissions. It never fires on its own: an admin clicks it and confirms, and undo restores the previous limit. Plans are on the pricing page, and new workspaces get a 14-day Pro trial with no card.

Questions and answers

Is an OpenAI API key free?

Creating a key is free, but calls are billed. New API accounts are prepaid: you buy credits first, with a $5 minimum purchase, and calls fail with credit_balance_exhausted when the credits run out. Free credits, if your account has any, are spent before purchased credits.

Where can I find my OpenAI API key again?

You can't see an existing key's full secret again, because OpenAI only shows it when you create it. Create a new key on your project's API key page, update your app to use it, then delete the old key with the trash can icon.

Why is my OpenAI API key not working?

A 401 Incorrect API key provided error means a typo or extra space, a key from another organization or project, or a deleted or deactivated key. An old revoked key can also be cached locally. A 429 with credit_balance_exhausted means the organization is out of prepaid credits.

How do I limit what an OpenAI API key can spend?

Give the key its own project and set a monthly spend limit on that project with Enforce a hard limit turned on. Requests fail once tracked spending reaches the limit. Enforcement isn't instant, so spend can slightly exceed it.

What should I do if my OpenAI API key leaks?

Rotate it right away from the API Keys page: create a replacement, update your app, then delete the leaked key. Check usage for spend you don't recognize, and keep a hard spend limit on the project so a future leak hits a ceiling.

Sources

Updated