Edge Function returned a non-2xx status code
Last verified:
The fix
Edge Function returned a non-2xx status code is the supabase-js wrapper. The status and the response body are on error.context. Read those before you change the function.
- 1
Print the status and the body
Branch on
FunctionsHttpErrorbefore you edit the function. Logerror.context.statusand the body. Usetext()so a non-JSON body still prints. The docs usejson()when you already know the function returned JSON. Do not replace this with an emptycatch, and do not treatdata === nullas success.FunctionsFetchErrormeans there was no readable response. If the browser says the preflight did not pass, use the CORS page after you have the status.import { FunctionsHttpError } from "@supabase/supabase-js"; const { data, error } = await supabase.functions.invoke("hello"); if (error instanceof FunctionsHttpError) { const status = error.context.status; const raw = await error.context.text(); console.log(status, raw); } - 2
Match 401 to the check that actually ran
If the body is
Missing authorization header,Invalid JWT, orInvalid Token or Protected Header formatting, your handler did not run. Call the function withsupabase.functions.invokeso the session JWT and theapikeyheader are sent. A rawfetchhas to sendAuthorizationitself whileverify_jwtis on. For a webhook or another caller with no user JWT, turn the check off for that function only: the detail tab in the Functions dashboard,verify_jwt = falseunder that function inconfig.toml, orsupabase functions deploy <name> --no-verify-jwt. Then check the credential in code. Do not turn the check off on a user-only function and skip a replacement check. Do not log the service role or the secret key. - 3
Answer OPTIONS, and return the real error with CORS headers
Prefer
withSupabasefromnpm:@supabase/server, which answersOPTIONSand adds CORS headers. Otherwise return onOPTIONSbefore reading the body, and putcorsHeaderson the success response and on every error response. Import them fromnpm:@supabase/supabase-js@^2/corson supabase-js v2.95.0 or later. The header list has to includeauthorization,x-client-info,apikey, andcontent-type. On a thrown error, log it and return JSON with the message and a 4xx or 500. A bare 500 with no CORS headers is why the browser hides the body you just added.if (req.method === "OPTIONS") { return Response.json({ ok: true }, { headers: corsHeaders }); } - 4
Read the logs, then fix the status you printed
In the dashboard, open the function and read Invocations for the status code and Logs for
consoleoutput and uncaught exceptions. 500 is a throw in your code. 503 is a boot failure in the version you deployed. 504 is a timeout. 546 is a resource limit. An emptyDeno.env.getis a secret that is not in Edge Function Secrets. Set it there, or withsupabase secrets set. Do not print the value. You do not redeploy just to pick up a secret. You do deploy code:supabase functions deploy <name>. On Lovable's built-in backend, ask Lovable to deploy the function. A file saved in git is not that deploy.
Why it happens
supabase.functions.invoke does not throw, and it does not put a failed response in data. When the HTTP status is outside 200-299, the client returns FunctionsHttpError with the message Edge Function returned a non-2xx status code. The docs' client example reads the function's own payload with error.context.json(). error.context is the Response. error.context.status is the code that was hidden, and error.context.text() is the body when it is not JSON. A json() call on a plain-text body throws, which hides the real error a second time. FunctionsRelayError is a relay failure (x-relay-error: true). FunctionsFetchError means the request never produced a response the client could read. The message on that one is not this sentence.
The status tells you who answered. A 401 body of Invalid JWT, Missing authorization header, or Invalid Token or Protected Header formatting is the platform JWT check. The troubleshooting guide says that check runs before your code, so a failure there never starts the function. verify_jwt is on by default. invoke sends the session JWT on Authorization and the project's key on apikey. A hand-written fetch that omits Authorization gets Missing authorization header while the check is on. The auth-headers guide says the check expects a user JWT, and that a publishable or secret key is not a JWT. The check still accepts those keys on Authorization or apikey for migration, and accepting the key is not the same as authenticating the caller. The 401 troubleshooting page says the legacy check does not understand the new asymmetric signing keys, and its fix is to turn the check off and authorize in the function. Do that for a webhook or any caller that has no user JWT. Leave the check on for a function that should only run when invoke sends a signed-in session, and fix the missing session instead of shipping --no-verify-jwt as the patch.
A browser call sends a preflight when the request has a JSON body or headers such as authorization, apikey, or content-type. The CORS guide says the function has to answer OPTIONS before other work. withSupabase from npm:@supabase/server does that. If you do it yourself, return on OPTIONS before you read the body. A handler that parses JSON first throws on the preflight, and that throw becomes a 500. The platform check still runs first. An OPTIONS request with no Authorization header can come back as Missing authorization header from the platform, and your OPTIONS branch never runs. The browser then reports that the preflight failed. That browser sentence is the CORS page. This sentence is the invoke error, and the status on error.context says whether you are looking at that 401 or at a response your code returned. Every response the browser will read, including the error, needs the CORS headers. A catch that returns a bare 500 fails that check even after you fixed OPTIONS.
A 500 with WORKER_ERROR is an exception your function did not catch. The status-codes guide names unhandled errors, a failed async call, and invalid JSON. The body is useful only if you return it. console.error is what shows up in the Edge Function logs. A 503 BOOT_ERROR means the deployed function failed to start: a syntax error, a bad import, or bad config. A 504 means it did not answer before the timeout. A 546 means WORKER_RESOURCE_LIMIT (the logs name the limit; it used to be reported as WORKER_LIMIT). Those three are what a bad deploy looks like. They are not fixed by catching the client error and showing success. A 404 means that function name is not deployed. A secret the code reads with Deno.env.get comes back empty when it was never stored. The production value is set in the dashboard under Edge Function Secrets, or with supabase secrets set. A name cannot start with SUPABASE_. The secrets guide says a deployed function reads a new secret immediately, so a missing secret is not fixed by redeploying. Code changes are. supabase functions deploy <name> ships the function. On a Lovable project that uses the built-in backend, a commit that edits supabase/functions does not deploy it. Ask Lovable to deploy the function, then look under More, then Database, then Manage, then Edge functions. A secret typed into chat is not stored. Do not put it in a VITE_ variable.
The next error you'll probably hit
Once this is fixed, the usual follow-up is Response to preflight request doesn't pass. Response to preflight request doesn't pass access control check on a Supabase Edge Function. Answer OPTIONS and send CORS headers on errors too.
Prompt to paste back into your AI tool
Paste this above the error.
supabase.functions.invoke fails with: Edge Function returned a non-2xx status code The function name is <name>. error.context.status is <status or "not printed yet">. The body is: <paste error.context text, or "not printed yet"> Do not empty-catch this error. Do not return success when data is null. Do not turn off verify_jwt on a function that should require a signed-in user unless you add a real check in the function. Do not put the service role, secret key, or any secret in a NEXT_PUBLIC_ or VITE_ variable. Do not log the secret value. 1. Show the invoke call. If the status and body are missing, add the FunctionsHttpError branch that prints error.context.status and await error.context.text(). Stop there if I have not pasted them yet. 2. If the body is Missing authorization header, Invalid JWT, or Invalid Token or Protected Header formatting, say the platform check rejected the request and my code did not run. Tell me to use invoke, or to send Authorization. Turn verify_jwt off only for a webhook or a caller with no user JWT, and name the function. 3. If the status is 500, show the throw and return JSON with cors headers. If the browser blocked the preflight, answer OPTIONS before reading the body. Point me at the CORS page for the header list. 4. If the status is 503, 504, or 546, tell me to read the function logs. 503 is a boot error in the deployed source. Do not invent a client retry loop. 5. If the body says a secret is missing, tell me to set it in Edge Function Secrets or with supabase secrets set. On a Lovable built-in backend, tell me to ask Lovable to deploy the function. Change only this function and the call site.
Common questions
+Where is the real error? The only text I have is Edge Function returned a non-2xx status code.
That sentence is FunctionsHttpError. The Response is error.context. Read status and text() (json() when the function returned JSON). data stays null on purpose. A different message, Failed to send a request to the Edge Function, means the client got no readable response.
+The logs are empty and the status is 401. Did the function crash?
A body of Invalid JWT or Missing authorization header is the platform check. Your code did not run, so there is no function log line from the handler. invoke sends the session JWT. A fetch that omits Authorization fails this check while verify_jwt is on.
+I added the secret and redeployed. Why is Deno.env.get still empty?
A production secret is read as soon as it is saved in Edge Function Secrets or set with supabase secrets set. Redeploying does not create it. The name cannot start with SUPABASE_. Chat text and a VITE_ variable are not that secret. A code change still needs supabase functions deploy, or a Lovable deploy on the built-in backend.
+The function works in curl and the browser says the preflight failed. Is that this error?
curl does not send the OPTIONS preflight. If invoke still reports this sentence, print the status. A 401 from the platform on OPTIONS never reaches your OPTIONS branch. A 500 means the handler threw, often by reading JSON on the preflight. The browser wording is the CORS page. Answer OPTIONS first, and put the same CORS headers on the error response.
Related errors
- Response to preflight request doesn't passResponse to preflight request doesn't pass access control check on a Supabase Edge Function. Answer OPTIONS and send CORS headers on errors too.CursorLovableBoltSupabaseReactOct 19, 2026
- JWT expiredJWT expired. PostgREST code PGRST301. The access token on this request is past its exp. Let the client refresh the session. A dead refresh token is a different error.CursorLovableBoltSupabaseAuthOct 27, 2026
- Auth session missing after refreshAuth session missing: sign-in worked in the browser, but Next.js cannot see the Supabase cookie. Use the @supabase/ssr server client and refresh.CursorLovableBoltSupabaseAuthOct 14, 2026
- NEXT_PUBLIC_ secret in the bundleA NEXT_PUBLIC_ or VITE_ secret is in the browser bundle. Rotate the key, move the call to a server route, and keep real public keys in the client.CursorLovableBoltNext.jsViteNov 11, 2026