Your Supabase call fails with one of these:

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

or AuthApiError: Invalid API key, or a 401 with No API key found in request. The app worked yesterday, or it works locally and not in production. Here is how to find the cause, and how to fix it without leaking the wrong key.

What the error means

Every request to the Supabase API carries a key in the apikey header. The server could not match it to your project. Either the key is wrong, missing, from a different project, or it was rotated.

The 6 usual causes

  1. Env var not set in production. .env works locally, but the host (Vercel, Netlify, Replit, Lovable, Bolt) has no value, so the client is created with undefined.
  2. Wrong prefix for your framework. In Next.js a browser-side variable must start with NEXT_PUBLIC_. In Vite it must start with VITE_. Without it the value is empty in the browser.
  3. URL and key from different projects. You copied the URL from one project and the key from another (common after cloning a template).
  4. Key was rotated or regenerated. The old key in your deploy no longer matches.
  5. Extra characters. A trailing newline, a space, or quotes pasted into the env var field.
  6. Using the service_role key where the anon key is expected, or the reverse, on a path that checks which one it is.

The fix, in order

  1. Print the project ref from your URL and the first 8 characters of the key length only (never the whole key) at startup, and compare with Settings, API in the dashboard.
  2. Set the env vars in the hosting dashboard, then redeploy. A changed env var does not apply to an existing build.
  3. Check the name matches your framework's public prefix.
  4. Paste values again without quotes or spaces.
  5. Use the anon key in the browser. Use the service_role key only on a server you control.

The mistake that turns a 401 into a breach

When this error blocks you at 2 a.m., it is tempting to paste the service_role key into frontend code because "it works". That key skips row-level security. Anyone who opens your site's JavaScript can read and write every table.

If you did that at any point, rotate the key in the dashboard now. Deploying the fix does not remove the old key from cached bundles or from your git history.

Check what your site ships

The free read-only check at https://proofforge.live looks at your deployed JavaScript for leaked keys and open Supabase or Firebase data, and tells you what it could not test. The anon key in your bundle is normal; a service_role key is not. If you want to know whether one user can read another's data, use the free two-account test: https://proofforge.live/two-account-test

Paid one-time fix reports are $9, $29 and $79, and the free scan does not require them.