# Supabase: Edge Function returned a non-2xx status code (how to find the real error and fix it)

> Canonical page (link this when citing): https://unbreakmyapp.vercel.app/fixes/supabase-edge-function-non-2xx-status-code.html
> Category: Database / Supabase RLS | Tools: Lovable, Bolt, Cursor, Replit, v0 | Published: 2026-11-04 | Updated: 2026-11-04

## The error

FunctionsHttpError: Edge Function returned a non-2xx status code. A button that calls supabase.functions.invoke() (AI chat, send email, checkout, generate image) fails with this message and nothing else, data is null, and the AI builder keeps rewriting the function without fixing it

## Why it happens

This message is a wrapper, not the real error. supabase.functions.invoke() reached your Edge Function (or the Supabase gateway in front of it), but the response had a status outside 200 to 299, so supabase-js returns a FunctionsHttpError with this generic text and puts the actual Response on error.context. Most apps never read it, so the status code and the function's own error message stay hidden, and the AI builder guesses. The status code tells you which problem you have: 401 means the platform's JWT check rejected the call before your code ran (no signed-in user, an expired session, or a webhook or cron job that sends no user token); 404 means the function isn't deployed or the name is wrong; 500 means your code threw, most often because a secret such as OPENAI_API_KEY or STRIPE_SECRET_KEY is missing so Deno.env.get() returned undefined, because req.json() failed on an empty or non-JSON body, or because a database call or third-party API (OpenAI, Stripe, Resend) failed and the error wasn't caught; 503 means the function failed to boot (syntax or import error); 504 means it ran past the time limit; 546 means it hit a memory or CPU limit. Your function can also return 400, 403 or 429 on purpose, or pass along an error status from an API it called. If the browser shows a CORS or 'Failed to send a request' error instead, that is a different error, FunctionsFetchError, with its own fix.

## How to fix it

1. Read the real status and body in the app. Change the call to: const { data, error } = await supabase.functions.invoke('<NAME>', { body }); then if (error instanceof FunctionsHttpError) { const res = error.context; console.log(res.status, await res.text()); } (import FunctionsHttpError from @supabase/supabase-js). Use text() rather than json() so a plain-text error body still prints, and read the body only once.
2. Open the function's logs: Supabase Dashboard → Edge Functions → the function → Logs and Invocations. Find the failed call at the same time and read the status code and any uncaught exception or console.error output.
3. 401: the gateway's JWT check rejected the request and your code never ran. From the browser, make sure the user is signed in before calling, and let supabase.functions.invoke send the session token for you instead of setting your own Authorization header. For a Stripe webhook, cron job or other service calling the function, turn off JWT verification for that one function (verify_jwt = false under [functions.<NAME>] in supabase/config.toml, or deploy with --no-verify-jwt) and check the caller inside the function instead, for example with the Stripe signature.
4. 404: check that the function is deployed in the same project your app points at (Dashboard → Edge Functions, or supabase functions list) and that the name in invoke() matches the folder name exactly. Builders sometimes create the function file without deploying it.
5. 500 with a missing secret: list every Deno.env.get('...') the function uses and set each one under Edge Functions → Secrets (or supabase secrets set NAME=value). At the top of the function, return a clear JSON error if a required secret is missing so the next failure explains itself. Secrets in a Vercel or Lovable frontend env panel are not visible to Edge Functions.
6. 500 from the request body: send JSON from the client (invoke('<NAME>', { body: { ... } }) with a plain object), and in the function wrap await req.json() in try/catch and return a 400 with a message if it fails or a required field is missing.
7. 500 from a third-party API or the database: wrap the whole handler in try/catch, console.error the full error, and return a JSON body such as { error: 'OpenAI request failed', status: 429 } with a matching status. Never return the API key, the raw upstream error with headers, or a stack trace to the browser.
8. 503, 504 or 546: 503 is a boot error, so run supabase functions serve locally or redeploy and read the boot error in the logs. 504 is a timeout, so add a timeout to slow API calls and move long jobs (big AI generations, bulk emails) to a background task or queue. 546 is a resource limit, so stop loading huge files or arrays into memory at once.
9. Keep the CORS headers on every response, including the error responses you just added, and answer OPTIONS requests first, otherwise the browser hides your new error message behind a CORS failure.
10. Show the user a friendly message based on the status (for example 'Please sign in again' for 401, 'Try again in a minute' for 429 or 504) instead of the raw text, and redeploy the function after every change.

