Deutsch
Error messageSupabase · Edge Functions

Fix “blocked by CORS policy” when calling a Supabase Edge Function

Why the browser says blocked by CORS policy when your app calls a Supabase Edge Function, the five usual causes and a handler that fixes it without opening your API.

The message you seeBrowser

has been blocked by CORS policy: Response to preflight request doesn't pass access control check

By Tim GeithnerPublished 5 min read

Short answer

The browser asked your Edge Function for permission before the real request (the preflight) and did not get a valid answer, so it blocked the call. The usual causes: the function has no OPTIONS branch, error responses lack the CORS headers, a header the Supabase client sends is not allowed, the function crashed, or the URL is wrong. Answer OPTIONS first, add the headers to every response and allow your own site’s origin. Never fix it by reflecting any origin with credentials.

Ask an AI about this page:ChatGPTClaude

On this page

Your app called a Supabase Edge Function from the browser, and the browser refused to hand over the answer. Before a call with a JSON body or an Authorization header, the browser first sends an OPTIONS request, the preflight, and asks the function: may this site call you with these headers? If the answer is missing, is an error, or does not allow your site, the real request is never sent. Chrome then prints Access to fetch at '...' from origin '...' has been blocked by CORS policy: Response to preflight request doesn't pass access control check, usually followed by No 'Access-Control-Allow-Origin' header is present on the requested resource. CORS itself is working as designed. Your function’s answer is what needs fixing.

Find your cause in 30 seconds

What you seeMost likely cause
The function reads await req.json() at the top, no OPTIONS checkThe function does not answer the preflight
Normal calls work, but a failing call shows a CORS error instead of your errorError responses have no CORS headers
Request header field x-client-info is not allowed by Access-Control-Allow-HeadersA request header is not allowed
It worked yesterday, the function logs show an errorThe function crashed
It does not have HTTP ok status, or the error started after a renameThe URL is wrong

We reproduced each cause by running the handlers on this page against the same checks the browser makes, and checked Supabase’s gateway answer for a wrong URL on 4 December 2026.

The five causes

1. The function does not answer the preflight

Many generated functions start like this:

VulnerableNo preflight branch
import { corsHeaders } from "npm:@supabase/supabase-js@^2/cors";

Deno.serve(async (req) => {
  const { name } = await req.json(); // the OPTIONS preflight has no body: this throws
  return Response.json({ message: `Hello ${name}` }, { headers: corsHeaders });
});

The preflight is an OPTIONS request without a body. req.json() throws Unexpected end of JSON input, the function fails, and the preflight gets an error instead of your answer. Supabase’s troubleshooting guide names exactly this log line as the sign of a missing CORS handler. The same happens if the function checks a token or a plan before it looks at the method.

2. Error responses have no CORS headers

The success path adds the headers, the early returns do not:

VulnerableError response without CORS headers
import { corsHeaders } from "npm:@supabase/supabase-js@^2/cors";

Deno.serve(async (req) => {
  if (req.method === "OPTIONS") return new Response("ok", { headers: corsHeaders });
  if (!req.headers.get("Authorization")) {
    return new Response("Unauthorized", { status: 401 }); // no CORS headers: the browser hides this 401
  }
  return Response.json({ ok: true }, { headers: corsHeaders });
});

The preflight passes, the real request runs, and the browser then blocks the response because it has no Access-Control-Allow-Origin. Your code never sees the 401 or its message. You see a CORS error and look in the wrong place.

3. A request header is not allowed

supabase.functions.invoke() sends authorization, x-client-info, apikey and content-type, and supabase-js can add more, for example x-retry-count. Every header the browser lists in the preflight must appear in Access-Control-Allow-Headers. A hand-written list such as authorization, content-type fails with Request header field x-client-info is not allowed by Access-Control-Allow-Headers in preflight response. Supabase now ships the full list in the SDK, so you don’t have to keep it in sync by hand.

4. The function crashed

If your code throws before it returns a response, for example on a missing secret, a failed import or a bad body, the runtime answers instead of your code. Supabase warns that the browser may then falsely report CORS as the reason. Open Edge Functions → your function → Logs in the Supabase dashboard: an error there is the real cause. Run the same request with curl, outside the browser, to see the actual status and body.

5. The URL is wrong

A typo in the function name, a function that was renamed or never deployed, or the URL of another project. We called a function name that does not exist: Supabase’s gateway answers the preflight with 404 and {"code":"NOT_FOUND","message":"Requested function was not found"}. Chrome reports that as Response to preflight request doesn't pass access control check: It does not have HTTP ok status. Call functions with supabase.functions.invoke("name") instead of a hand-built URL, so the project URL always comes from your Supabase client.

How to fix it

