Supabase Edge Function CORS error: a working fix
"Blocked by CORS policy" when your app calls a Supabase Edge Function? The function must answer the browser's OPTIONS check. Working code and the common mistakes.
4 min readsupabase, cors, edge functions, error fix
Short answer: the browser sends a preflight OPTIONS request before calling your Edge Function, and the function must answer it with the right CORS headers, before it does anything else. Edge Functions do not copy the CORS settings of your database API, so you have to add them yourself, to every response, including errors.
You see it in the browser console as: Access to fetch at ... has been blocked by CORS policy: Response to preflight request doesn't pass access control check. It is common in Lovable and Bolt apps that call a function for AI features, payments or email.
What CORS is, in plain words
A web page on yourdomain.com is not allowed to call another address unless that address says it is okay. Before a call that carries extra headers, the browser first asks, with an OPTIONS request, "may I?" If the function does not answer yes, the browser refuses to send the real request and reports a CORS error. The message is about the answer to that question, not about your actual call.
A working pattern
Put the shared headers in one file:
// supabase/functions/_shared/cors.ts
export const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers":
"authorization, x-client-info, apikey, content-type",
"Access-Control-Allow-Methods": "POST, GET, OPTIONS",
};
Then use them in the function:
// supabase/functions/my-function/index.ts
import { corsHeaders } from "../_shared/cors.ts";
Deno.serve(async (req) => {
if (req.method === "OPTIONS") {
return new Response("ok", { headers: corsHeaders });
}
try {
const data = { message: "hello" };
return new Response(JSON.stringify(data), {
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
} catch (error) {
return new Response(JSON.stringify({ error: String(error) }), {
status: 400,
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
}
});
Newer Supabase documentation and the supabase-js library include a ready-made helper for this. Check the current docs for your version, and use it if it is there.
The four mistakes behind most CORS errors
1. The OPTIONS check is not first
If your code reads the body, checks the user or queries the database before answering OPTIONS, and any of that throws, the preflight never gets its headers. The OPTIONS branch goes at the very top of the handler.
2. You require sign-in before answering OPTIONS
Preflight requests do not carry the user's login token. A function that rejects requests without authorisation will reject the preflight, and the browser reports a CORS error. Answer OPTIONS first, and check authorisation after.
3. Headers are missing from error responses
If a 400 or 500 response has no CORS headers, the browser hides the real error and shows a CORS one instead. Add the headers to every response, including the ones in your catch block.
4. The allowed headers are incomplete
The Supabase client sends authorization, apikey and content-type. If your Access-Control-Allow-Headers list leaves one out, the preflight fails.
If the preflight passes but the call still fails
Open the Network tab, find the real request (not the OPTIONS one) and read its status. A 401 means the call is missing a valid token. A 500 is a bug in the function, and it only looks like CORS because the error response had no headers. Look at the function's logs in the Supabase dashboard.
After changing the function
Local changes do nothing until deployed. Deploy again, for example supabase functions deploy my-function, and hard-refresh the page. If you edit through a builder such as Lovable, make sure the function was actually redeployed.
Tighten it before launch
Access-Control-Allow-Origin: * lets any website call your function from a visitor's browser. For a function that costs money or touches data, allow only your own domain:
const allowed = ["https://yourdomain.com", "https://www.yourdomain.com"];
const origin = req.headers.get("origin") ?? "";
const headers = {
...corsHeaders,
"Access-Control-Allow-Origin": allowed.includes(origin) ? origin : allowed[0],
};
CORS does not replace sign-in or rate limits, because scripts outside a browser ignore it. See the AI bill that arrived before the first customer for why a function that calls a paid service needs both.
Calling another company's API from the browser
If the error is about a third-party API and not your function, route the call through an Edge Function. Your page calls your function, and the function calls the other service from the server, where there is no CORS rule. It also keeps the API key out of the browser.
Ask your AI tool
"Our Edge Function fails with a CORS preflight error. Move the OPTIONS handler to the top of the function, return CORS headers on every response including errors, allow authorization, apikey, x-client-info and content-type, then restrict the allowed origin to our domain. Redeploy the function."
FAQ
Why does the error say CORS when the real problem is something else?
When a function fails without CORS headers, the browser replaces the real error with a CORS one.
Is Access-Control-Allow-Origin * safe?
For public, harmless functions, yes. Not for anything that spends money or reads private data.
Can I disable CORS in the browser to test?
That hides the problem on your machine only. Your users still hit it.
Stuck on this?
Send us a link. Broken function calls are routine for us to trace.