Supabase "Legacy API keys are disabled" or "Invalid API key": move your app to the new publishable and secret keys
Your Lovable, Bolt, or Cursor app suddenly gets 401 from Supabase after a restore, a new project, or someone turning off the old anon and service_role keys. Supabase is deprecating those keys by the end of 2026. How to find every place that still uses them and swap safely.
Free · no card
Paste your error. Get a free diagnosis by email.
Tell us what broke and we’ll email you a short diagnosis: the likely cause and what to try first. Usually within a day.
What you are seeing
- Every Supabase call returns
401withLegacy API keys are disabled,Invalid API key, orInvalid JWT, and nothing in your code changed. - It started right after you restored a paused Free project, created a new project, or someone clicked to disable the legacy keys in Settings > API Keys.
- Your
.envor Lovable/Bolt settings hold a long key that starts witheyJ, while the Dashboard now shows keys starting withsb_publishable_andsb_secret_. - The website works but an Edge Function, cron job, Database Webhook, or an older mobile build still fails.
- Edge Functions called with the new key fail with
Invalid JWT.
Why it happens
- Supabase is replacing the long-lived JWT
anonandservice_rolekeys with publishable (sb_publishable_...) and secret (sb_secret_...) keys, and says it is deprecating the legacy keys by the end of 2026. Many AI-generated apps and tutorials still hard-wire theeyJ...keys. - Supabase's published timetable says projects restored from November 1, 2025 are no longer restored with the legacy keys, and new projects do not get them, so paused projects that are restored can break if the app still ships the old key.
- The migration was done in one place but missed a caller: the frontend was updated, but an Edge Function still reads
SUPABASE_SERVICE_ROLE_KEYorSUPABASE_ANON_KEY(those still carry the legacy keys), or a Database Webhook orpg_netcall still sends the old key. - The new keys are not JWTs. Sending one on
Authorization: Bearermakes anything that tries to verify it as a JWT fail, and the Edge Functions built-inverify_jwtcheck only understands the legacy JWT keys. - A plain mismatch: the URL and the key come from different projects (staging key with production URL, or keys from a local supabase start stack).
How to fix it
1. Find out which key is being rejected
# Look at the key your deployed app actually sends (DevTools > Network >
# any request to supabase.co > Request headers > apikey).
# starts with eyJ... -> legacy anon/service_role key
# starts with sb_publishable_ -> new publishable key
# Then check the Dashboard: Settings > API Keys. If the legacy tab says the
# keys are disabled (or the project never had them), the eyJ key is dead.
2. Create the new keys if the project has none
Open Settings > API Keys and the Publishable and secret API keys tab. If you only see a Create new API keys button, click it. Supabase says this adds a publishable and a secret key named default alongside any legacy keys and does not break anything that currently works.
3. Swap the key in the frontend
// .env (names per your framework; Vite shown)
VITE_SUPABASE_URL=https://YOUR_PROJECT.supabase.co
VITE_SUPABASE_PUBLISHABLE_KEY=sb_publishable_... // was the eyJ anon key
// src/lib/supabase.ts
import { createClient } from '@supabase/supabase-js'
export const supabase = createClient(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY
)
// Update the same variable in your host (Vercel, Netlify, Lovable, Bolt)
// and redeploy: frontend env vars are baked in at build time.
4. Swap the key in Edge Functions and server code
// before
const key = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
// after (the runtime injects the new keys as a JSON object by name)
const key = JSON.parse(Deno.env.get('SUPABASE_SECRET_KEYS')!)['default']
# Functions called with the new keys: the built-in check only understands
# legacy JWT keys, so turn it off and authorize in code (see Supabase's
# Securing Edge Functions guide). In supabase/config.toml:
[functions.my-function]
verify_jwt = false
5. Fix Database Webhooks and pg_net calls
-- before: legacy key on Authorization (the new keys are rejected there)
-- 'Authorization', 'Bearer <service_role key>'
-- after: secret key on the apikey header, read from Vault, not hardcoded
select net.http_post(
url := 'https://YOUR_PROJECT.supabase.co/functions/v1/your-function',
headers := jsonb_build_object(
'Content-Type', 'application/json',
'apikey', (select decrypted_secret from vault.decrypted_secrets
where name = 'secret_key')
),
body := jsonb_build_object('event', 'ping')
);
6. Keep the secret key out of the browser
The publishable key replaces anon and is safe in frontend code; Row Level Security still decides what it can read. The secret key replaces service_role, bypasses RLS, and must only live in server code or function secrets. Do not fix a 401 by pasting the secret key into the frontend: Supabase blocks secret keys sent from a browser with 401 anyway, and anyone can copy it from your bundle.
7. Search for every remaining legacy key, then decide on the old ones
# from the repo root: find hardcoded old keys and old variable names
grep -rn "eyJhbGci" --exclude-dir=node_modules .
grep -rnE "SUPABASE_(ANON|SERVICE_ROLE)_KEY" --exclude-dir=node_modules .
# Also check: host env vars, CI secrets, cron jobs, Zapier/Make, mobile builds.
# If legacy keys were disabled and you need a stopgap, Supabase lets you
# re-activate them in Settings > API Keys (unless they were disabled because
# one leaked). Deactivate again once nothing uses them.
How to know it is fixed
The deployed app's requests carry an sb_publishable_ key and return 200, sign-in and data loads work, each Edge Function and webhook succeeds in its logs, and the two grep searches return nothing outside docs. With the legacy keys deactivated, everything still works. Sources: Supabase: API keys (deprecation notice, key types, known limitations), Supabase: Migrating to publishable and secret API keys, Supabase announcement and timetable (discussion #29260), and Supabase: Edge Function 401 error response. Related: Supabase paused my free project, env variables undefined after deploy, and exposed keys.