Fix guides · Lovable, Bolt, Cursor, Claude Code, Replit, v0 ·
Fix Supabase "Invalid API key" and 401 errors
{"message":"Invalid API key","hint":"Double check your Supabase anon or service_role API key."}
Supabase rejects every request with {"message":"Invalid API key","hint":"Double check your Supabase anon or service_role API key."} because the apikey header doesn't match a valid key for that project, or the key was sent on the wrong header. The fix: copy both the project URL and anon (or publishable) key from the same project's Settings > API, set them in your host's environment variables, and rebuild. Never put service_role in client code.
Common causes – and how to tell which one you have
1. URL and key from different projects
How to tell: The Project URL in your env vars points to project A, but the anon key was copied from project B's Settings > API. Every request fails with Invalid API key.
Fix: Open Settings > API in the Supabase Dashboard for the project your app should use. Copy both the Project URL and the anon key from that one page. Update both env vars together.
2. Service role or secret key used in browser code
How to tell: The key in your frontend env var starts with sb_secret_ (new format) or is a JWT that decodes to {"role":"service_role"}. Supabase rejects secret keys from browser contexts with 401.
Fix: Replace the key with the anon (eyJ...) or sb_publishable_... key from the same Settings > API page. Rotate the exposed service_role/secret key immediately in the dashboard.
3. Env var not set on the hosting platform
How to tell: Works locally (reads .env) but fails after deploy. The Network tab shows the apikey header as undefined or missing. The hosting dashboard (Vercel/Netlify/Replit) has no matching variable.
Fix: Add the exact env var names and values in your host's project settings (Environment Variables). Redeploy — NEXT_PUBLIC_ and VITE_ variables are baked in at build time.
4. Legacy keys disabled in the dashboard
How to tell: The app used to work, then started failing after Supabase migrated the project to new keys. In Settings > API Keys, the "Legacy API keys" toggle is off. The old anon JWT no longer works.
Fix: Either re-enable legacy keys as a stopgap, or (better) switch your app to the new sb_publishable_... key from the same page and redeploy. Update all environments.
5. Whitespace or truncation in the copied key
How to tell: The key looks right but console.log(key.length) shows a different length than the dashboard. Often a trailing newline, space, or smart quotes from a password manager.
Fix: Copy the key again directly from the dashboard (click the copy icon). Paste into the env var field with no quotes. Log the length to verify it matches.
How to fix it
- In your hosting dashboard (Vercel, Netlify, Replit, etc.), set two environment variables with the exact names your framework or template expects: - Next.js (standard): NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY - Vite/Lovable/Bolt (Lovable default): VITE_SUPABASE_PUBLISHABLE_KEY and VITE_SUPABASE_URL (note: Lovable uses PUBLISHABLE_KEY, not ANON_KEY) - Plain Node: SUPABASE_URL and SUPABASE_ANON_KEY Paste the values with no quotes, no trailing spaces, no newlines.
- In your hosting dashboard (Vercel, Netlify, Replit, etc.), set two environment variables with the exact names your framework expects: - Next.js: NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY - Vite/Lovable/Bolt: VITE_SUPABASE_URL and VITE_SUPABASE_ANON_KEY - Plain Node: SUPABASE_URL and SUPABASE_ANON_KEY Paste the values with no quotes, no trailing spaces, no newlines.
- If you use the new key format (sb_publishable_...), use that as the anon key. The legacy anon key (JWT starting with eyJ...) still works until you disable legacy keys in Settings > API Keys.
- Redeploy (or restart the dev server locally). The framework inlines NEXT_PUBLIC_ / VITE_ variables at build time, so a redeploy is required after changing them.
- If the error persists, log the key length in your code (console.log(import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY?.length)) to detect hidden whitespace or truncation. A Supabase anon or publishable key is a single continuous string — legacy anon keys (JWT) start with eyJ..., publishable keys start with sb_publishable_....
- If the error persists, log the key length in your code (console.log(import.meta.env.VITE_SUPABASE_ANON_KEY?.length)) to detect hidden whitespace or truncation. A Supabase anon key is a JWT and should be a single continuous string.
- Confirm the request now returns 200 and your data loads. If you rotated keys, update every environment (local .env, preview, production).
Using Lovable, Bolt or v0?
In Lovable or Bolt, tell the AI: "Supabase returns 401 Invalid API key. Make sure the URL and anon/publishable key come from the same project's Settings > API, set them as VITE_SUPABASE_URL and VITE_SUPABASE_PUBLISHABLE_KEY (Lovable's default) in the project's env/secrets, and redeploy. Don't use the service_role key." Then check the Network tab on the preview — the apikey header must match the dashboard.
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to find where the Supabase client is created, print the env var names it reads, compare them with the hosting dashboard, and log the key length (not the key) before the first request. Have it fix the mismatch, redeploy, and verify the apikey header in the browser Network tab.
The “fix” to avoid
Agents often 'fix' this by putting the service_role key in the frontend env var (with NEXT_PUBLIC_ or VITE_ prefix) because it 'works'. This exposes a key that bypasses all RLS to every visitor — anyone who opens DevTools can read your entire database. Rotate any exposed service_role/secret key immediately. Also avoid disabling legacy keys before the app is migrated to sb_publishable_.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My Supabase calls fail with 401 {"message":"Invalid API key","hint":"Double check your Supabase anon or service_role API key."}. Check in order: 1) The Project URL and anon key come from the same project's Settings > API page. 2) The env var names match what the framework expects (NEXT_PUBLIC_ for Next.js, VITE_ for Vite). 3) The key is not the service_role / sb_secret_ key — that must stay server-side only. 4) The env var is set on the hosting dashboard and the site was redeployed after the change. 5) No extra whitespace, quotes, or newlines in the value. Fix the mismatch, redeploy, and verify the apikey header in DevTools Network matches the dashboard key.
Questions people ask
What's the difference between the old anon key and the new sb_publishable_ key?
The old anon key is a JWT (starts with eyJ...). The new sb_publishable_ key is an opaque string. Both work for client-side code. Supabase is deprecating legacy keys by end of 2026. Use sb_publishable_ for new projects; existing projects can keep using anon until legacy keys are disabled.
Why does the hint say 'check your service_role key' if I should never use it in the browser?
The hint is generic — it names the only two key types the original message knew about. It does not mean the service_role key is the fix. Using service_role in the browser is a critical security vulnerability.
My key works for Auth but fails for Storage or Realtime. Why?
Each Supabase service (PostgREST, Auth, Realtime, Storage, Edge Functions) validates the apikey header independently. A key that works for one may be rejected by another if it's the wrong type (service_role blocked from browser) or from a different project. The anon/publishable key works for all services when RLS policies allow it.
Official documentation
- Supabase: API keys (anon, service_role, publishable, secret)
- Supabase: Migrating to new API keys
- GuardLayer: Fix Supabase Invalid API key
- Vibe Code Rescue: Supabase Invalid API key on every request