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

Source: https://botpassword.com/keys/openai
Last checked: 2026-11-08

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.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| User-owned project key | — | Yes | One project, limited to what its owner can do there; permissions can be set to All, Restricted or Read Only | An expiration date can be set at creation; administrators can enforce a maximum lifetime |
| Service account key | — | Yes | One project, acting as a project service account rather than a person; reads and writes all API resources in the project by default | An expiration date can be set at creation; administrators can enforce a maximum lifetime |
| Admin API key | — | Yes | Administration 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](https://platform.openai.com/settings/organization/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.

## 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.

## How to revoke a OpenAI key

1. Open the [API keys](https://platform.openai.com/settings/organization/api-keys) page, or the [Security settings](https://platform.openai.com/settings/profile/security) 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 the 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

- `OPENAI_API_KEY`

## Gotchas

- 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.

## Using this key with AI agents (BotPassword)

- `run_with`: Runs 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_request`: Makes 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_secret`: Saves a value the agent already holds as a new vault item.
- `capture_secret`: Planned, not built yet: will read a newly created key straight from the page or command output into the vault.

## FAQ

### 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](https://platform.openai.com/settings/organization/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.

## Sources

- https://developers.openai.com/api/reference/overview
- https://developers.openai.com/api/docs/quickstart
- https://developers.openai.com/api/docs/guides/api-keys
- https://developers.openai.com/api/docs/guides/production-best-practices
- https://developers.openai.com/api/docs/guides/safety-best-practices
- https://developers.openai.com/api/docs/guides/error-codes
- https://help.openai.com/en/articles/8867743-assign-api-key-permissions
- https://help.openai.com/en/articles/9186755-managing-projects-in-the-api-platform
- https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety
- https://help.openai.com/en/articles/8304786-how-can-i-keep-my-openai-accounts-secure
- https://github.com/openai/openai-python
- https://docs.github.com/en/code-security/secret-scanning/introduction/supported-secret-scanning-patterns
