Invalid API key (Supabase)

On this page

Short answer

Supabase rejected the apikey your app sent. The usual cause is that the live site's environment has the wrong key, a key from a different project, a disabled legacy key, or a secret key used in the browser. Copy the URL and publishable key from the same project, set them under the exact names your code reads, and redeploy.

Supabase Launch Safety Pack

RLS checklist, policy templates, key migration and environment variable setup.

See what’s inside

Secure checkout by Stripe

One-time payment through Stripe. Priced in USD. Local taxes such as UK VAT may be added at checkout.

The exact error you'll see

The network response from Supabase, visible in the browser's Network tab with HTTP status 401:

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

Source: https://github.com/supabase/supabase-js/issues/1247 (opens in a new tab)

If the variable is missing entirely, createClient throws before any request is made:

supabaseKey is required.

Source: https://github.com/supabase/supabase-js/blob/master/packages/core/supabase-js/src/SupabaseClient.ts (opens in a new tab)

supabaseUrl is required.

Source: https://github.com/supabase/supabase-js/blob/master/packages/core/supabase-js/src/lib/helpers.ts (opens in a new tab)

Applies to

  • @supabase/supabase-js clients in Vite apps (Lovable, Bolt) and Next.js apps (v0, Cursor) deployed to Vercel, Netlify or similar
  • Key types: publishable (sb_publishable_...), secret (sb_secret_...), and legacy anon and service_role JWTs (long strings starting eyJ)
  • Dashboard: Settings > API Keys and the project's Connect dialog

Why it happens

  • Wrong project or bad paste. The key must belong to the same project as the URL. A stray quote, space or cut-off character also breaks it.
  • Legacy key disabled. Legacy keys "stay valid until you disable them in the Settings > API Keys section." Disable them, and an app still using eyJ... fails.
  • Secret key in the browser. "A secret key doesn't work in a browser. Supabase matches on the User-Agent header and returns HTTP 401 Unauthorized."
  • Environment variable never reached the build. Vite only exposes VITE_ variables to client code. Next.js inlines NEXT_PUBLIC_ values at build time. On Vercel, changes "only apply to new deployments."

How to fix it, step by step

  1. In Supabase, open the project's Connect dialog. It shows the URL and publishable key for your framework. Or open Settings > API Keys to see every key.
  2. Check that the URL's project reference matches the project you copied the key from.
  3. Find the variable names your Supabase client file reads, and set exactly those names on your host. Supabase's Next.js example:
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=sb_publishable_...

Source: https://supabase.com/docs/guides/getting-started/api-keys (opens in a new tab)

For Vite apps, the names must start with VITE_.

  1. Paste values with no quotes or trailing spaces.
  2. Redeploy. On Vercel, the running deployment keeps the values it was built with.
  3. If the key in the browser starts sb_secret_, or is the service_role key, replace it with the publishable key. Then rotate the secret key, because it was exposed.

What the docs don't spell out

  • Both key systems work side by side. Creating publishable and secret keys "doesn't revoke your legacy keys." You can switch the app to sb_publishable_ first, confirm it works, and only then disable the legacy keys. (Supabase API keys (opens in a new tab))
  • The hint text is out of date for new keys. The hint names anon and service_role, but Supabase is "deprecating the anon and service_role keys by the end of 2026." The fix is the same for sb_publishable_ keys: check the value and the project. (supabase-js issue (opens in a new tab), Supabase API keys (opens in a new tab))
  • Old supabase-js versions may not know new key formats. supabase-js warns "Unrecognized Supabase API key format" for an sb_ key it doesn't recognize, and says you may need to upgrade. (supabase-js source (opens in a new tab))

How to check it worked

  • Reload the live site with DevTools' Network tab open. Requests to your-project.supabase.co return 200, not 401.
  • Each request's apikey header starts sb_publishable_ (or is the legacy anon key), never sb_secret_.
  • Sign in or load a page that reads data, and the error is gone.

Sources

  1. https://supabase.com/docs/guides/getting-started/api-keys (opens in a new tab): key types, eyJ legacy note, deprecation by end of 2026, legacy keys stay valid until disabled, secret key 401 in browsers, Connect dialog, Settings > API Keys, env example
  2. https://github.com/supabase/supabase-js/issues/1247 (opens in a new tab): "Invalid API key" response body and 401
  3. https://github.com/supabase/supabase-js/blob/master/packages/core/supabase-js/src/SupabaseClient.ts (opens in a new tab): supabaseKey is required.
  4. https://github.com/supabase/supabase-js/blob/master/packages/core/supabase-js/src/lib/helpers.ts (opens in a new tab): supabaseUrl is required.
  5. https://github.com/supabase/supabase-js/blob/master/packages/core/supabase-js/src/lib/fetch.ts (opens in a new tab): unrecognized key format warning
  6. https://vite.dev/guide/env-and-mode (opens in a new tab): VITE_ prefix
  7. https://nextjs.org/docs/app/guides/environment-variables (opens in a new tab): NEXT_PUBLIC_ inlined at build time
  8. https://vercel.com/docs/environment-variables (opens in a new tab): changes apply only to new deployments

Tool to consider: Supabase.

Supabase Launch Safety Pack

RLS checklist, policy templates, key migration and environment variable setup.

See what’s inside

Secure checkout by Stripe

One-time payment through Stripe. Priced in USD. Local taxes such as UK VAT may be added at checkout.