Supabase "Failed to send a request to the Edge Function": how to fix it

FunctionsFetchError: Failed to send a request to the Edge Function (console: "Access to fetch at 'https://<ref>.supabase.co/functions/v1/<name>' ... has been blocked by CORS policy")

FunctionsFetchError: Failed to send a request to the Edge Function means your app called supabase.functions.invoke() and got no readable response at all. In a browser that almost always means the request was blocked: the function doesn’t answer the CORS preflight, or it isn’t deployed under the name you’re calling (a 404 the browser reports as CORS).

Quick fix

At the very top of the function, return the CORS headers for OPTIONS requests, and add the same headers to every response, errors included. If that doesn’t do it, open Edge Functions in the Supabase dashboard and check that a function with the exact name you pass to invoke() is deployed, then redeploy it.

import { corsHeaders } from 'npm:@supabase/supabase-js@^2/cors'

// first line inside your handler
if (req.method === 'OPTIONS') {
  return Response.json({ ok: true }, { headers: corsHeaders })
}

What you see

A button in your Lovable, Bolt, Cursor or v0 app calls a Supabase Edge Function (a Stripe checkout, an AI call, an email). It works in the function’s test call, maybe even with curl, but from your app you get:

FunctionsFetchError: Failed to send a request to the Edge Function

and in the browser console, a line like this one from issue #16146:

Access to fetch at 'https://xxxxxx.supabase.co/functions/v1/hello3' from origin 'http://localhost:3000' 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.

Why it happens

supabase-js has three errors for functions.invoke(). Its source defines them:

  • FunctionsHttpError: “Edge Function returned a non-2xx status code”. Your function answered, with an error you can read.
  • FunctionsRelayError: “Relay Error invoking the Edge Function”. Supabase’s relay had a problem.
  • FunctionsFetchError: “Failed to send a request to the Edge Function”. The client code throws it when the fetch call itself fails.

In a browser, the usual reason a fetch fails like that is that the browser blocked it. A web page calling https://<ref>.supabase.co/functions/v1/... is a cross-origin request, so the browser first sends an OPTIONS preflight. Supabase’s CORS guide says you “need to handle CORS Preflight requests” yourself. curl doesn’t do CORS, which is why the same function looks fine from the terminal (a Supabase collaborator points this out in #16146).

Check the causes below in order.

Cause 1: the function doesn’t answer the OPTIONS preflight

The function goes straight to await req.json() or an auth check, the preflight fails, and the real request is never sent. This was the fix in #16146: the author “needed this bit at the top”, the if (req.method === 'OPTIONS') check.

This is Supabase’s current example from the CORS guide, for @supabase/supabase-js v2.95.0 and later:

import { corsHeaders } from 'npm:@supabase/supabase-js@^2/cors'

export default {
  fetch: async (req) => {
    // Handle the CORS preflight request.
    if (req.method === 'OPTIONS') {
      return Response.json({ ok: true }, { headers: corsHeaders })
    }

    try {
      const { name } = await req.json()
      return Response.json({ message: `Hello ${name}!` }, { headers: corsHeaders })
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error)
      return Response.json({ error: message }, { status: 400, headers: corsHeaders })
    }
  },
}

If your function uses the older Deno.serve(async (req) => { ... }) style, the same rule applies: the OPTIONS check goes first. If you’d rather not handle it at all, the guide’s withSupabase wrapper from npm:@supabase/server “handles CORS and preflight (OPTIONS) requests for you”.

Cause 2: error responses don’t carry the CORS headers

The success path has corsHeaders, but a return new Response(..., { status: 400 }) or an uncaught exception doesn’t. The browser blocks that response too, so a perfectly readable error from your function turns into the generic “Failed to send a request” message.

Add corsHeaders to every Response the function returns, and wrap the body in try/catch so a thrown error still goes out with them, as in the example above.

Cause 3: a header is missing from Access-Control-Allow-Headers

supabase-js sends authorization, apikey and x-client-info on every request, content-type with a body, and x-retry-count on retries, per the header table. A hand-written list that’s missing one fails the preflight. So does a custom header you pass to invoke().

Importing corsHeaders from the SDK keeps the allowed headers in sync with what the client sends. On an older supabase-js, the guide’s hard-coded _shared/cors.ts works too. Keep its Access-Control-Allow-Headers list complete, and add any custom header you send.

Cause 4: the function isn’t deployed under that name (a 404 reported as CORS)

Supabase’s 404 guide says it directly: “Browsers misreport 404s as generic CORS errors”, so the client “surfaces only a generic message: Failed to send a request to the Edge Function.” That covers typos (capitals, em-dashes instead of dashes, extra slashes), a function that exists in your repo but was never deployed to the project your app uses, and NOT_FOUND_FUNCTION_BLOB, where the dashboard shows the function as ACTIVE but its bundle is missing.

  1. In the dashboard, open Edge Functions, confirm the function is listed, and use its test call.
  2. Compare the name in your code letter for letter:
