Thomas Osayi

November 22, 2026 · 8 min read · Thomas Osayi

Supabase edge function errors: CORS, 401 Invalid JWT, missing secrets and timeouts

How to read and fix the most common Supabase edge function errors in an AI-built app: CORS failures, 401 Invalid JWT, undefined secrets, 546 limits and silent 500s.

Edge functions are where AI builders put the parts of an app that cannot run in the browser: calling OpenAI, creating a Stripe checkout, sending email, talking to a third party API with a secret key. They are also where a lot of AI-built apps quietly fail. The button spins, the console says "Failed to send a request to the Edge Function" or "blocked by CORS policy", and the prompt to fix it produces a new version with the same problem.

The good news is that the errors fall into a few groups, and Supabase gives you enough logging to tell which one you have. Here is how I work through them.

How do I find out what my edge function error actually is?

Open the function's Logs tab in the Supabase dashboard before changing any code, because the browser error is often not the real one.

In the dashboard, go to Edge Functions, select the function and open Logs. Supabase documents that this view shows invocations and console logs in one list, along with boot and shutdown events and uncaught exceptions. Each invocation shows its status code and timing. If an invocation is missing entirely, the request never reached your code, which points to the URL, the deployment or the JWT check.

From the terminal, the Supabase CLI has a unified logs command. Running supabase logs with the remote flag and the functions service shows your console output, and the function-edge service shows invocation events. Flags such as search and since narrow it down, for example to messages containing "error" in the last 30 minutes.

Two details from the logging docs matter:

  • Invocation logs do not contain request or response bodies. If you need to know what came in, log it yourself with console.log, leaving out passwords, tokens and personal data.
  • A single log message can be up to 10,000 characters, and a function can log up to 100 events in 10 seconds, so do not log inside tight loops.

Why does my edge function fail with a CORS error?

Because the function is not answering the browser's OPTIONS preflight, or because some of its responses leave out the CORS headers.

When a web app calls a function on a different domain with an Authorization header, the browser first sends an OPTIONS request asking for permission. Your function has to answer that with an ok response and the right Access-Control headers. If it does not, the browser never sends the real request.

Supabase's troubleshooting guide lists the common causes: a missing Access-Control-Allow-Origin header, unhandled OPTIONS requests, invalid or expired JWTs, missing Authorization headers, functions that were never deployed, and wrong function URLs. For the headers themselves, it recommends importing corsHeaders from @supabase/supabase-js/cors on version 2.95.0 or later. On older versions, define them in a shared file such as _shared/cors.ts, allowing the authorization, x-client-info, apikey and content-type headers.

The fix has two parts:

  • At the very top of the handler, if the request method is OPTIONS, return a response with the CORS headers and nothing else.
  • Include the CORS headers in every other response, including errors and the catch block.

The second part is the one AI-generated functions miss most. The happy path returns JSON with CORS headers. A thrown error returns a plain 500 without them. The browser blocks that response and reports a CORS error, so you spend an hour on CORS when the real problem was a missing secret.

Why am I getting a 401 Invalid JWT or Missing authorization header?

Because the platform's built-in JWT check rejected the request before your code ran, or because your own code returned a 401.

Supabase's 401 guide gives a simple test: read the response body. If it says "Invalid Token" or "Missing authorization header", the platform check rejected it. If the body is your own message or empty, your code returned the 401, so search the function for 401 and trace the condition.

For platform rejections, the guide names three causes:

  • Missing Authorization header. The supabase-js client adds it automatically when you call functions.invoke. A plain fetch or cURL call must add it.
  • Invalid key. The request carries a key or token the check does not accept.
  • Incompatible keys. Projects on the newer asymmetric signing keys can fail the legacy check. The documented fix is to turn off JWT verification for the function in the dashboard, redeploy with the no-verify-jwt flag in the CLI, or set it through the Management API.

JWT verification is on by default. The auth docs show that you can turn it off per function in supabase/config.toml with a functions section for that function name and verify_jwt set to false.

Turning it off is correct for functions that outside services call, like a Stripe webhook, because Stripe does not have a Supabase token. It is not a fix for user-facing functions on its own. If you turn it off, your function must check the caller itself, either by verifying the user's token with the Supabase client or by checking a webhook signature. Otherwise anyone who finds the URL can call it.

Why is my secret undefined in production?

Because local secrets in a .env file are not uploaded when you deploy, so the hosted function has nothing to read.

Edge functions read secrets with Deno.env.get and the secret's name. Some come built in. At the time of writing the docs list SUPABASE_URL, SUPABASE_DB_URL, SUPABASE_ANON_KEY and SUPABASE_SERVICE_ROLE_KEY among them, plus newer variables for the publishable and secret key format. Anything else, such as STRIPE_SECRET_KEY or OPENAI_API_KEY, you must set yourself.

The commands from the Supabase docs:

  • supabase secrets set STRIPE_SECRET_KEY=your_value sets one secret.
  • supabase secrets set with the env-file flag pointing at a file sets many at once.
  • supabase secrets list shows which names exist, without the values.