One handler pattern fixes causes 1 to 3: answer OPTIONS first, add the same headers to every response, errors included, and allow your own site instead of everyone. It takes the header list from the Supabase SDK (version 2.95.0 or later), so new SDK headers are allowed automatically:

Safesupabase/functions/hello/index.ts
import { corsHeaders as supabaseCors } from "npm:@supabase/supabase-js@^2/cors";

// Your site's origin, exactly as the browser shows it: scheme, host and port.
const corsHeaders = {
  ...supabaseCors,
  "Access-Control-Allow-Origin": "https://your-app.example",
};

Deno.serve(async (req) => {
  // 1. Answer the preflight first, before any auth check or body parsing.
  if (req.method === "OPTIONS") {
    return new Response("ok", { headers: corsHeaders });
  }

  try {
    const { name } = await req.json();
    // Check the caller here before you touch user data or paid APIs.
    return Response.json({ message: `Hello ${name}` }, { headers: corsHeaders });
  } catch {
    // 2. Errors carry the headers too, so the browser shows your real error.
    return Response.json({ error: "Invalid request" }, { status: 400, headers: corsHeaders });
  }
});

In the app, call the function through the Supabase client, which builds the right URL and sends the session:

SafeThe call in your app
const { data, error } = await supabase.functions.invoke("hello", {
  body: { name: "Ada" },
});

If you serve the app from several origins (production, www, a preview domain, localhost), compare the request’s Origin with an exact list and send Vary: Origin. The CORS guide has that version, and also shows withSupabase from @supabase/server, which answers the preflight for you. For cause 4, fix the error the logs show. For cause 5, fix the name or deploy the function.

Fix the CORS error on a Supabase Edge Function

My app gets “blocked by CORS policy: Response to preflight request doesn’t pass access control check” when it calls a Supabase Edge Function. Find the function and the code that calls it. Fix the function like this: import corsHeaders from npm:@supabase/supabase-js@^2/cors and override Access-Control-Allow-Origin with our production origin (for several origins, compare the Origin header against an exact-match list and add Vary: Origin). Answer OPTIONS requests at the very top with status 200 and these headers, before any auth check or body parsing. Add the same headers to every response, including every error response and the catch block. In the frontend, call the function with supabase.functions.invoke instead of a hand-built URL.

Do not copy the request’s Origin header into Access-Control-Allow-Origin without an allowlist, do not add Access-Control-Allow-Credentials unless the function really uses cookies, and do not remove the function’s auth check. Show me the changes before deploying.

Check that the fix works

Send the preflight yourself, the way the browser does. Use your project’s function URL and your site’s origin:

Check: send the preflight yourself
curl -i -X OPTIONS "https://YOUR_PROJECT_REF.supabase.co/functions/v1/hello" \
  -H "Origin: https://your-app.example" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: authorization, x-client-info, apikey, content-type"
  1. The answer has status 200, Access-Control-Allow-Origin: https://your-app.example, and an Access-Control-Allow-Headers line that contains all four requested headers.
  2. Change the origin to https://example.com and send it again. Access-Control-Allow-Origin must still name your site, not example.com, and there must be no Access-Control-Allow-Credentials: true. The browser will block that foreign site.
  3. Open your app, call the function with a wrong input on purpose, and check the console: you must see your own error (400, Invalid request), not a CORS error.

Frequently asked questions

Can I fix it with mode: ‘no-cors’ in fetch?

No. In no-cors mode the browser only sends simple requests, so the Authorization header and a JSON body are not allowed, and your code gets back an empty, unreadable response. The error disappears and so does your data. Fix the function instead.

Why does the call work in curl or Postman but not in my app?

CORS is enforced only by browsers. curl and Postman never send a preflight and never check the CORS headers. That is also why Supabase suggests running the request outside the browser: if it fails there too, the real problem is the function, not CORS.

Why does it work on localhost but not on my live site, or the other way round?

The allowed origin must match exactly: scheme, host and port. https://your-app.example, https://www.your-app.example and http://localhost:5173 are three different origins. Allow each one you really use, with an exact-match list.

Is Access-Control-Allow-Origin: * dangerous on my Edge Function?

Not on its own for a function that checks a Bearer token, which is how supabase-js calls functions: the browser never attaches that token by itself. It is Supabase’s documented default. It becomes dangerous together with cookies and Access-Control-Allow-Credentials, and a fixed origin is still the tidier choice.

Sources

  1. 1.Cross-Origin Resource Sharing (CORS) — MDN Web Docs
  2. 2.Request: mode property — MDN Web Docs
  3. 3.Fetch Standard: CORS protocol — WHATWG
  4. 4.CORS (Cross-Origin Resource Sharing) support for Invoking from the browser — Supabase Docs
  5. 5.Edge Function 500 error response — Supabase Docs
  6. 6.Unable to call Edge Function — Supabase Docs

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt