Fix guides · Lovable, Bolt, Cursor, Claude Code ·

Fix Supabase "Edge Function returned a non-2xx"

FunctionsHttpError: Edge Function returned a non-2xx status code

This message comes from supabase.functions.invoke() and means your Edge Function did run, but it answered with an error status such as 400, 401 or 500. The text itself is generic; the real reason is in the function's logs or in the response body. Usually it's a missing secret (API key) in Edge Function secrets, a crash in the function code, or a missing or wrong auth header.

Free · 3 a day · no signup

Common causes – and how to tell which one you have

1. Missing secret in Edge Function secrets

How to tell: The function calls an external API (OpenAI, Stripe, Resend) and the logs show undefined for the key or a 401 from that API. Keys in your app's .env or the Lovable/Bolt chat are not automatically available to Edge Functions.

Fix: Add the key under Edge Functions secrets in the Supabase Dashboard (or supabase secrets set NAME=value), with exactly the name the code reads via Deno.env.get('NAME'), then call the function again.

2. The function throws or crashes

How to tell: The function logs in the Supabase Dashboard show an uncaught error, a stack trace, or a 500 status for your request.

Fix: Read the error in the logs and fix that line. Wrap the function body in try/catch and return a JSON error with a clear message and status, so the next failure explains itself.

3. Function rejects the request on purpose

How to tell: The status is 400, 401, 403 or 404, and the function code has checks that return those, for example 'Missing email' or 'Unauthorized'.

Fix: Read the response body in the client (await error.context.json() or .text()) to see the message, then send the body or headers the function expects.

4. JWT verification fails

How to tell: You get 401 and the logs show the request was rejected before your code ran, often because the user isn't logged in or a webhook from another service is calling the function.

Fix: Call the function through supabase.functions.invoke() from a logged-in client so the user's token is sent. For public webhooks, turn off JWT verification for that one function and verify the webhook signature in the code instead.

5. CORS preflight not handled

How to tell: The browser console also shows a CORS error, and the logs show an OPTIONS request that failed or returned an error.

Fix: Handle OPTIONS at the top of the function by returning 'ok' with the CORS headers, and add the same CORS headers to every response, including error responses.

How to fix it

  1. Open the Supabase Dashboard, go to Edge Functions, select the function and open its logs. Trigger the error again and read the newest entry.
  2. In your client code, after const { data, error } = await supabase.functions.invoke('name', { body }), add: if (error) console.log(await error.context.json()) to see the function's own error message (use .text() if it isn't JSON).
  3. If the logs mention a missing or invalid key, add it under Edge Functions secrets (or run supabase secrets set NAME=value) with exactly the name the code reads via Deno.env.get().
  4. If the logs show a crash, fix the line named in the stack trace and wrap the handler in try/catch that returns a JSON error with status 500.
  5. If the status is 401, make sure the user is logged in and you call the function with supabase.functions.invoke(), or disable JWT verification only for a public webhook function that verifies its own signature.
  6. Call the function again and confirm it returns a 2xx status and the expected data.

Using Lovable, Bolt or v0?

In Lovable or Bolt, ask: "supabase.functions.invoke returns 'Edge Function returned a non-2xx status code'. Log the response body from error.context and show me the function's logs." Then open the Supabase Dashboard, go to Edge Functions, pick the function and read its logs for your request. If it mentions a missing key, add it in the Edge Function secrets with the exact name the code uses; secrets you pasted into chat or into .env are not enough.

Using Cursor, Claude Code or Windsurf?

Ask Cursor or Claude Code to change the client call so it reads the real error with await error.context.json() (falling back to .text()), and to wrap the function in try/catch returning { error: message } with a proper status. Have it check every Deno.env.get() name against supabase secrets list and run the function locally with supabase functions serve before redeploying.

The “fix” to avoid

Agents often 'fix' this by returning status 200 for every outcome, including failures, or by swallowing the error in the client. The message disappears, but the feature still doesn't work and you lose the only signal that something is wrong. Some also turn off JWT verification for all functions, which lets anyone call them. Fix the actual error the logs show instead.

Copy-paste fix prompt for your AI coding tool

Paste this into Cursor, Claude Code, Lovable or Bolt:

supabase.functions.invoke() fails with "FunctionsHttpError: Edge Function returned a non-2xx status code". First, update the client to log the real error body: if (error) console.log(error.context.status, await error.context.text()). Then check in this order: 1) every Deno.env.get() name exists in the Edge Function secrets, 2) the function code throws (wrap the handler in try/catch and return JSON { error } with a proper status), 3) the request is missing the auth header or body fields the function requires, 4) OPTIONS/CORS is handled and CORS headers are on every response, including errors. Don't return 200 for failures and don't disable JWT verification globally. Show me what the logs say, fix the cause, and verify the call now returns 2xx.
Your error looks a bit different? Paste your exact error and get a diagnosis tailored to your code — free.

Questions people ask

Where do I see the real error behind 'non-2xx status code'?

In the Supabase Dashboard under Edge Functions → your function → logs, and in the response body, which you can read in the client with await error.context.json().

I added my API key to .env, why is it still missing?

Edge Functions run on Supabase's servers and only see secrets set for Edge Functions, not your frontend .env file. Add the key to the Edge Function secrets with the exact name the code uses.

What is the difference between FunctionsHttpError, FunctionsRelayError and FunctionsFetchError?

FunctionsHttpError means your function ran and returned an error status. FunctionsRelayError means Supabase couldn't reach your function, and FunctionsFetchError means the request never got to Supabase, for example due to a network or CORS problem.

Official documentation

Related errors

Get fixes like this every week