AI and model APIs

OpenAI API keys: find, store, rotate, and use them safely with AI agents

OpenAI API keys are created on the API keys page of the OpenAI dashboard (platform.openai.com/settings/organization/api-keys) and sent as Authorization: Bearer <key>. OpenAI recommends a user-owned project key for local development and a project service account key for deployed apps and CI. The SDKs read the key from OPENAI_API_KEY, and OpenAI API keys belong on the server, never in browser or app code.

OpenAI key types

TypePrefixSecret?ScopeExpiry
User-owned project key
For your own local development. Requests use your access to the project.
—YesOne project, limited to what its owner can do there; permissions can be set to All, Restricted or Read OnlyAn expiration date can be set at creation; administrators can enforce a maximum lifetime
Service account key
For deployed applications, scheduled jobs and CI. Creating one needs service-account management permission, which organization and project owners have. Permissions are changed after creation, in the project's API keys settings.
—YesOne project, acting as a project service account rather than a person; reads and writes all API resources in the project by defaultAn expiration date can be set at creation; administrators can enforce a maximum lifetime
Admin API key
Managed on the separate Admin keys page (platform.openai.com/settings/organization/admin-keys). Use a standard key for model requests.
—YesAdministration endpoints only, such as users, invites, projects, API keys and audit logs—

How to create a OpenAI key

  1. Sign in to the OpenAI dashboard and select the project the key is for.
  2. Open the API keys page.
  3. Click + Create new secret key, name the key and choose its permissions (All, Restricted or Read Only).
  4. Set an expiration date; OpenAI strongly recommends one for project API keys.
  5. Copy the key immediately. OpenAI shows the full key value only once.
  6. For a deployed app, open the project's Members page and click + Service account instead; this creates the service account and its key together.

Open OpenAI's key settings

Limiting what the key can do

OpenAI API keys are scoped to one project. On a user-owned key, choose All (the default), Restricted (per-resource None, Read or Write) or Read Only when creating or editing the key. Key scopes can narrow access but never grant more than the key's owner has. Administrators can also require service-account keys only, allow user-owned keys only, or turn off new key creation at the organization or project level, and can enforce an IP allowlist.

How to rotate a OpenAI key

  1. Create a replacement key in the same project, with an expiration date.
  2. Update every application and secret store that reads the old key, usually OPENAI_API_KEY.
  3. Verify that requests succeed with the replacement key.
  4. Revoke the old key on the API keys page.

OpenAI API keys have no built-in overlap window: the old and new keys both work until you revoke the old one. A revoked key stops working within a few seconds. Organization and project key policies, such as a maximum lifetime, apply only to new keys.

How to revoke a OpenAI key

  1. Open the API keys page, or the Security settings page, which lists all of your keys.
  2. Revoke the key. OpenAI says revoked keys stop working within a few seconds.
  3. For automation, the Admin API can list, retrieve, update and revoke project API keys, including service account keys.

If your OpenAI key leaked

  1. Revoke the exposed OpenAI API key on the API keys page or the Security settings page.
  2. Create a replacement key, preferably a service account key for deployed apps, and deploy it.
  3. Check the Usage page for activity you do not recognise. Keys created after December 20, 2023 have usage tracking on by default.
  4. Review the organization's audit logs (available through the Admin API) for keys or members you did not create.
  5. Update every service, CI secret and teammate that used the old key.
  6. Contact OpenAI Support if suspicious activity continues. OpenAI is a GitHub secret scanning partner, and OpenAI says it disables API keys it detects on the public internet.

Environment variables and CLI

OpenAI's SDKs and docs read the key from OPENAI_API_KEY.

Things that catch people out

  • OpenAI shows the full API key value only once. A lost key cannot be recovered, only replaced.
  • Admin API keys are for administration endpoints only; use a standard project key or service account key for model requests.
  • Keys created before December 20, 2023 do not track usage by default, and their usage appears as Untracked until tracking is turned on.
  • Legacy user keys and members of several organizations need the OpenAI-Organization and OpenAI-Project headers to choose where usage is billed.

Common OpenAI authentication errors

Incorrect API key provided

HTTP 401. The OpenAI API key in the request is not correct, often because it was copied incompletely, revoked or expired.

Invalid Authentication

HTTP 401. The request's authentication is invalid, for example a wrong key or the wrong organization.

IP not authorized

HTTP 401. The request IP does not match the IP allowlist configured for the project or organization.

Recent incidents that put OpenAI keys at risk

If you were affected by one of these, rotate your OpenAI keys using the rotation steps above.

Using your OpenAI key with AI agents

A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.

BotPassword works this way. For a OpenAI key, these intents fit:

IntentWhat BotPassword does
run_withRuns a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
http_requestMakes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.
store_secretSaves a value the agent already holds as a new vault item.
capture_secretPlanned, not built yet: will read a newly created key straight from the page or command output into the vault.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

Where do I find my OpenAI API key?

You cannot view an existing OpenAI API key again. Create a new one on the API keys page of the OpenAI dashboard and copy it when it is created.

What is the OpenAI API key environment variable?

OpenAI's SDKs read the key from OPENAI_API_KEY, and the Python SDK uses it by default when no key is passed.

Should I use a user API key or a service account key?

Use a user-owned project key for your own local development and a project service account key for deployed apps, scheduled jobs and CI, so the app does not depend on one developer's key.

How do I restrict an OpenAI API key?

Set the key's permissions to Restricted or Read Only when you create or edit it, and keep each key in a separate project. Administrators can add an IP allowlist and a maximum key lifetime.

How do I rotate an OpenAI API key without downtime?

Create the new key, deploy it, confirm requests work, then revoke the old key. Both keys work until the old one is revoked.

What happens if my OpenAI API key leaks on GitHub?

OpenAI is a GitHub secret scanning partner and says it disables keys it detects on the public internet. Revoke the key yourself as well, create a replacement and check the Usage page.

Can an AI agent use my OpenAI key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a .env file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with run_with, http_request, store_secret, so the agent gets the result and never the value.

Stop pasting OpenAI keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.