Blog ·

Checkout or sign-up button fails with a CORS error? Fix your Supabase Edge Function

You built your app with Lovable, Bolt, Cursor or Replit. Part of it runs in a Supabase Edge Function: often the step that creates a Stripe checkout, sends a welcome email or finishes sign-up. Someone clicks the button and nothing happens. The browser console shows an error that mentions CORS.

A common cause is that the function does not answer the browser's safety check before the real request, or does not send the right headers back. The fix is a few lines in the function, then a redeploy.

The short answer

Before a browser calls your Edge Function from your site, it first sends an OPTIONS request, called a preflight, to ask whether the call is allowed. Supabase's docs say that to call Edge Functions from the browser you need to handle these preflight requests. Your function has to reply to OPTIONS with CORS headers, and include the same headers on every other response it sends, errors included. If it doesn't, the browser blocks the response and your button quietly fails.

How to tell if this is your problem

Open your live site, open the developer tools (F12 on most computers), go to the Console tab and click the button that fails. Look for:

  • an error that mentions CORS. Firefox, for example, shows Cross-Origin Request Blocked. The wording differs between browsers.
  • the address of the blocked request. Edge Functions live at an address like https://<project_id>.supabase.co/functions/v1/<function-name>, so the last part tells you which function it is.
  • if your app calls the function with the Supabase library, the error Failed to send a request to the Edge Function. The library uses this for a network error while calling your function, which is what a blocked request looks like to your app.

MDN, the web's main reference for browser behaviour, notes that the details of a CORS failure are hidden from your app's code for security reasons. The console is the only place to see them, so check it rather than any error your app shows on screen.

CORS is not always the real problem

Supabase warns that if anything goes wrong in your function before the CORS part runs, the browser may falsely report CORS as the reason. A crash in the function can look exactly like a CORS error. So check the function's logs before you change any code:

  1. In your Supabase dashboard, open Edge Functions and select the function.
  2. Open Invocations to see each request and response, with status codes. Open Logs to see errors and anything the function printed.

Supabase gives one clue to look for: without an OPTIONS handler, a browser call can show up in the logs as SyntaxError: Unexpected end of JSON input. If you see a different error instead, such as a TypeError, fix that first, because it is what is really stopping your button.

Fix it in four steps

1. Answer the preflight first

At the very top of the function's handler, before anything else runs, check for an OPTIONS request and reply straight away with the CORS headers. Supabase's troubleshooting guide shows this pattern:

  • import the headers: import { corsHeaders } from 'npm:@supabase/supabase-js@^2/cors'
  • first line inside the handler: if (req.method === 'OPTIONS') { return new Response('ok', { headers: corsHeaders }) }

Supabase recommends importing corsHeaders from the Supabase library (version 2.95.0 or later), so the list of allowed headers stays in step with the headers the library sends. On an older version, Supabase's guide shows keeping your own list in a _shared/cors.ts file instead. Supabase also offers a withSupabase wrapper from @supabase/server that handles CORS and preflight for you.

2. Add the headers to every response, including errors

Every new Response(...) the function returns should include the CORS headers, for example headers: { ...corsHeaders, 'Content-Type': 'application/json' }. That includes the responses in error paths. Supabase's own examples add them to error responses too. Otherwise a failed checkout shows up as a confusing CORS error instead of the real message.

3. Deploy the function again

Your fix is not live until the function is deployed again. Supabase's deploy guide uses the Supabase CLI command supabase functions deploy <function-name>. If your AI tool manages your functions, ask it to deploy the updated function.

4. Test on the live site

Open your live site in a private browser window with the Console open. Click the button again and check that the CORS error has gone and the step completes. Then look at the function's Invocations in Supabase to confirm the request arrived.

A note on the allowed origin

Supabase's examples allow any site to call the function, using 'Access-Control-Allow-Origin': '*'. MDN warns that using the wildcard for a private API is a bad idea and suggests naming your own site as the header's value instead, for example https://yourapp.com. If you restrict it, make sure the address matches your live site exactly, or the browser will block calls from your site.

A prompt you can paste into your AI tool

"When I click the checkout (or sign-up) button on my live site, the browser console shows a CORS error for my Supabase Edge Function called [function name]. Update only that function so it answers OPTIONS preflight requests first with the CORS headers from @supabase/supabase-js/cors, and includes those headers on every response, including error responses. Don't change anything else, and tell me how to redeploy the function."

Then check the function's logs and test the button again on the live site.

Catch silent breaks before your users do

This kind of break is easy to miss because the page itself still loads fine. Scheduled testing from FlowCanary is launching by 30 November 2026. Once it is live, it will test the flows a new visitor can do in your app, such as sign-up and checkout, in a real browser on a schedule, with logged-in flows on Pro, and tell you in plain English when one breaks, with a fix prompt for Lovable, Bolt, Cursor or Replit. Until then, the free check works today: paste your live URL and it opens the page in a real browser and reports JavaScript errors, failed requests and blank pages.

Sources

Run the free checker