Skip to content

What does 'Invalid API key' (Supabase) mean?

Short answer

The apikey the request sent doesn't match a live key for that project. It's usually a wrong env var, a key from another project, or a legacy key you deactivated during rotation. Put the project's publishable (or legacy anon) key in the frontend. Never use a secret or service_role key there to make the error go away.

Last updated 2026-11-25 · Reviewed by ProofForge

The exact error

text
{"message":"Invalid API key","hint":"Double check your Supabase `anon` or `service_role` API key."}

Why it happens

  • The env var is empty in production (a build-time variable wasn't set on the host).
  • The URL is for one project and the key for another.
  • You rotated or deactivated keys and the deployed bundle still has the old one.
  • Whitespace or quotes were copied into the value.

How to fix it

  1. Copy the project URL and publishable key from Settings → API Keys for the same project.
  2. Set them as build-time variables on your host and redeploy. Vite needs the VITE_ prefix and Next.js needs NEXT_PUBLIC_ for browser values.
  3. Keep the secret / service_role key only in server-side env vars.
bash
# .env (frontend, public by design)
VITE_SUPABASE_URL=https://<project-ref>.supabase.co
VITE_SUPABASE_PUBLISHABLE_KEY=sb_publishable_...
# server only, never VITE_/NEXT_PUBLIC_
SUPABASE_SECRET_KEY=sb_secret_...

Check it's fixed

Redeploy, hard-refresh, and check the request's apikey header in DevTools → Network starts with sb_publishable_ (or is the legacy anon JWT).

Check your live app

A free, read-only public-surface scan. It cannot diagnose private sessions or every cause described above.

Read-only. No writes or row contents.

FAQ

Is the publishable / anon key supposed to be public?

Yes. It identifies your project. Row Level Security is what protects your data.

Can I use the service_role key to fix this?

Not in the browser. It bypasses RLS, so anyone who opens DevTools can read and write everything.

I rotated keys and now everything says Invalid API key?

Expected. Update every client and server to the new keys and redeploy.

Sources

Related

Read as Markdown · Scan methodology