CCyberwiki

Rotating API keys at each provider

To rotation an API key, a new key is created while the old one still works, every place that reads the key is moved to the new value, and the old key is switched off last. The overlap keeps the application running. While it lasts, a leaked old key also still works, so it ends as soon as the old key shows no traffic. Stripe builds the overlap into its Dashboard, where both keys stay valid for up to seven days after a rotation,1 and AWS lets one user hold two active access keys at the same time.2

Screen and button names in Table 1 are as the providers' documentation showed them on 22 November 2026.

Table 1. Where to create and retire keys
ProviderCreate the new keyRetire the old key
StripeAPI keys page, overflow menu, Rotate key, choose an Expiry, Rotate API keyThe chosen expiry; Now deletes the old key at once
OpenAIAPI Keys page, project key with an expiration dateRevoke the old key once the new one is confirmed to work
AnthropicCreate key on platform.claude.com/settings/keysThree-dot menu next to the key, Delete API key
SupabaseSettings > API Keys, new secret keyDelete the secret key, or deactivate the legacy keys
AWSIAM console, Users, the user, Security credentials, or aws iam create-access-keyActions, Deactivate, later Delete; or aws iam update-access-key then aws iam delete-access-key
GitHubSettings, Developer settings, Personal access tokens, Fine-grained tokens, Generate new tokenSame list, Delete next to the token
Google CloudAPI key: Credentials page, the key, Rotate key, CreateDelete the old API key; disable a service account key with gcloud iam service-accounts keys disable

Steps

1. Close the leak before replacing the key

When the rotation follows a leak, the cause comes first: a new key written to the same file, page or log escapes the same way.3 Tracing the leak is part of Responding to a leaked API key, and Secret scanning finds keys committed to a repository. If the leaked key is being used by someone else right now, retire it before anything else and accept that the parts of the application still using it stop working until they get the new one.3

2. Create the new key with no more access than the old one

Narrowing a key's access costs little during a rotation, because every place that uses it is being changed anyway (Over-privileged access token, API key restrictions). On Google Cloud, Rotate key copies the old key's restrictions onto the new one, and the operation exists only in the console.4 On GitHub, a fine-grained token is limited to chosen repositories and permissions, while a classic token reaches every repository its owner can reach.5

Copy the value into a secrets store straight away. AWS shows a secret access key only at creation,2 and a live-mode Stripe key that you create cannot be revealed again after the first view.1 Supabase's Management API call that creates a key prints its value, so its documentation runs it in a local terminal and not in CI.3

3. Deploy the new value everywhere the key is read

On Vercel, a changed environment variable takes effect only in a new deployment. Edit the variable, check that the right environments are selected (Production, Preview, Development), click Save, then Redeploy the latest production deployment, and redeploy preview deployments too if they use the key.6 Copies kept as CI secrets or in scheduled jobs need the new value as well (Secrets management).

Supabase has two places that name the key. An Edge Function (server code that Supabase runs) written with withSupabase({ auth: 'secret' }) accepts only the key named default and must be changed to name the new key, for example auth: 'secret:rotated_secret_key'. A Database Webhook or pg_net call (an HTTP request sent from the database) that sent the legacy service_role key as a bearer token should send the new secret key in the apikey header.3

4. Wait until the old key receives no traffic

The provider's records show whether a forgotten server still sends the old key. AWS lists Last used for every access key and suggests waiting several days before going on.2 Stripe advises expiring the old key only after its request logs have shown zero requests for a few hours or days.1

5. Disable the old key, then delete it

Disabling first gives a way back. On AWS, a deactivated key can be turned on again if an application breaks, and deleted once nothing has failed for a while.2 A way back also lets a leaked key return. A deleted Google Cloud API key can be restored for 30 days,4 and a disabled service account key can be enabled at any time; disabling it also leaves short-lived credentials already issued from it valid.7 On Supabase, deleting a secret key is final, and re-enabling the legacy keys after deactivating them brings the leaked key back to life.3

Deleting a GitHub token that was used to create a deploy key deletes the deploy key too.5 An Anthropic key is deleted from its three-dot menu on the API keys page,8 and OpenAI's guidance is to revoke the old key once the replacement is confirmed to work.9

6. Retire what still trusts the old key

Some keys are signed by a secret, and the signature stays valid as long as the secret does. Supabase's legacy anon and service_role keys are JWTs signed by the project's legacy JWT secret, so after a service_role leak that secret must be revoked as well, and browser clients move from the legacy anon key to a publishable key.3 Stripe keeps webhook signing secrets apart from API keys, one per webhook endpoint;1 rotate them on their own if they leaked too (Webhook signature verification).

7. Give the new key an end date

An expiry date forces the next rotation. OpenAI project keys accept an expiration date, and administrators can cap key lifetime for the organisation or a project.9 GitHub tokens take an expiration at creation, organisations can set a maximum lifetime, and tokens unused for a year are removed.5 Anthropic gives 90 days as an example rotation interval and separate keys per environment, so one leaked key can be switched off without stopping the others.10 OpenAI also supports workload identity federation, in which a workload exchanges an identity from a trusted provider for a short-lived token in place of a stored key.9

Prompt for a coding agent

I am rotating the API key stored in the environment variable NAME_OF_VARIABLE. List every place in this repository that reads it or passes it on: application code, environment files, CI workflow secrets, deployment configuration, scheduled jobs, database functions, Supabase Edge Functions and webhook calls. For each place, give the file and line and say whether the value is read at build time or at run time. Do not change any values and do not print any secret.

In SecFlash

A key that others can read is a reason to rotate it. SecFlash reports a key in the code or its git history as SF-SEC-702 and a workflow that can leak a credential as SF-SEC-805 on the Security page. On the Exposure page it reports a secret key in public JavaScript (SF-EXP-301), a token in public JavaScript (SF-EXP-302), a secret key written into the page (SF-EXP-304) and a readable environment file (SF-EXP-307).

See also

References

  1. a b c d Stripe (n.d.). "API keys". Stripe Docs.
  2. a b c d "Update access keys". AWS Identity and Access Management User Guide. Amazon Web Services.
  3. a b c d e f Supabase (updated 19 November 2026). "Rotate a leaked API key". Supabase Docs.
  4. a b Google Cloud. "Manage API keys". Authentication documentation.
  5. a b c GitHub (2026). "Managing your personal access tokens". GitHub Docs.
  6. back Vercel. "Rotating environment variables". Vercel Docs.
  7. back Google Cloud (updated 17 November 2026). "Disable and enable service account keys". Identity and Access Management documentation.
  8. back Anthropic (n.d., read 22 November 2026). "What should I do if I suspect my API key has been compromised?". Claude Help Center.
  9. a b c "Best practices for API key safety". OpenAI Help Center. OpenAI.
  10. back Anthropic (n.d., read 22 November 2026). "API key best practices: keeping your keys safe and secure". Claude Help Center.