Supabase "Invalid API key" and "Legacy API keys are disabled" fixed
Your app worked yesterday. Now every Supabase call fails with Invalid API key, or with a message saying Legacy API keys are disabled. Both mean the same thing at heart: the key your live app sends is not one the project accepts. It is almost never a Supabase outage. It is usually a mismatch between the key in your code, the key in your hosting settings and the keys switched on in the Supabase dashboard.
The two messages
- "Invalid API key": the key doesn't belong to the project at that URL, was deleted or rotated, or got mangled on the way (quotes, spaces, a cut-off value).
- "Legacy API keys are disabled": your app still sends an old JWT-style
anonorservice_rolekey (they start witheyJ), but someone turned legacy keys off under Project Settings → API Keys. Supabase now wants the newsb_publishable_…key in the browser.
Check these five things, in order
- Same project? The project ref in
VITE_SUPABASE_URL(the part before.supabase.co) must match the project you copied the key from. Lovable, Bolt and Cursor projects often end up with a test project and a live one. - Stale build. Vite and Next.js copy
VITE_andNEXT_PUBLIC_values into the JavaScript at build time. Changing the value in Vercel, Netlify or your host does nothing until you redeploy. Rebuild after every key change. - An old key hardcoded somewhere. AI builders like to paste the key straight into
supabase.tsorclient.ts"just in case". A hardcoded value wins over your environment variable. Search the project foreyJandsb_publishable_. - Legacy keys switched off. If you (or a teammate, or an AI agent with dashboard access) disabled legacy keys, swap the browser key for the publishable key and rebuild. Server code that used
service_roleneeds a newsb_secret_…key. - Quotes and spaces.
VITE_SUPABASE_ANON_KEY="eyJ..."pasted into a hosting dashboard can include the quote marks. A trailing space or a line break does the same. Paste the bare value.
Quick way to see what your live site really sends: open it, press F12, go to Network, click any request to supabase.co and look at the apikey header. If it starts with eyJ while legacy keys are off, you found it.
The fix you should never use
When the browser key fails, AI tools sometimes reach for the key that "works": the service_role or sb_secret_ key. That key skips Row Level Security and can read and change every row. Put it in a VITE_ variable and it ships to every visitor. Supabase blocks secret keys from browsers (see "Forbidden use of secret API key in browser"), but anyone can still copy it from your JavaScript and use it from a script. Only the publishable key (or the legacy anon key, while it's still enabled) belongs in front-end code.
// Browser: the publishable key, nothing stronger
const supabase = createClient(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY
);
A prompt to paste into Lovable, Bolt or Cursor
Supabase returns "Invalid API key" (or "Legacy API keys are disabled").
1. Find every place a Supabase URL or key is set, including hardcoded
values in the Supabase client file. Remove hardcoded keys.
2. The browser client must read VITE_SUPABASE_URL and
VITE_SUPABASE_PUBLISHABLE_KEY from env only.
3. Never use the service_role or sb_secret_ key in browser code.
4. Tell me which env variables to set on my host, then rebuild.
After the fix, LeakPatrol's free read-only scan checks the JavaScript your live site loads and flags service_role, sb_secret_ and other secret keys, so you can be sure the "fix" didn't leak the wrong key. Background on the key change: what Supabase changed for new projects.
Check your app in a minute
Free, read-only, no sign-up. Every problem comes with a plain-English fix and a prompt for your AI tool.
Get the 10-point checklist
Every check we run on AI-built apps, with how to do each one yourself. Straight to your inbox.
Plus the odd security tip. Unsubscribe any time.
Questions
Why does Supabase say Invalid API key?
The key your app sends does not belong to the project at that URL, was rotated or deleted, or was mangled by quotes or spaces. Check the project ref matches, remove hardcoded keys and rebuild.
What does Legacy API keys are disabled mean in Supabase?
Your app still sends an old eyJ-style anon or service_role key, but legacy keys were turned off in Project Settings, API Keys. Use the sb_publishable_ key in the browser and an sb_secret_ key on the server.
I changed the key in Vercel or Netlify but still get the error. Why?
Vite and Next.js bake public env variables into the JavaScript at build time. Redeploy after changing the value, and make sure no old key is hardcoded in the client file.
Can I use the service_role key to make the error go away?
No. It works, but it skips Row Level Security and anyone can copy it from your site. Use the publishable key in the browser and keep secret keys on a server or in an Edge Function.
More guides
- Supabase RLS policy generator (free)
- Is this API key safe in my frontend? Free key checker
- Security scan MCP server for Claude Code, Cursor and other AI agents
- Supabase's October 30 change: what it fixes for your app's security, and what it doesn't
- Supabase "42501 permission denied for table": the safe fix (not GRANT ALL)
- Supabase "new row violates row-level security policy": 5 causes and the safe fix
- Supabase service_role key exposed? What to do in the next hour
- Supabase select returns an empty array but the data is there: the RLS fix
- Supabase "Database error saving new user": find the cause and fix it safely
- Supabase Storage upload: "new row violates row-level security policy" fixed
- Supabase "infinite recursion detected in policy for relation" fixed
- Supabase Edge Function "blocked by CORS policy" fixed
- Supabase "Forbidden use of secret API key in browser" (401) fixed
- Supabase login or magic link redirects to localhost:3000 (fixed)
- Is your Supabase storage bucket public? What anyone can see, and the fix
- Lovable data exposure study: is your app one of the 24,664?
- Lovable security check: is your Lovable app safe?
- Bolt.new security check: is your Bolt app safe?
- Launch checklist for apps built with Lovable, Bolt and Cursor
- Supabase security check: is your anon key safe? (RLS checker)
- How to check if your API key is exposed on your website
- Are VITE_ env variables secret? How to hide API keys
- Stop users giving themselves your paid plan (Supabase)