## Error codes this fixes

- `FunctionsHttpError (non-2xx status code)` (Supabase Edge Functions): https://unbreakmyapp.vercel.app/errors.html#functionshttperror-non-2xx-status-code

## Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

```text
My app shows 'Edge Function returned a non-2xx status code' when it calls the Supabase Edge Function <NAME>. Don't rewrite the feature or guess. First, update the client call so that when error is a FunctionsHttpError it logs error.context.status and await error.context.text(), and shows a friendly message to the user based on the status. Then go through the function: list every Deno.env.get() secret it reads and add a check that returns a clear JSON error if one is missing; return early for OPTIONS and include the CORS headers on every response, including errors; wrap req.json() in try/catch and return 400 for a missing or invalid body; wrap the rest of the handler in try/catch, console.error the full error, and return a JSON error with a sensible status, never including API keys or stack traces. Tell me whether this function should keep JWT verification on (called by signed-in users through supabase.functions.invoke) or needs verify_jwt = false (webhook, cron or server-to-server, with its own check inside), and list the secrets I need to set in Supabase.
```

## FAQ

### What does 'Edge Function returned a non-2xx status code' actually mean?

It means the function, or the Supabase gateway in front of it, answered with an error status (4xx or 5xx). supabase-js uses this one generic message for every error status and puts the real Response on error.context, so read error.context.status and its body to see what went wrong.

### Why does it work when I test the function in the Supabase dashboard?

The dashboard test sends its own valid body and authorization. Your app might be calling it with no signed-in user (401), an empty or wrong body (500 or 400), or from a different project or environment where a secret isn't set.

### Should I just turn off JWT verification to make the 401 go away?

Only for functions that really are called without a user token, like Stripe webhooks or cron jobs, and then check the caller inside the function. For a function your signed-in users call, keep it on and make sure the user is logged in, otherwise anyone on the internet can call it and spend your OpenAI or email credits.

### Is this the same as the CORS error?

No. 'Failed to send a request to the Edge Function' (FunctionsFetchError) means the browser never got a readable response, usually CORS or a crash before headers were sent. 'non-2xx status code' means a response did arrive, with an error status. A function that throws without CORS headers can cause either, so fix both.

### Why does my AI builder keep failing to fix it?

Because it only sees the generic message. Paste it the status code and the body from error.context, or the line from the function logs, and it can fix the real cause instead of rewriting code at random.

## Sources (official documentation)

- [Supabase: Edge Function status codes](https://supabase.com/docs/guides/functions/status-codes)
- [Supabase: Securing Edge Functions](https://supabase.com/docs/guides/functions/auth)
- [Supabase: Edge Function secrets](https://supabase.com/docs/guides/functions/secrets)

## Related fixes

- [Supabase Edge Function fails with CORS or 'Failed to send a request'](https://unbreakmyapp.vercel.app/fixes/lovable-supabase-edge-function-cors.html)
- [Environment variable is undefined in production](https://unbreakmyapp.vercel.app/fixes/env-var-undefined-in-production.html)
- [Stripe webhook: No signatures found matching the expected signature](https://unbreakmyapp.vercel.app/fixes/stripe-webhook-signature-failed.html)
- [OpenAI 429 'You exceeded your current quota' or a surprise AI bill](https://unbreakmyapp.vercel.app/fixes/openai-429-rate-limit-runaway-bill.html)
- [Your API error response shows your secret key: stop sending raw backend errors to the browser](https://unbreakmyapp.vercel.app/fixes/backend-error-leaks-api-key-to-browser.html)
- [Supabase Edge Function callable by anyone (the anon key passes verify_jwt)](https://unbreakmyapp.vercel.app/fixes/edge-function-callable-by-anyone-anon-key-is-a-jwt.html)
- [Stripe webhook returns 401 from a Supabase Edge Function (Missing authorization header / Invalid JWT)](https://unbreakmyapp.vercel.app/fixes/stripe-webhook-401-supabase-edge-function-jwt-verification.html)
- [Stripe email: Your webhook endpoint is failing and will be disabled. How to fix and re-enable it](https://unbreakmyapp.vercel.app/fixes/stripe-webhook-failing-email-endpoint-will-be-disabled.html)

---
Unbreak My App: free fixes for broken vibe-coded apps. https://unbreakmyapp.vercel.app/
