LeakPatrol › Guides

Supabase "Invalid API key" and "Legacy API keys are disabled" fixed

By LeakPatrol · Updated 20 November 2026

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

Check these five things, in order

  1. 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.
  2. Stale build. Vite and Next.js copy VITE_ and NEXT_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.
  3. An old key hardcoded somewhere. AI builders like to paste the key straight into supabase.ts or client.ts "just in case". A hardcoded value wins over your environment variable. Search the project for eyJ and sb_publishable_.
  4. 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_role needs a new sb_secret_… key.
  5. 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.

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