const { data, error } = await supabase.functions.invoke('create-checkout', { body: { priceId } })
  1. If the test call fails, or a direct request returns {"code":"NOT_FOUND_FUNCTION_BLOB"}, redeploy, from the function’s code tab in the dashboard or with the CLI:
supabase functions deploy create-checkout

The 404 guide also has a SQL query against your edge logs that separates platform 404s (function not found) from 404s your own code returned.

Cause 5: the live app points at the wrong project

The Unable to call Edge Function page lists “Function not deployed” and “Incorrect function URL” among the network causes. If you switched Supabase projects, or have separate staging and production projects, check that your live build’s Supabase URL is the project where the function is deployed. The request URL in the Network tab shows which project ref it went to.

“Edge Function returned a non-2xx status code”

This is the other error you’ll see once the request gets through. It’s FunctionsHttpError: the function did answer, with a 4xx or 5xx status. That’s progress: CORS and routing work, and the problem is inside the function or in what you sent it.

The message itself doesn’t say what went wrong. The response is in error.context, so read its body, and open Invocations and Logs under the function in the dashboard to see what it logged:

import { FunctionsHttpError, FunctionsRelayError, FunctionsFetchError } from '@supabase/supabase-js'

const { data, error } = await supabase.functions.invoke('create-checkout', { body: { priceId } })

if (error instanceof FunctionsHttpError) {
  console.error('Function returned an error', error.context.status, await error.context.json())
} else if (error instanceof FunctionsRelayError) {
  console.error('Relay error', error.message)
} else if (error instanceof FunctionsFetchError) {
  console.error('Request never got a response (CORS, 404 or network)', error.context)
}

The branching follows the client source. If your function returns plain text instead of JSON, use await error.context.text(). Once CORS is fixed, every real failure arrives this way, with your function’s own message, instead of the generic fetch error.

A prompt for your AI builder: “Calling the edge function from the browser fails with ‘Failed to send a request to the Edge Function’. In supabase/functions//index.ts, return the CORS headers for OPTIONS requests before anything else, import corsHeaders from npm:@supabase/supabase-js@^2/cors, and include corsHeaders on every response including errors and the catch block. Then check that the name passed to supabase.functions.invoke matches the deployed function exactly.”

A note on Access-Control-Allow-Origin: *

Wide-open Access-Control-Allow-Origin: * is what Supabase’s examples use. It’s fine for a function that checks the user’s JWT. If your function does anything sensitive without checking who’s calling, CORS won’t protect it, because anyone can call it with curl. Fix that in the function, not in the headers.

Check it worked

  1. Redeploy the function, then trigger the action from your deployed site, not just localhost.
  2. In the Network tab, the OPTIONS request to /functions/v1/<name> returns 200 with an access-control-allow-origin header, followed by the real POST.
  3. Under the function in the dashboard, the invocation shows up in Invocations and Logs.
  4. Force an error (send an empty body). Your app should get a FunctionsHttpError with the function’s message, not the generic fetch error.

Edge Functions are often where the secret keys live (Stripe, OpenAI, email). If a key ever sat in your frontend code before you moved it into a function, read secret key exposed in your frontend bundle and rotate it. For CORS on your own API (not Supabase), see blocked by CORS policy. If the function writes to a table and now fails with a policy error, see new row violates row-level security policy. For someone to review your functions, auth checks and Row Level Security as a whole, get a written pre-launch audit.

FAQ

What is the difference between "Failed to send a request to the Edge Function" and "Edge Function returned a non-2xx status code"?

The first is FunctionsFetchError: the fetch itself failed, so your app got no readable response (usually CORS or a 404 the browser reports as CORS). The second is FunctionsHttpError: your function answered with an error status, and its body is in error.context.

Why does the Edge Function work with curl but not from my app?

curl does not do CORS. A browser sends an OPTIONS preflight first and blocks the request if the function does not answer it with CORS headers. Handle OPTIONS at the very top of the function.

Does a CORS error always mean my CORS headers are wrong?

No. Supabase says browsers misreport platform 404s as generic CORS errors. If the function name in invoke() does not match a deployed function, or its bundle is missing (NOT_FOUND_FUNCTION_BLOB), you see the same message. Check the name and redeploy.

Is Access-Control-Allow-Origin: * safe for an Edge Function?

It is what Supabase's examples use, and it is fine for a function that checks the caller's JWT. CORS does not stop anyone calling your function with curl, so a function that does something sensitive must check who is calling in its own code.