On this page
- Why rotations cause downtime
- What you need to know about OpenAI keys first
- Step 1: Map every place the key lives
- Step 2: Create the replacement key
- Step 3: Roll it out everywhere, old key still alive
- Step 4: Verify the new key works
- Step 5: Watch the old key go quiet
- Step 6: Revoke the old key
- The whole procedure at a glance
- Rotating the admin key itself
- Automating it
- FAQ
Most OpenAI key rotations that cause an outage go wrong the same way: someone deletes the old key, pastes the new one into one place, and finds out about the other three places from the error alerts. The fix isn't speed. It's order.
OpenAI's own guidance for rotating a key is four steps: issue a replacement key, update the workload, verify it works, then revoke the previous key (OpenAI: API keys and service accounts). This guide turns those four steps into a procedure you can run on a production app without a single failed request, first by hand and then with the Admin API.
If you're here because a key just leaked, stop reading this and go to what to do in the first 10 minutes after you leak an API key. A leak changes the order: you revoke first and accept some downtime.
Why rotations cause downtime
A key rotation has a window in which some of your system knows the new key and some still uses the old one. Downtime happens when the old key dies before that window closes. The usual culprits:
- Revoking first. Deleting the old key, then creating a new one, guarantees an outage the length of your deploy.
- Forgotten consumers. The production server got the new key; the cron worker, the GitHub Actions secret, the staging app and a teammate's
.envdid not. - Values read once at boot. Most apps read
OPENAI_API_KEYwhen the process starts and build one client from it. Changing the variable does nothing until the process restarts. - Platforms that bake variables into deployments. On Vercel, for example, changes to environment variables apply only to new deployments, never to the one already running (Vercel docs). You have to redeploy.
The cure for all four is the same: an overlap rotation, where both keys work at the same time, and the old one is revoked only after you can prove nothing uses it.
What you need to know about OpenAI keys first
Three facts from OpenAI's documentation shape the procedure.
There are three kinds of key. User-owned project keys are for your own local development and act with your access. Service account keys belong to a project's service account and suit deployed applications, scheduled jobs and CI. Admin API keys manage the organization itself through the Admin API (OpenAI). Production traffic should run on a service account key, so a person leaving the company never takes your app down with them.
The full value is shown once. OpenAI returns the complete key only when it is created; afterwards you only ever see a redacted value, so a lost key can't be recovered (OpenAI Terraform guide). Put it in your secret manager the moment you create it.
Every key reports when it was last used. The project API key object includes last_used_at, alongside created_at, the owner and an optional expires_at (API reference). That one field is what lets you revoke with confidence instead of hope.
Rotation also doesn't touch your limits: OpenAI defines rate limits at the organization and project level, not per user or key (rate limits guide). A new key in the same project behaves exactly like the old one.
Step 1: Map every place the key lives
Before you create anything, list every consumer of the key. Ten minutes here saves the outage. Check:
- Your hosting platform's environment variables, per environment (production, preview, staging).
- CI secrets: GitHub Actions, GitLab CI variables, your build system.
- Secret stores and orchestrators: AWS Secrets Manager, Vault, Kubernetes Secrets, Doppler.
- Background workers, cron jobs, notebooks and internal tools that call OpenAI.
- Local
.envfiles of anyone who runs the app against production.
A search of your repositories for OPENAI_API_KEY finds the code paths; the dashboards above find the values. If the key is used in two projects, it should be two keys. Rotation is a good moment to split them.
Step 2: Create the replacement key
Create the new key in the same OpenAI project as the old one, so it inherits the same models, limits and billing. In the dashboard, open the project's API keys and create a new secret key owned by a service account. Give it a name that says what and when, such as prod-api-2026-11, so the old and new keys are easy to tell apart later.
To script it, use the Admin API with an admin key. The examples below share one base URL:
export ORG_API=https://api.openai.com/v1/organization
This call adds a key to an existing service account and returns its value once:
curl -s $ORG_API/projects/$PROJECT_ID/service_accounts/$SERVICE_ACCOUNT_ID/api_keys \
-H "Authorization: Bearer $OPENAI_ADMIN_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "prod-api-2026-11"}'
The response contains the key's id and its value. Pipe the value straight into your secret store; don't print it to a terminal you share, or to CI logs.
Step 3: Roll it out everywhere, old key still alive
Update every consumer from your list in step 1. The old key keeps working throughout, so there is no rush and no ordering problem between services.
- Change the value in each secret store and platform.
- Redeploy or restart anything that reads the key at startup. A changed variable that no process has reloaded is the most common "the rotation worked, but production still uses the old key" story.
- If you can, roll out to a small slice first (one region, one worker pool) and watch error rates before going wide.
For long-running services, the cleanest design is to read the key from your secret store on a short cache (say, five minutes) rather than once at boot. Then a rotation needs no restart at all.
Step 4: Verify the new key works
Don't infer it; test it. A read-only call to the models endpoint is enough to prove the key authenticates:
curl -s -o /dev/null -w "%{http_code}\n" https://api.openai.com/v1/models \
-H "Authorization: Bearer $NEW_OPENAI_API_KEY"
200 means the key is valid. A 401 means it is wrong, revoked or from another organization or project (error codes). Then check your application itself: a health check or smoke test that makes a real model call through your normal code path.
Step 5: Watch the old key go quiet
Now prove nothing still uses the old key. List the project's keys and look at last_used_at:
curl -s "$ORG_API/projects/$PROJECT_ID/api_keys?limit=100" \
-H "Authorization: Bearer $OPENAI_ADMIN_KEY" \
| jq -r '.data[] | [.name, .id, (.last_used_at // 0 | todate)] | @tsv'
If the old key's last use keeps advancing after your rollout, something you missed is still calling with it. Find it before you go on. How long to wait depends on your traffic: for an API serving requests every second, fifteen minutes of silence is convincing; for a nightly job, wait until it has run once on the new key.
Step 6: Revoke the old key
Only now delete the old key, in the dashboard or with the Admin API:
curl -s -X DELETE $ORG_API/projects/$PROJECT_ID/api_keys/$OLD_KEY_ID \
-H "Authorization: Bearer $OPENAI_ADMIN_KEY"
The response confirms the id and deleted: true. Any caller you missed now gets a 401, so keep an eye on error rates for the next hour, and remove the old value from every secret store so nobody "fixes" an error by pasting it back.
The whole procedure at a glance
| Step | What you do | How you know it's done |
|---|---|---|
| Map | List every consumer of the key | Every environment and job is on the list |
| Create | New service-account key in the same project | Value stored in your secret manager |
| Roll out | Update and redeploy every consumer | All processes restarted or reloaded |
| Verify | GET /v1/models and an app smoke test | 200, and real calls succeed |
| Drain | Watch the old key's last_used_at | No use for a full traffic cycle |
| Revoke | Delete the old key | Error rates stay flat |
Rotating the admin key itself
The admin key is the most powerful credential in this process, so rotate it too, with the same overlap. The Admin API can create (POST /v1/organization/admin_api_keys), list and delete (DELETE /v1/organization/admin_api_keys/{key_id}) admin keys (API reference). Create the new one, switch your automation to it, confirm a call works, then delete the old one. Keep admin keys out of application servers entirely: they belong only in the system that provisions keys.
Automating it
Done by hand, this procedure takes half an hour and depends on someone remembering step 5. That's why most teams rotate far less often than their policy says. OWASP's secrets management guidance recommends automating rotation, because manual handling raises the risk of leaks and mistakes (OWASP).
This is the job we built Keysquid for. For OpenAI it holds an admin key and uses it only to create and delete keys for a dedicated project service account. Each rotation creates the new key, verifies it against /v1/models, pushes it to the places you connected (Vercel, Railway, GitHub Actions, Kubernetes, AWS Secrets Manager and others) with a redeploy where the platform needs one, and rolls those platforms back if a push fails. The previous key is revoked only after a grace period, 10 minutes by default. The admin key and the new values are encrypted to an open-source agent you run, so Keysquid's servers never see them; the security model explains how.
Whatever you use, the rule doesn't change: the new key must be live everywhere before the old one dies.
Frequently asked questions
Can two OpenAI API keys be active at the same time?
Yes. A project can hold several keys at once, and each works until it is deleted or expires. That overlap is what makes a zero-downtime rotation possible: the new key is live before the old one is revoked.
Does rotating an OpenAI API key reset my rate limits or usage?
No. OpenAI defines rate limits at the organization and project level, not per key, so a new key in the same project shares the same limits and the same usage history.
What happens to requests that still use the old key after I revoke it?
They fail with an HTTP 401 authentication error. That is why you revoke last, after the old key's last-used time has stopped moving.
How often should I rotate OpenAI API keys?
Rotate immediately after a suspected leak or when someone with access leaves, and on a schedule otherwise. Every 30 to 90 days is a common policy; if rotation is automated, shorter is cheap.
Should production use a user key or a service account key?
A service account key. OpenAI's guidance is that user-owned keys are for your own local development and act with your access, while service account keys belong to the project and suit deployed apps, scheduled jobs and CI.
Sources
- developers.openai.com/api/docs/guides/api-keys
- developers.openai.com/api/reference/resources/organization/subresources/projects/subresources/api_keys
- developers.openai.com/api/docs/guides/terraform/service-accounts
- developers.openai.com/api/reference/resources/organization/subresources/admin_api_keys
- developers.openai.com/api/docs/guides/rate-limits
- developers.openai.com/api/docs/guides/error-codes
- vercel.com/docs/environment-variables
- cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html