Rotate a leaked API key without taking the app down
Last verified:
The fix
Create the new key, point the host at it, redeploy, and prove a request works. Then revoke the old key. Revoking first is how production starts failing.
- 1
Create a new key and leave the old one working
OpenAI: create a key on the API keys page. Do not revoke yet. Stripe: create a new secret key, or rotate with an expiration so both work for a while. Do not choose expire immediately. Supabase: create a new secret key. Do not touch the JWT secret. Anthropic: create a key in the Console. Copy the new value once, into a password manager or the host, not into the chat and not into git.
- 2
Close the hole, then update the host
If the key was in frontend code, move the call to a server route and keep the secret in an unprefixed env var. If a Supabase table was readable with the anon key, enable RLS and add policies before you depend on a new key. Then set the host variable to the new value for the environment that is actually serving (Production, and Preview if previews call the API). Do not commit
.env.local. - 3
Redeploy and verify
The running deployment keeps the old value until a new one is built. Redeploy.
NEXT_PUBLIC_andVITE_values are baked in at build time, so a redeploy that reuses the old build can keep them. Send a real request that only succeeds with the new key. On your machine, restart the dev server so it is not still holding the old export. - 4
Revoke the old key after that request works
OpenAI: revoke it from the API key list in Security settings. Stripe: expire the old secret once the new deployment is the one serving. Supabase: delete the leaked secret key only after every server component uses the new one, or deactivate a legacy
service_rolekey if you still need a way back. Anthropic: Disable the leaked key, confirm the app, then Delete. If production fails here, the live process is still on the old value. Put a working key on a new deployment before you revoke again. Stripe's grace period is only so you can migrate. Once the new deployment is serving and the old key's request log is quiet, expire the leaked secret. Do not leave it for the whole window if the log shows calls you did not make. - 5
Check usage, then set a cap
OpenAI: the Usage page shows traffic for the key. A hard spend limit is under Organization limits or Project settings, Limits. Turn on Enforce a hard limit. When it trips, requests return 429 with
organization_spend_limit_exceededorproject_spend_limit_exceeded. Anthropic: set the limit on the Billing page, under the tier's cap. Hitting a limit you set returns 400invalid_request_error, and the message starts withYou have reached your specified API usage limits. Stripe: read the old key's request logs for calls you did not make, then expire it. Supabase has no model bill on this key. A leaked secret key is data access. RLS is what limits the anon key. A spend cap does not replace that.
Why it happens
The old key keeps working until the provider revokes it. Saving a new value in a dashboard does not turn the old one off, and the deployment that is already live keeps using whatever it was given. OpenAI's production guide is the order that avoids the outage: create a replacement, update the app, and revoke the old key only after you have verified the replacement. OpenAI revocations take effect within a few seconds. Revoke first and the live app starts returning 401 while it still holds the old value.
Stripe can rotate a secret or restricted key with a grace period of up to 7 days, during which both keys work. Expiring immediately deletes the old key now. You can also create a second key, switch over, then expire the old one. Publishable keys (pk_) cannot be expired. They are meant for the browser. The secret (sk_) is the one you rotate. Supabase: create a new secret key under Settings, API Keys. The old secret key and a legacy service_role key keep working until you retire them. Deleting a secret key cannot be undone. Deactivating a legacy key can be turned back on. Do not regenerate the project JWT secret to do this. That invalidates the current API secrets immediately. The anon key and the publishable key are public by design. Rotating them does not close a table with Row Level Security off.
Anthropic: create the key in the Console. Disable is reversible. Delete is not, and an expired key cannot be turned back on. Do that after the app is sending the new key. If the leaked value was in client code, a NEXT_PUBLIC_ or VITE_ variable, or a git commit, fix that path first. Otherwise the new key leaks the same way.
The next error you'll probably hit
Once this is fixed, the usual follow-up is NEXT_PUBLIC_ secret in the bundle. A NEXT_PUBLIC_ or VITE_ secret is in the browser bundle. Rotate the key, move the call to a server route, and keep real public keys in the client.
Prompt to paste back into your AI tool
Paste this above the error.
A secret API key was exposed (client bundle, git, logs, or a host that showed env vars). The provider is <OpenAI, Anthropic, Stripe, or Supabase>. The env var name is <NAME>. Do not revoke the old key before the new deployment is verified. Do not regenerate the Supabase JWT secret. Do not put the new secret in NEXT_PUBLIC_ or VITE_. Do not commit .env or .env.local. Do not print the key. 1. Tell me to create a new key and leave the old one active. 2. If the leak was client code or a public Supabase table, close that hole first (server route, or RLS plus policies). 3. Tell me to set the host env var, redeploy, and hit one real request that only works with the new key. 4. Then tell me the provider's revoke step (OpenAI revoke, Stripe expire, Supabase delete or deactivate, Anthropic disable then delete). 5. Tell me where to check usage and set a spend limit if that provider has one. Do not rewrite unrelated files.
Common questions
+I revoked the key first and production broke. What now?
The live deployment was still using the old value, and OpenAI revocations apply within seconds. Create another key, set it on the host, redeploy, confirm a request, and only then revoke anything left over.
+Should I rotate the Supabase anon key?
The anon key and the publishable key are supposed to be in the browser. Rotating them does not hide a table with RLS off. Rotate a leaked service role or secret key, and enable RLS on the tables the advisor flagged.
+Stripe says the old key works for up to 7 days. Should I wait?
That grace period is so you can migrate without downtime. Once the new deployment is verified, expire the leaked secret. Use the request log to see whether anything is still calling the old key before you do.
Related errors
- NEXT_PUBLIC_ secret in the bundleA NEXT_PUBLIC_ or VITE_ secret is in the browser bundle. Rotate the key, move the call to a server route, and keep real public keys in the client.CursorLovableBoltNext.jsViteNov 11, 2026
- GH013: Repository rule violations foundGitHub blocked the push because a commit still contains a secret. Push protection says Push cannot contain secrets. Rotate the key, then remove it from those commits.CursorLovableBoltOpenAIStripeOct 22, 2026
- 401 Incorrect API key providedOpenAI rejected the key it received. Right after a rotation, the app is usually still sending the old value, a value with extra characters, or a key from the wrong project. Anthropic's matching failure is invalid x-api-key.CursorLovableBoltOpenAINext.jsOct 22, 2026
- Changed a Vercel env var but the live app still uses the old valueSaving an environment variable does not change the deployment that is already live. Redeploy. NEXT_PUBLIC_ and VITE_ values are copied into the bundle at build time.CursorLovableBoltVercelNext.jsOct 22, 2026
- RLS Disabled in Public: Table public.x is public, but RLS has not been enabledRLS Disabled in Public. Table public.x is public, but RLS has not been enabled. Enable Row Level Security and add a policy in the same change. The anon key can read that table until you do.CursorLovableBoltSupabasePostgresNov 3, 2026