Symptom: CORS error calling Supabase Edge Function
The browser blocked the call because the function didn't answer the OPTIONS preflight or return CORS headers. Use Supabase's withSupabase wrapper, which handles CORS and preflight for you, or handle OPTIONS yourself and add CORS headers to every response, including errors.
Cause: The function doesn't answer the preflight or omits Access-Control-Allow-Origin / allowed headers.
How to fix it
- If available in your setup, wrap the handler with
withSupabase(per Supabase's CORS guide) so CORS is handled automatically. - Otherwise, return early for
OPTIONSrequests with the CORS headers. - Include the CORS headers on every response, including error responses.
- Allow the headers supabase-js sends (
authorization,x-client-info,apikey,content-type). - Restrict the allowed origin to your domain in production.
Example
Illustrative example, not from a customer project.
+ if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders })
- return new Response(JSON.stringify(data))
+ return new Response(JSON.stringify(data), { headers: { ...corsHeaders, 'Content-Type': 'application/json' } })
FAQ
Why does it work with curl?
CORS is enforced only by browsers.
Do errors need CORS headers too?
Yes, otherwise the browser hides the real error behind a CORS message.
QUICK FIX · $49 · THIS ERROR ONLY
Get this exact error fixed — $49 Quick FixFixed within scope, or we keep working until it is · Read-only access Not sure it's this error? $149 diagnosis
- Fixed, or we keep working free until it is (within the written scope).
- A human reviews every fix before it ships.
- Read-only access; fixes arrive as pull requests you merge.
- Your $149 diagnosis is credited in full toward any paid job.
One error, one root cause, fixed in your app. If it turns out bigger, we tell you before doing more, and your $49 is credited toward the fix, diagnosis or Launch Path if you book within 14 days. Fixing three? The 3-Error Pack is $119.
Bigger job: More than one auth or data issue? Auth & database security fix, from $400.
Built with Lovable, Bolt, Replit, Cursor or v0? See rescue by platform and the security checklist.