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.
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:/
If the variable is missing entirely, createClient throws before any request is made:
supabaseKey is required.
supabaseUrl is required.
Applies to
@supabase/supabase-jsclients 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 legacyanonandservice_roleJWTs (long strings startingeyJ) - 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-Agentheader and returns HTTP 401 Unauthorized." - Environment variable never reached the build. Vite only exposes
VITE_variables to client code. Next.js inlinesNEXT_PUBLIC_values at build time. On Vercel, changes "only apply to new deployments."
How to fix it, step by step
- 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.
- Check that the URL's project reference matches the project you copied the key from.
- 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:/
For Vite apps, the names must start with VITE_.
- Paste values with no quotes or trailing spaces.
- Redeploy. On Vercel, the running deployment keeps the values it was built with.
- If the key in the browser starts
sb_secret_, or is theservice_rolekey, 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
anonandservice_role, but Supabase is "deprecating theanonandservice_rolekeys by the end of 2026." The fix is the same forsb_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.coreturn 200, not 401. - Each request's
apikeyheader startssb_publishable_(or is the legacyanonkey), neversb_secret_. - Sign in or load a page that reads data, and the error is gone.
Sources
- https:/
/ : key types,supabase. com/ docs/ guides/ getting- started/ api- keys (opens in a new tab) eyJlegacy note, deprecation by end of 2026, legacy keys stay valid until disabled, secret key 401 in browsers, Connect dialog, Settings > API Keys, env example - https:/
/ : "Invalid API key" response body and 401github. com/ supabase/ supabase- js/ issues/ 1247 (opens in a new tab) - https:/
/ :github. com/ supabase/ supabase- js/ blob/ master/ packages/ core/ supabase- js/ src/ SupabaseClient. ts (opens in a new tab) supabaseKey is required. - https:/
/ :github. com/ supabase/ supabase- js/ blob/ master/ packages/ core/ supabase- js/ src/ lib/ helpers. ts (opens in a new tab) supabaseUrl is required. - https:/
/ : unrecognized key format warninggithub. com/ supabase/ supabase- js/ blob/ master/ packages/ core/ supabase- js/ src/ lib/ fetch. ts (opens in a new tab) - https:/
/ :vite. dev/ guide/ env- and- mode (opens in a new tab) VITE_prefix - https:/
/ :nextjs. org/ docs/ app/ guides/ environment- variables (opens in a new tab) NEXT_PUBLIC_inlined at build time - https:/
/ : changes apply only to new deploymentsvercel. com/ docs/ environment- variables (opens in a new tab)
Related fixes
Tool to consider: Supabase.
Supabase Launch Safety Pack
RLS checklist, policy templates, key migration and environment variable setup.
Secure checkout by Stripe
One-time payment through Stripe. Priced in USD. Local taxes such as UK VAT may be added at checkout.