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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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:
| Key | Looks like | Privilege | Where it belongs |
|---|---|---|---|
| Publishable | sb_publishable_... | Low | Browser, mobile app, your Lovable frontend |
| Secret | sb_secret_... | Elevated, bypasses RLS | Backend only: Edge Functions, servers |
| anon (legacy) | eyJ... (JWT) | Low | Same as publishable |
| service_role (legacy) | eyJ... (JWT) | Elevated, bypasses RLS | Same 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
anonkey. - 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
- Check the failing request in DevTools → Network on the published site: note the project ref in the URL and the
apikeyheader’s prefix (sb_publishable_,sb_secret_oreyJ). - 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).
- Update it in code. Search your Lovable project for
createClient. Lovable-generated projects usually keep the client in a file undersrc/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.
)
- 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.
- Publish a new build, hard-refresh the live site and confirm 200s in the Network tab. Then test sign-in and one write.
- 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.
Want us to look at your app?
VibeCPR Rescue Reports are launching soon: we scan your repo and live app, and a human verifies every critical finding. Join the waitlist for early access and founder pricing. You'll also get Patch Notes, our weekly fixes newsletter.
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
- Supabase docs: Understanding API keys
- Supabase docs: Rotate a leaked or compromised API key
- Supabase KB: Rotating anon, service and JWT secrets (legacy)
- Lovable docs: Supabase integration
Related fixes
- AI agent pushed API keys to GitHub? git commit --no-verify skipped your pre-commit hook
- Fix a surprise AI API bill: rate limits and spend caps
- Your app is public to anyone with the link: a share link is not a login
- Triage Bolt's "RLS Policy Always True" warning: when it's fine and how to fix it
- Fix secrets leaked via Claude Code .claude/settings.local.json
- Fix "Your Cloud Firestore database has insecure rules" (Firebase email)
- Login lockout keyed only on email? Rate-limit by IP and account without locking users out
- Lovable won't publish because of security findings? What each finding means and how to fix it
- "Sign in with Google" lets anyone in: restrict who can use your app (allowlist, domain check, RLS)
- Fix Stripe sk_live keys left in MCP or agent config
- Fix Stripe webhook "No signatures found matching the expected signature for payload"
- Fix Supabase auth redirecting to localhost after deploy
- Fix Supabase Edge Function "blocked by CORS policy" (preflight)
- Fix Supabase "infinite recursion detected in policy for relation" (42P17) in RLS
- Form says success but no row in Supabase (or Stripe webhook returns 200 but writes nothing)
- Fix "new row violates row-level security policy for table" (Supabase)
- Supabase only returns 1,000 rows? Fix the silent max rows limit in Lovable and Bolt apps
- Fix Supabase "permission denied for table" (42501) on new tables after Oct 30, 2026
- Fix a Supabase public Storage bucket that lets anyone list or download your files
- Supabase Realtime leaking rows or not receiving changes: fix RLS and the publication
- Supabase RLS not working on the server? Your API uses the service_role or secret key
- Fix Supabase RLS policy USING (true) that makes a table publicly readable
- Your app's rules only live in the frontend: move them into Supabase
- Fix a Supabase SECURITY DEFINER function anyone can call through /rest/v1/rpc
- Fix Supabase .single() "JSON object requested, multiple (or no) rows returned" (PGRST116)
- Stop Supabase users from upgrading their own plan or making themselves admin
- Fix a Supabase view that bypasses RLS: security_invoker and revoking anon write grants
- Fix "404: NOT_FOUND" on refresh in a React/Vite app on Vercel (vercel.json rewrite)
- Fix "Module not found: Can't resolve" on Vercel (works locally)
- Is my Vite dev server exposed? Check --host / server.host and patch the RCE (GHSA-p4f5-76q4-ffq7)