Guide
"blocked by CORS policy" Supabase Edge Function: how to fix
How this guide was made: Written with AI assistance and checked against the sources linked on this page.
Updated
The short answer
The browser sends an OPTIONS preflight before your real request. If the function does not answer it with CORS headers, or if any later response (including an error) leaves them out, the browser blocks the call. Add an OPTIONS handler, attach corsHeaders to every response, redeploy, then check the function logs for the real error.
What the error means
The full message in the browser console can look like this:
Access to fetch at 'https://<project-ref>.supabase.co/functions/v1/<function-name>'
from origin 'https://your-app.com' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Your app runs on one domain and the function runs on supabase.co. Before a cross-site call with an Authorization or Content-Type: application/json header, the browser first asks the server, with an OPTIONS request, whether that call is allowed. The server has to reply with Access-Control-Allow-* headers. If they are missing or don't list what your app sends, the browser stops and never shows you the server's real answer.
That is why the same function works from curl, Postman or the dashboard but fails in the app: those tools skip the CORS check.
It can show up right after a deploy, because the deployed code is different from what you tested: a new version without the OPTIONS branch, a crash that now happens on a path you didn't test, or a client that now sends a header the allow-list doesn't include.
Causes
| Cause | How to tell | Fix |
|---|---|---|
The function has no OPTIONS handler | The code goes straight to await req.json(). Logs may show SyntaxError: Unexpected end of JSON input (the preflight has no body) | Return early on OPTIONS with corsHeaders (step 2) |
| Success responses have CORS headers, error responses don't | Preflight passes, but some calls still fail with a CORS message. The function logs show a thrown error or a 400/500 | Add headers: corsHeaders to every Response, including the catch block (step 3) |
| The function crashes before it returns anything | Functions > Logs shows an uncaught error (TypeError, ReferenceError, missing env var) at the time of the failed call | Fix the crash and wrap the handler in try/catch that still returns corsHeaders (step 3) |
The client sends a header that isn't in Access-Control-Allow-Headers | Console says Request header field <name> is not allowed by Access-Control-Allow-Headers | Use the SDK's corsHeaders, or add that header name to your hardcoded list (step 1) |
| The hardcoded header list is out of date | Your _shared/cors.ts lacks x-retry-count, traceparent, tracestate or baggage, and failures appear on retries or with trace propagation turned on | Import corsHeaders from @supabase/supabase-js/cors (v2.95.0 or later), or update the list (step 1) |
| Wrong function URL, or the function isn't deployed | The function name in the URL doesn't match the dashboard, or Functions has no entry for it | Fix the name or deploy it (step 5) |
How to fix it
1. Get a correct set of CORS headers
If your function can use @supabase/supabase-js v2.95.0 or later, import the headers from the SDK so the list stays in step with what the client libraries send:
import { corsHeaders } from 'npm:@supabase/supabase-js@^2/cors'
On an older version, create supabase/functions/_shared/cors.ts:
export const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers':
'authorization, x-client-info, apikey, content-type, x-retry-count, traceparent, tracestate, baggage',
'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
}
and import it with import { corsHeaders } from '../_shared/cors.ts'.
If your frontend sends any custom header of its own (for example x-user-locale), add its name to Access-Control-Allow-Headers, in lowercase, separated by commas.
2. Answer the preflight first
Put this at the very top of the handler, before reading the body, checking auth or calling anything else:
if (req.method === 'OPTIONS') {
return Response.json({ ok: true }, { headers: corsHeaders })
}
3. Send the headers on every response, errors included
A complete function:
import { corsHeaders } from 'npm:@supabase/supabase-js@^2/cors'
export default {
fetch: async (req: Request) => {
// 1. Preflight
if (req.method === 'OPTIONS') {
return Response.json({ ok: true }, { headers: corsHeaders })
}
try {
const { name } = await req.json()
// ...your logic...
return Response.json({ message: `Hello ${name}!` }, { headers: corsHeaders })
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
console.error(message)
return Response.json({ error: message }, { status: 400, headers: corsHeaders })
}
},
}
If your function uses Deno.serve(async (req) => { ... }), the same rule applies inside it: the OPTIONS branch first, and headers: corsHeaders on every new Response(...) or Response.json(...), including early returns for missing input or a failed auth check.
Search the file for new Response( and Response.json( and check each one. One return without the headers is enough to bring the error back on that path.
4. Redeploy
supabase functions deploy <function-name>
If the function is shared code (_shared/cors.ts), redeploy every function that imports it.
5. Confirm the URL and find the real error
In the Supabase dashboard open Functions, pick the function, and check:
- the name matches the one in your app's URL:
https://<project-ref>.supabase.co/functions/v1/<function-name>; - Logs for crashes at the time of the failed call;
- Invocations for the request and response of each call.
If an error happens before the CORS check is satisfied, the browser can report CORS even when the real cause is something else. The logs show what actually happened.
How to check it's fixed
- Test the preflight from a terminal (replace the values in angle brackets):
curl -i -X OPTIONS "https://<project-ref>.supabase.co/functions/v1/<function-name>" \
-H "Origin: https://your-app.com" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: authorization, x-client-info, apikey, content-type"
You want a 200 status and Access-Control-Allow-Origin, Access-Control-Allow-Headers and Access-Control-Allow-Methods in the response headers.
- Test an error path. Send a bad body and check the error response also carries the header:
curl -i -X POST "https://<project-ref>.supabase.co/functions/v1/<function-name>" \
-H "Origin: https://your-app.com" \
-H "Authorization: Bearer <anon-or-publishable-key>" \
-H "Content-Type: application/json" \
-d "not json"
The response should be a 400 with Access-Control-Allow-Origin in the headers.
- Open the live app, open DevTools > Network, trigger the call, and confirm the
OPTIONSrow and thePOSTrow both succeed and the console has no CORS message.
FAQ
Why does it work in curl and Postman but not in my app?
CORS is enforced only by browsers. Curl and Postman don't send a preflight and don't check the headers, so they show the function's real response. The browser needs the OPTIONS answer and the Access-Control-Allow-* headers first.
Should I set Access-Control-Allow-Origin to my domain instead of *?
You can. Replace '*' with your exact origin, for example 'https://your-app.com', with no trailing slash. If you have a preview domain and a production domain, read the request's Origin header, check it against your list, and echo back the one that matches (also send Vary: Origin so caches don't serve one origin's answer to another). A value that doesn't match the page's origin exactly brings the error back.
I added the headers and still see the error. What now?
Open Functions > Logs. The function can crash or return early without headers on one path, for example from a missing secret after deploy or an empty body. Fix that error, make sure the catch block returns corsHeaders, and redeploy.
---
Still blocked? Run the free check at vibe-fixer.com: our AI agent checks your live app, and for $79 it finds the cause, fixes it in the code and re-checks the app.
Can VibeFix fix it for me?
Yes, for apps built in Lovable or any app whose code is on GitHub. The free check confirms the problem on your live app first. If you want it fixed, our AI agent finds the cause, fixes it in your code for $79 and tests your live app again. See pricing.
Sources
- <https://supabase.com/docs/guides/functions/cors>
- <https://supabase.com/kb/troubleshooting/unable-to-call-edge-function.md>
- <https://supabase.com/kb/troubleshooting/edge-function-500-error-response.md>
- <https://supabase.com/docs/guides/functions/function-configuration>
- <https://supabase.com/docs/guides/api/api-keys>
Last checked: 16 November 2026.
VibeFix is independent. Supabase and Stripe are trademarks of their owners.
Want it fixed?
Our AI agent fixes it in your code for $79 and tests your live app again. If we can't complete your re-check, you get your money back.
It starts with a free check that confirms the problem on your live app, no account needed.
More guides · Shopify store accessibility · About · Your privacy choices