Supabase "Edge Function returned a non-2xx status code": how to fix it
Your Lovable, Bolt or Cursor app calls a Supabase Edge Function and all you get is FunctionsHttpError: Edge Function returned a non-2xx status code. It worked in preview, or it worked yesterday. That message only says the function answered with an error. Here is how to find the real one and fix it.
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Step 1: read the real error first
Don't guess. The real error is waiting in two places:
- The function logs. In the Supabase dashboard open Edge Functions, pick the function and open Logs (on Lovable Cloud, open the backend view and the function's logs). Trigger the error again and read the newest entry.
- The response body. The client hides it, but you can read it in code:
const { data, error } = await supabase.functions.invoke('my-function', { body: { id } })
if (error) {
// FunctionsHttpError keeps the real response
const details = await error.context?.json?.().catch(() => null)
console.error('Function failed:', error.context?.status, details)
}
The status code tells you which fix below to try: 401 means a login or key problem, 404 means the function isn't deployed under that name, 500 means the function crashed, and 546 means it ran out of time or memory.
1. A secret is missing in production (500)
This is the most common cause. The function reads something like Deno.env.get('OPENAI_API_KEY') or a Stripe key, and that secret was never set on the live project, so the function crashes on its first line. In the dashboard open Edge Functions then Secrets and check every name the function reads, spelled exactly the same. From the CLI:
supabase secrets list
supabase secrets set OPENAI_API_KEY=sk-...
Secrets set in a local .env file or in Lovable's preview don't carry over to a different project on their own.
2. The request isn't logged in or the key is wrong (401)
By default Edge Functions check for a valid JWT. If the user's session has expired, or the call is made before sign-in finishes, you get a 401. Make sure you call the function through the Supabase client (it sends the user's token for you) and only after the session exists. If the function must be public, such as a Stripe webhook, deploy it with JWT checks turned off and verify the request another way (for Stripe, check the signature):
supabase functions deploy stripe-webhook --no-verify-jwt
If you recently switched to the new sb_publishable_ and sb_secret_ keys, see our guide on Supabase legacy API keys being disabled.
3. The browser blocks it (CORS)
If the console also shows a CORS error, the function isn't answering the browser's preflight OPTIONS request, or it returns an error response without CORS headers. Answer OPTIONS first and add the headers to every response, including errors:
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}
Deno.serve(async (req) => {
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders })
try {
// ... your code
return new Response(JSON.stringify(result), { headers: { ...corsHeaders, 'Content-Type': 'application/json' } })
} catch (e) {
return new Response(JSON.stringify({ error: String(e) }), { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } })
}
})
More on this in our CORS error after deploy guide.
4. The function crashes on bad input (500 or 400)
If the logs show something like Unexpected end of JSON input or Cannot read properties of undefined, the function got a body it didn't expect. Check that the app sends body as an object and that the function reads it once with await req.json(). Wrap the work in try/catch and return a clear message, as in the code above, so the next failure tells you what went wrong.
5. It's not deployed, or it times out (404 or 546)
A 404 means the name in functions.invoke('...') doesn't match a deployed function on this project. Check the list in the dashboard and redeploy if needed. A 546 or a timeout usually means a slow call to an outside API or a big loop. Return early and move slow work to a background job.
Quick checklist
- Read the function logs and the response body before changing anything
- Every secret the function reads is set on the live project
- Calls send a logged-in user's token, or the function is deliberately public and checks requests another way
- OPTIONS answered and CORS headers on every response
- Input checked and errors returned as clear messages
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, or start with a free scan.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.