For local development, values go in supabase/functions/.env, which should be in .gitignore. The docs add two rules worth knowing: secret names cannot start with SUPABASE_, and only project Owners or Administrators can create or delete secrets. Supabase also says no redeploy is needed, since functions read a new secret immediately.

The classic symptom is an error like "Neither apiKey nor config.authenticator provided" from the Stripe library, or a 401 from OpenAI, both meaning the key arrived as undefined. Check the names match exactly, including case.

Why does my function time out or return a 546?

Because it hit one of the runtime limits, and the shutdown reason in the logs tells you which.

At the time of writing the Supabase limits page lists a maximum of 256MB of memory, 2 seconds of CPU time, a wall clock duration of 150 seconds on the free plan and 400 seconds on paid plans, and a request idle timeout of 150 seconds. A 546 response with WORKER_LIMIT or WORKER_RESOURCE_LIMIT means one of those was exceeded.

CPU time is time spent computing, not waiting. Waiting on a fetch to OpenAI does not use much CPU. Parsing a large CSV, resizing images or looping over thousands of rows does. Wall clock time is total elapsed time, so a slow third party API can hit it even when CPU is low.

Fixes, depending on which limit:

  • Memory or CPU: process data in smaller batches, move heavy work like image processing to a dedicated service, and avoid loading whole tables into memory.
  • Wall clock: return a response quickly and let the slow work finish separately, for example by writing a job row and processing it with a scheduled function.
  • Idle timeout: stream the response for long AI generations so the connection is never idle.

Why does it work locally but fail after deploying?

Usually because the deployed version is different from what you are running locally, or the app is calling the wrong URL.

Check these in order:

  • Was the latest version deployed? Lovable and Bolt deploy functions for you, but code edited in GitHub or Cursor often needs supabase functions deploy with the function name.
  • Is the app calling the right project? A hard-coded project URL from an old project is common after remixing an app.
  • Is the function name exactly the same? Calls are case sensitive.
  • Are imports reachable? A package that resolved locally may fail at boot in the hosted runtime, which appears as a boot error in the logs.
  • Are secrets set in the hosted project, as covered above?

Also remember that a function using the service role key bypasses row level security entirely. That is sometimes needed, but it means the function must decide who is allowed to do what. "Is your Lovable or Bolt app leaking data? Check Supabase row level security" covers the policies side.

A worked example

Here is an illustration with a made-up app. A meal planning app built in Lovable has a generate-plan function that calls OpenAI. Users report that the generate button spins and then shows nothing. The browser console says the request was blocked by CORS policy.

The Logs tab tells a different story. Every invocation returns a 500, and the console output shows "OPENAI_API_KEY is not set". The founder had added the key in a local .env file while testing in Cursor, and it never reached the hosted project. The function's catch block returned a plain error with no CORS headers, so the browser reported CORS.

The fix: set the secret with supabase secrets set, add CORS headers to the catch block, add an OPTIONS handler at the top, and add a console.error before each error response. Generation then takes around 40 seconds, under the wall clock limit, so streaming can wait.

When should I get help?

If the logs show an error you cannot explain, or you have turned off JWT verification to make something work and are not sure the function is still protected, that is the time to get another set of eyes on it.

I fix single broken functions as a Quick Fix, from $60 in two days. When the edge function is one of several launch blockers, Fix Plus Features from $200 covers up to three fixes and one small feature. The "fix your Lovable app" page lists the other problems I usually check alongside.

The bottom line

Read the Logs tab before you touch the code. A CORS error usually means a missing OPTIONS handler or an error path without CORS headers. A 401 with Invalid Token or Missing authorization header comes from the platform check. An undefined secret means it was never set in the hosted project. A 546 means a CPU, memory or time limit. Fix the cause the logs point to, log the reason before every error, and do not turn off JWT verification without adding your own check.

Questions, answered in one line

Where do I find Supabase edge function logs?

In the Supabase dashboard, open Edge Functions, select the function and open the Logs tab, which lists invocations and console output together.

Why do I get a CORS error only when the function fails?

Your success path probably adds CORS headers but your error path does not, so the browser blocks the error response and reports a CORS problem instead of the real error.

Do I need to redeploy after setting a Supabase secret?

No, Supabase documentation says functions read a newly set secret immediately, so you only need to call the function again.

How do I let Stripe or another service call my edge function?

Turn off JWT verification for that one function, because Stripe does not send a Supabase token, and verify the request inside the function using the Stripe signature instead.

What does a 546 error mean?

A 546 response means the function hit a resource limit such as memory or CPU time, and the shutdown reason in the logs says which one.

Can someone fix a broken edge function for me?

Thomas Osayi fixes a single broken edge function through his Quick Fix package, from $60 with a two day turnaround.

Sources

About the author

Thomas Osayi is a full-stack developer in Los Angeles studying computer science at Loyola Marymount University. He founded and built SQWAD, a SaaS platform Shopify brands use to run creator programs, and builds custom Next.js websites, SaaS MVPs and fixes for AI-built apps for businesses and founders.

See his work · Services and prices

Related articles

Fix, finish and launch your AI-built app

Built your app with Lovable, Bolt, v0, Replit or Cursor and got stuck? I fix the bugs, connect auth and payments, clean up the code and deploy it to production.

See the service