If your Lovable Supabase connection broke after publish and the Network panel shows 401 Invalid API key, do not start with RLS or authentication code. The published frontend reached Supabase; its gateway rejected the key sent to that project URL. Compare the live URL with the publishable key from the same project. A mismatch gives you a narrow fix: restore the matching pair and publish once. A match rules out this cause and sends you to the remaining checks below.
Every platform claim below was checked against Lovable and Supabase vendor documentation on 2026-10-12. App Rescue Desk is independent and is not affiliated with Lovable or Supabase.
TL;DR
- Save one failed request from the published app before another deployment replaces the evidence.
- Compare the project reference in the request URL with the project that issued the publishable key.
- Treat the Supabase URL and publishable key as one pair. Do not copy either value from another project.
- Update the production configuration source, publish once and repeat the same customer action.
- Do not change RLS policies, OAuth redirect URLs or the database password to fix an API-key rejection.
What does “Invalid API key” mean in a published Lovable app?
Every request to a Supabase project must carry an API key. The key identifies the application component making the request; a signed-in user’s session identifies the user separately. Supabase documents this distinction in its API key guide.
When the live request returns 401 with Invalid API key, the Supabase gateway did not accept the key sent to that project endpoint. The response does not prove that the database is offline. It also does not identify where the wrong value entered the build.
For an app connected to a Supabase project you own, Lovable connects one Lovable project to one Supabase project at a time. The project connection, generated client code and published build must still agree about which project they use. Lovable documents the connection and its limits in Connect to Supabase.
This article concerns that external Supabase connection. A project using Lovable’s built-in backend has different controls in the Cloud view.
Why can a Supabase connection break after the app was already live?
A working release already had a valid URL-and-key pair. A later change can separate them:
- someone connects or copies configuration from another Supabase project;
- a production variable changes, but the published build still contains the old value;
- a rollback restores a build that uses a retired key;
- the team disables a legacy
anonkey before every deployed client has moved to a publishable key; - browser code receives a secret key instead of a publishable key;
- a second hosting environment builds the same repository with different values.
Supabase gives every project its own API URL and keys. Its Data API examples initialize the client with both values together (Supabase API routes). A valid key from project B is not the matching key for project A’s URL.
The page can still load because its HTML, CSS and browser files come from the web host. The failure begins when those browser files call Supabase. That is why a working landing page does not establish a working backend connection.
How do you confirm which Supabase project the live app calls?
Use the published domain, not the Lovable preview.
- Open the failing page.
- Open the browser developer tools.
- Open Network.
- Repeat the failing action once.
- Select the failed
rest/v1,auth/v1,storage/v1orfunctions/v1request. - Record the request URL, status and redacted response body.
- Copy only the project reference from the hostname.
A hosted Supabase URL has this shape:
https://<project-reference>.supabase.co
Now open the intended production project in Supabase. Use its Connect dialog or Settings → API Keys page. Confirm that the project reference matches the failed request.
Next, compare the browser-safe key used by the application with a publishable key from that same project. Compare it locally. Do not paste the full key into an incident ticket or chat.
Publishable keys and legacy anon keys are designed for public application components. Secret keys and legacy service_role keys are not. Supabase states that a secret key used in a browser returns 401 Unauthorized; never solve that response by exposing another elevated key.
What is one concrete fix for a mismatched project URL and key?
Consider this configuration found in one published build:
Request URL project: project-a
Publishable key from: project-b
Response: 401 Invalid API key
The narrow fix is to restore one complete pair. If project A is the intended production backend:
- Open project A’s Connect dialog.
- Copy project A’s API URL.
- Copy project A’s publishable key.
- Find where the production Supabase client reads those two values.
- Replace both values as one change.
- Publish one new build.
- Confirm that the published domain serves that build.
- Repeat the same failing action once.
The client initialization should receive the pair from one configuration source:
import { createClient } from '@supabase/supabase-js'
export const supabase = createClient(
SUPABASE_URL,
SUPABASE_PUBLISHABLE_KEY
)
Use the variable names already present in the project. Do not create duplicate variables with similar names. If the values enter browser files during the build, changing a dashboard field alone does not change the existing files. The production environment-variable check explains how to confirm which value the live build received.
After publishing, check the Network request again. A new 200, 201 or expected application response supports that the URL-and-key mismatch caused the failure. Then test the complete customer action. A successful health request does not prove that login, saving or file upload completes.
What if the URL and publishable key already match?
Keep the 401 response and test the remaining causes one at a time.
Is the request missing its apikey header?
Supabase requires an API key on its API routes. If the header is absent, trace how the published client is initialized. An empty variable, conditional client setup or proxy can remove the value before the request leaves the browser.
Do not add the key manually to every fetch call if the application already uses the Supabase client. Fix the shared client configuration.
Was the key retired before this build stopped using it?
Supabase supports publishable keys alongside legacy anon keys. Creating a new publishable key does not disable the legacy key. Deactivation is a separate action, and Supabase recommends moving clients before retiring the old key (migrating to new API keys).
If the published app still sends a deactivated key, deploy the active publishable key first. Confirm the new deployment. Retire an old key only after no live component uses it.
Is a secret key present in the browser?
A value beginning with sb_secret_ belongs only in code you control, such as a server or Edge Function. Remove it from the browser configuration. Replace it with the project’s publishable key and review where the secret was exposed.
Do not put a service_role key in the browser either. It bypasses Row Level Security and creates a separate security incident.
For a wider check before another release, the Lovable production-readiness bundle check separates browser-safe keys from secrets that must remain server-side.
Is the wrong deployment still serving production?
A successful publish does not establish which release the custom domain serves. Record the live deployment identifier, then inspect the request again. If the hostname or key remains unchanged, the new build is not live or it read a different configuration source.
Does Lovable show “Supabase connection issue” only in the editor?
Lovable uses that message when it cannot find the connected project or the workspace no longer has access to its Supabase organization. Reconnecting the organization can restore editor access. Lovable also states that disconnecting Supabase does not change the application code or delete the project.
Treat the editor connection and the published request as separate evidence. Fixing the connector does not prove that the live build now contains the intended URL and key.
Which similar Supabase errors need a different fix?
| Evidence from the live request | What it points to | Next investigation |
|---|---|---|
401 Invalid API key |
API key rejected for the requested project | URL, key, header and deployed build |
Data request returns [] without an error |
The request reached the project but no rows were visible | RLS returned an empty result |
42501 or permission denied |
Database grants or policy permissions | Grants and RLS policies |
redirect_uri_mismatch |
OAuth callback is not authorized | Lovable Google callback mismatch |
Wrong password or SQLSTATE 28P01 |
A server-side database connection was rejected | Supabase SCRAM wrong-password diagnosis |
Edge Function returned a non-2xx status code |
A function request returned an HTTP error | Lovable Edge Function status diagnosis |
Changing RLS cannot make an invalid API key valid. Rotating the database password cannot repair a browser API request. Keep the repair tied to the recorded response.
What evidence should you keep before another publish?
Use one short incident record:
LOVABLE–SUPABASE CONNECTION FAILURE
1. Published app URL:
2. Failed customer action:
3. Timestamp and timezone:
4. Request path and status:
5. Redacted response body:
6. Project reference in the request hostname:
7. Expected production project reference:
8. Key type: publishable, anon, secret or service_role:
9. Current deployment or commit:
10. Last successful action:
11. Project, key or deployment changes since then:
Do not include complete API keys, access tokens, cookies or customer records. A project reference and key type are enough to identify most configuration mismatches.
Frequently asked questions
Does Invalid API key mean Supabase is down?
No. The response says the gateway rejected the key supplied for that request. Check the project URL, key and header first. Check platform status separately when several unrelated projects fail at the same time.
Should I generate a new key immediately?
Not for a simple mismatch. First confirm which key the live build sends and which project receives it. Generating another key adds a third value without explaining the failure. Rotate a key when it was exposed or must be retired, then update every component before disabling the old one.
Why does the Lovable preview work while the published app fails?
Preview and production can use different builds or configuration sources. A working preview proves that its URL-and-key pair works. Inspect the request made by the published domain before copying any value.
Can I use the Supabase secret key to avoid the error?
No. A secret key must not ship to a browser and bypasses Row Level Security. Use a publishable key in the browser. Keep secret keys in server-controlled components.
Will reconnecting Supabase in Lovable repair the published app?
Only if the reconnection also results in a new build with the intended project configuration. Confirm the live request after publishing. The connector status alone does not prove which values the browser uses.
When does this require a production investigation?
The self-service repair ends when you cannot identify the live configuration source, several deployments use different keys, or a key change coincides with failed payments, account access or customer writes. Restoring future requests does not account for work missed during the incident.
If the URL and key appear correct but the published workflow still fails, the Lovable app fix service can trace the live request through the deployed build and Supabase project →. The engagement covers a defined diagnosis and repair for the broken workflow; it is not ongoing maintenance.