Fix guides · Lovable, Bolt, Cursor ·

Fix CORS for Supabase Edge Functions with Lovable

Access to fetch at ... from origin ... has been blocked by CORS policy

Lovable apps often fail to reach Supabase Edge Functions because the function does not include CORS headers in its responses. The browser blocks the response because Access-Control-Allow-Origin is missing for the Lovable runtime domain. The fix is to add CORS handling to the Edge Function so it returns the right headers on all responses, including errors and OPTIONS preflight requests. Supabase's own CORS example includes apikey and x-client-info headers.

Free · 3 a day · no signup

Common causes – and how to tell which one you have

1. Edge Function missing Access-Control-Allow-Origin

How to tell: In DevTools → Network, the failing Edge Function request shows a response but has no Access-Control-Allow-Origin header, or lists a different origin.

Fix: Add CORS handling to the Edge Function: check the request Origin against an explicit list (your Lovable preview domain and production domain), and set Access-Control-Allow-Origin only if it matches. Do not echo any origin blindly. Also add Access-Control-Allow-Methods and Access-Control-Allow-Headers including apikey, x-client-info, content-type and authorization.

2. OPTIONS preflight not handled

How to tell: The Network tab shows an OPTIONS request to the Edge Function that fails or returns 404/405.

Fix: Make the function answer OPTIONS requests with a 2xx status and include Access-Control-Allow-Origin, Access-Control-Allow-Methods, and Access-Control-Allow-Headers.

3. Credentials with wildcard origin

How to tell: You send cookies or HTTP auth and set Access-Control-Allow-Origin: *, which browsers reject when the request uses credentials mode include.

Fix: Set Access-Control-Allow-Origin to the exact allowed origin (not *), and set Access-Control-Allow-Credentials: true. Always validate the Origin against an allowlist first, never echo any origin with credentials enabled.

How to fix it

  1. Open your Supabase project dashboard and go to the Edge Functions page.
  2. Select the failing Edge Function and open its code.
  3. Add CORS handling: return Access-Control-Allow-Origin headers only after validating the request Origin against an explicit allowlist (your Lovable preview domain and your production domain). Do not echo back any origin blindly. Also include Access-Control-Allow-Methods and Access-Control-Allow-Headers with apikey, x-client-info, content-type and authorization (these are sent by the Supabase JS client).
  4. Make sure OPTIONS preflight requests return a 2xx status with the CORS headers.
  5. Include CORS headers on error responses as well, so that 4xx/5xx responses also allow the requesting origin.
  6. Redeploy the Edge Function and test from both the Lovable preview URL and any deployed production URL.

Using Lovable, Bolt or v0?

In Lovable or Bolt, tell the tool: "My fetch to a Supabase Edge Function is blocked by CORS. Add CORS handling to the Edge Function: return Access-Control-Allow-Origin for the requesting origin, handle OPTIONS preflight with a 2xx response, and include CORS headers on all responses including errors. Include apikey and x-client-info (these are sent by the Supabase JS client). Do not use a dashboard allowlist — CORS is set in the function code." Then check the function logs in the Supabase dashboard for crashes.

Using Cursor, Claude Code or Windsurf?

Ask the agent to first find out whether the failing URL is a Supabase Edge Function, your own API, or a third-party API. Have it make the change on the server side (CORS config in the Edge Function), not in the fetch call. To verify, have it run curl -i -X OPTIONS with an Origin: http://localhost:3000 header and an Access-Control-Request-Method header against the endpoint and show the Access-Control-Allow-* headers in the response. Then reload the page and confirm in the Network tab.

The “fix” to avoid

Agents often add mode: "no-cors" to fetch. That hides the error but gives an "opaque" response your code cannot read, so the data is still missing. Other bad fixes: routing through a public CORS proxy (leaks requests and keys), or setting Access-Control-Allow-Origin: * on an API that uses cookies (browsers reject this when credentials are included).

Copy-paste fix prompt for your AI coding tool

Paste this into Cursor, Claude Code, Lovable or Bolt:

My fetch to a Supabase Edge Function fails with: "Access to fetch at [URL] from origin [origin] has been blocked by CORS policy: No Access-Control-Allow-Origin header is present". Check in this order: 1) Does the Edge Function return Access-Control-Allow-Origin for the origin making the request? 2) Does the function handle OPTIONS preflight requests with a 2xx status and include Access-Control-Allow-Methods and Access-Control-Allow-Headers including apikey and x-client-info (headers the Supabase JS client sends)? 3) Are CORS headers also present on error responses (4xx/5xx)? Fix the handler to add the CORS headers on every response, including errors. CORS is configured in the Edge Function response headers, not in a Supabase dashboard allowlist.
Your error looks a bit different? Paste your exact error and get a diagnosis tailored to your code — free.

Questions people ask

Why does the request work in Postman or curl but not in my browser?

CORS is enforced only by browsers, to protect users. Postman and curl ignore it, so a working request there just proves the server responds — it does not prove the server allows your site origin.

Can I fix a CORS error from my frontend code?

No. The Access-Control-Allow-Origin header must come from the server response. From the frontend side, your only real option is to call your own backend route, which then calls the API server-to-server where CORS does not apply.

Where do I configure CORS for a Supabase Edge Function?

In the Edge Function code itself. Add CORS headers to the response. There is no dashboard allowlist for Edge Functions — CORS is always set in the function code. Note that the Supabase JS client sends apikey and x-client-info headers in preflight requests, so your Access-Control-Allow-Headers list must include apikey, x-client-info, content-type, and authorization.

Related errors

Get fixes like this every week