# 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).

> **Scan box (use the site's ScanBox component):** Check whether your live app has this problem. Paste your URL for a free outside-in scan covering exposed keys, open database rules, CORS and security headers. [Scan my app free →](https://proofforge.live/#scan)

## 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

- [Supabase: API keys](https://supabase.com/docs/guides/api/api-keys)
- [Vite: Env variables](https://vite.dev/guide/env-and-mode)

## Related

- [Fix: secrets in VITE_ / NEXT_PUBLIC_ vars](/fix/vite-env-secrets)
- [Fix: service_role key exposed](/fix/service-role-key-exposed)
- [Supabase service_role / secret key leaked: first 10 minutes](/leaked-keys/supabase-service-role)
- [Supabase JWT role decoder](/tools/supabase-jwt-role-decoder)
- [Exposed API key checker](/tools/exposed-api-key-checker)
- [All errors](/errors)
