Guides · Updated 2026-10-30
How to rotate Supabase API keys without downtime
Short answer: Never delete or disable the old key first. With Supabase's publishable and secret keys, old and new keys work at the same time, so the safe order is: create a new key, put it everywhere the old one is used (host environment variables, Edge Functions, cron jobs, webhooks), redeploy so the new value is actually live, confirm nothing still uses the old key, and only then delete it (secret keys) or deactivate it (legacy anon and service_role keys). Most downtime comes from doing the last step first, or from forgetting that changed environment variables only apply to new deployments.
Why rotating keys takes apps down
The usual story: a key leaks (in a public repo, in the browser's JavaScript, in a screenshot), you panic, delete or regenerate it in the Supabase dashboard, and the live site stops loading data. Nothing was wrong with the idea, only with the order. Your app was still sending the old key, and the new one wasn't deployed anywhere yet.
Supabase's own advice for a leaked key starts with "Don't rush": fix the root cause of the leak first (remove the key from the code or the frontend bundle), then rotate (https://supabase.com/docs/guides/api/api-keys). Rotating while the key is still being shipped to the browser just leaks the new one. One exception from Supabase's rotation guide (https://supabase.com/docs/guides/security/rotate-leaked-api-keys): if someone is actively using the leaked key, retire it right away, even though parts of your app that still use it stop working until you update them.
First: which key are you rotating?
- Secret key (sb_secret_...) or legacy service_role key (a long JWT starting with eyJ): this one bypasses Row Level Security and gives full access to your data. If it leaked, rotate it.
- Publishable key (sb_publishable_...) or legacy anon key: this one is meant to be public. Supabase calls the publishable key "safe to expose online". Finding it in your app's JavaScript is normal; what protects your data is RLS. Rotating it doesn't fix open tables.
- Keys from other services (OpenAI, Stripe, Resend...) follow their own rotation steps: see https://leakyvibe.com/guides/leaked-api-key-what-to-do
If your project still runs only on the legacy anon and service_role keys, note that Supabase is deprecating them by the end of 2026, and its troubleshooting guide says "it is no longer possible to rotate the legacy anon, service and JWT secrets" on their own; it recommends moving to the new keys instead (https://supabase.com/docs/guides/troubleshooting/rotating-anon-service-and-jwt-secrets-1Jq6yd). Don't regenerate the legacy JWT secret as a quick fix: Supabase warns that doing so immediately invalidates all current API keys and severs every connection using them until you deploy new ones. That's the classic way to take a site down.
The zero-downtime order (secret or service_role key)
This follows Supabase's official procedure, "Rotate a leaked or compromised key" (https://supabase.com/docs/guides/security/rotate-leaked-api-keys), with the deploy details that usually bite vibe-coded apps:
- Fix the leak. Remove the key from frontend code, public repos and any NEXT_PUBLIC_ or VITE_ variable. A secret key must never be in a browser or a shipped app.
- Create a new secret key in the Supabase dashboard under Settings > API Keys. If your project has no publishable/secret keys yet, creating them is safe: Supabase says it adds them alongside your existing anon and service_role keys, and the legacy keys keep working.
- Replace the old key with the new one everywhere it's used: your host's environment variables (Vercel, Netlify, Railway...), Edge Functions, cron jobs and workers, CI/CD scripts, third-party integrations, and Database Webhooks or pg_net calls. If the leaked key is the legacy service_role key, also swap the legacy anon key for a publishable key in your web and mobile clients: Supabase says retiring the compromised key deactivates both legacy keys.
- Redeploy. On Vercel, changes to environment variables "only apply to new deployments" (https://vercel.com/docs/environment-variables), so editing the variable alone changes nothing until you deploy again.
- Confirm every component now uses the new key. Supabase says there's no automatic usage indicator for legacy keys, so this is a manual check: load the live site, sign in, trigger your webhooks and scheduled jobs, and watch your logs for 401 or Invalid API key errors.
- Retire the old key. A secret key is deleted, which can't be undone, so don't do it until step 5 is done. Legacy anon or service_role keys are deactivated instead. If a client you missed turns up later, move it to a new key rather than re-enabling the legacy keys: Supabase notes that re-enabling them restores the compromised key.
Supabase's rule of thumb for the future: use a separate secret key for each backend component. If one leaks, you only rotate that one, and nothing else breaks.
Places people forget to update
- Next.js and Vite builds: NEXT_PUBLIC_ and VITE_ values are copied into the JavaScript at build time, so they need a rebuild, not just a restart. (These should only ever hold the publishable key.)
- Preview and development environments on your host, not just Production.
- Edge Functions: the new keys come in SUPABASE_SECRET_KEYS and SUPABASE_PUBLISHABLE_KEYS as a JSON object keyed by name (the first key is named default). Code that still reads SUPABASE_SERVICE_ROLE_KEY keeps using the legacy key. If a function checks the key with withSupabase({ auth: 'secret' }), it only accepts the secret key named default, so update it to accept the new key by name (for example auth: 'secret:rotated_secret_key').
- Database Webhooks and pg_net: they usually send the key as Authorization: Bearer. The new secret keys aren't JWTs, so Supabase says to send them on the apikey header instead, and to store them in Vault rather than hardcoding them in SQL.
- Mobile or desktop app versions already in users' hands. They keep the key they were built with until people update, which is one more reason only the publishable key belongs there.
- Automations in Zapier, Make or n8n, and any teammate's local .env file.
Moving from legacy keys to the new ones
If you're switching off anon and service_role rather than replacing one leaked key, Supabase's migration guide uses the same idea (https://supabase.com/docs/guides/getting-started/migrating-to-new-api-keys): both key types work at the same time, so you swap clients one at a time and deactivate the legacy keys only after nothing depends on them.
- Create the new keys (Settings > API Keys, Publishable and secret API keys tab).
- Swap the anon key for the publishable key in client code. It has the same low privileges, so your RLS policies behave the same.
- Swap service_role for a secret key in server code. Secret keys return HTTP 401 if used from a browser.
- Update Edge Functions and webhooks as described above.
- Check every caller, then deactivate the legacy keys. You can re-activate them if something breaks.
A starting prompt for Lovable, Bolt or Cursor: "List every place in this project that reads a Supabase key (env files, server routes, Edge Functions, scripts, webhooks). For each one, tell me which key it uses, whether it runs in the browser or on the server, and the exact variable name, so I can switch them to the new key before I disable the old one. Don't change or delete any keys yourself."
After you rotate
- Search your live site's JavaScript for sb_secret_ and service_role. Neither should appear.
- Check your Supabase logs and usage for anything you didn't do while the old key was out.
- If the leaked key was in git, it's still in the history even after you delete the line. Rotating is what makes it useless.
- Run the Security Advisor: a leaked secret key is an emergency, but open tables (RLS off) leak data with the public key alone. How to check: https://leakyvibe.com/guides/how-to-check-if-supabase-table-is-public
The free LeakyVibe scan at the top of this page checks your live site from the outside for secret keys in the public JavaScript and for Supabase tables that answer to the public key. It's a quick first check, not a full audit: it can't see your server, your dashboard or data behind a login.
Official sources (checked 2026-10-29, re-checked 2026-10-30): Supabase, "API keys" (https://supabase.com/docs/guides/api/api-keys), "Rotate a leaked or compromised API key" (https://supabase.com/docs/guides/security/rotate-leaked-api-keys), "Migrating to publishable and secret API keys" (https://supabase.com/docs/guides/getting-started/migrating-to-new-api-keys), "Rotating Anon, Service, and JWT Secrets" (https://supabase.com/docs/guides/troubleshooting/rotating-anon-service-and-jwt-secrets-1Jq6yd); Vercel, "Environment variables" (https://vercel.com/docs/environment-variables).