VibeCPR

← All fixes

Lovable · Supabase · API keys

Fix Lovable "Invalid API key" after rotating Supabase keys (anon, publishable, secret)

Updated · Applies to: Lovable, Supabase

TL;DR fix: A 401 "Invalid API key" means Supabase rejected the key your app sent, not that your data is gone. After a rotation the live app is usually still sending the old key, or a key from a different project than the URL. Fix: copy the publishable key (sb_publishable_...) or, if your app still uses legacy keys, the anon key (starts eyJ) from the same project as your URL, put it where your code calls createClient, and publish a new build. Never put a secret (sb_secret_...) or service_role key in the frontend; Supabase blocks secret keys in browsers with a 401 anyway. Next time, add the new key everywhere before disabling the old one.

Steps

  1. Confirm what the live app is sending. Open the published site, DevTools → Network, and click a failing request to your-project.supabase.co. Note the project ref in the URL and the apikey header. A 401 with Invalid API key means the gateway rejected that key for that project.
  2. Get the right key from the same project. In Supabase open your project's Connect dialog or Settings → API Keys. Copy the publishable key (sb_publishable_...). If your app was built on legacy keys and you haven't migrated, the anon key (a long eyJ... string) still works as long as legacy keys are enabled.
  3. Update the key where the app creates its Supabase client. Search the Lovable project's code for createClient. Replace the key (and URL, if it's from another project) with the pair you just copied, as one change. Keep the variable names already in the project.
  4. Move any secret or service_role key out of the frontend. If the frontend used a secret or service_role key, remove it. Those keys bypass RLS. Server-side keys belong in Edge Function secrets, which Lovable stores in your Supabase project.
  5. Publish a new build and retest. Editing code or reconnecting doesn't change files already deployed. Publish, hard-refresh the live site and check the Network tab for 200s instead of 401s, then test sign-in and a write.
  6. Only then disable the old key. Supabase lets old and new keys work side by side. Disable legacy keys or delete an old secret key only after every app, Edge Function and webhook uses the new one.

Which key goes where

From Supabase’s API key docs:

KeyLooks likePrivilegeWhere it belongs
Publishablesb_publishable_...LowBrowser, mobile app, your Lovable frontend
Secretsb_secret_...Elevated, bypasses RLSBackend only: Edge Functions, servers
anon (legacy)eyJ... (JWT)LowSame as publishable
service_role (legacy)eyJ... (JWT)Elevated, bypasses RLSSame as secret

Supabase is deprecating the legacy anon and service_role keys by the end of 2026 in favor of publishable and secret keys.

Why rotation broke the app

  • Creating new keys doesn’t break anything. Supabase adds publishable and secret keys alongside the legacy ones, and “your legacy keys keep working” until you disable them in Settings → API Keys.
  • Disabling legacy keys breaks any deployed build that still sends the old anon key.
  • Regenerating the legacy JWT secret invalidates the current anon and service_role keys immediately, per Supabase’s legacy rotation guide.
  • Mismatched pair: the URL is from project A and the key from project B. Same 401.
  • Secret key in the browser: Supabase returns 401 for secret keys sent from a browser.

The fix

  1. Check the failing request in DevTools → Network on the published site: note the project ref in the URL and the apikey header’s prefix (sb_publishable_, sb_secret_ or eyJ).
  2. Copy the right key from that same project’s Connect dialog or Settings → API Keys: the publishable key (or the anon key if you’re staying on legacy keys for now and they’re still enabled).
  3. Update it in code. Search your Lovable project for createClient. Lovable-generated projects usually keep the client in a file under src/integrations/supabase/, but use whatever your project has. Replace URL and key together:
import { createClient } from '@supabase/supabase-js'

export const supabase = createClient(
  'https://YOUR-PROJECT-REF.supabase.co',
  'sb_publishable_...'   // publishable (or legacy anon) key. Never sb_secret_ / service_role here.
)
  1. Edge Functions: if they used the old service_role key, update them to the new secret key. Lovable’s docs say secrets live in your Supabase project, where your Edge Functions read them.
  2. Publish a new build, hard-refresh the live site and confirm 200s in the Network tab. Then test sign-in and one write.
  3. Then retire the old key. Supabase’s rotation guide: create the new key, replace it everywhere, confirm nothing uses the old one, and only then delete it (secret) or disable it (legacy).

Prompt for Lovable

My app returns 401 "Invalid API key" from Supabase after I rotated keys.
Find every place that calls createClient or reads a Supabase URL/key.
In frontend code use only this project URL and this publishable key: <paste>.
Remove any sb_secret_ or service_role key from frontend code; server-side code
must read the secret key from Edge Function secrets. Don't change RLS policies.
List every file you changed.

If you rotated because a key leaked, a secret or service_role key exposure means someone could have bypassed RLS. Review your data and logs. And if the leaked key was only the publishable/anon key, your real exposure is whatever RLS allows: run the two-minute RLS check and see fix USING (true) policies.

When to get help

Get help if you rotated because a key leaked and you're not sure what was exposed, if Edge Functions or webhooks still fail after the frontend works, or if your app mixes keys from two Supabase projects.

FAQ

Why did my Lovable app break when I created new Supabase keys?

Creating publishable or secret keys does not break anything on its own. Supabase says new keys are added alongside the legacy anon and service_role keys, and legacy keys keep working until you disable them in Settings → API Keys. Apps break when the legacy keys are disabled (or the legacy JWT secret is regenerated) while the deployed app still sends the old key.

What is the difference between anon, publishable, service_role and secret keys?

Publishable keys (sb_publishable_...) and the legacy anon key are low-privilege and safe in the browser; RLS decides what they can reach. Secret keys (sb_secret_...) and the legacy service_role key are elevated, bypass Row Level Security, and belong only in backend code such as Edge Functions. Supabase says it is deprecating anon and service_role by the end of 2026.

How do I tell an old key from a new one?

Publishable and secret keys are short strings that start with sb_publishable_ or sb_secret_. Legacy anon and service_role keys are long JWTs that begin with eyJ. Supabase notes that a tutorial or AI assistant telling you to copy a long eyJ key was written for legacy keys.

Can I put the secret key in Lovable so everything works?

No. A secret key bypasses every RLS policy, so anyone who opens your site could read and change all data. Supabase also blocks secret keys from browsers: it matches on the User-Agent and returns HTTP 401 Unauthorized. Keep secret keys in Edge Function secrets.

I regenerated the legacy JWT secret. Why did everything stop at once?

Supabase's guide on rotating legacy keys says that once the JWT secret is regenerated, all current API secrets are immediately invalidated and connections using them are severed until you deploy the new keys. Migrating to publishable/secret keys avoids that downtime.

Will reconnecting Supabase in Lovable fix it?

Lovable's docs describe the connection as how Lovable deploys Edge Functions and reads your schema; disconnecting does not change your code. What the live site sends is whatever key is in the deployed build, so after updating the key in code you still need to publish and verify the live requests.

Sources

Related fixes