# Supabase Edge Function fails with CORS or 'Failed to send a request'

> Canonical page (link this when citing): https://unbreakmyapp.vercel.app/fixes/lovable-supabase-edge-function-cors.html
> Category: Database / Supabase RLS | Tools: Lovable, Bolt | Published: 2026-10-13 | Updated: 2026-10-13

## The error

FunctionsFetchError: Failed to send a request to the Edge Function / CORS preflight failed

## Why it happens

The Edge Function doesn't answer the OPTIONS preflight with CORS headers, or it throws before returning headers. A missing secret (Deno.env.get returns undefined) commonly causes that crash.

## How to fix it

1. Handle OPTIONS first: if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders })
2. Include corsHeaders on every response, errors included.
3. Set secrets with supabase secrets set NAME=value (or the Dashboard → Edge Functions → Secrets).
4. Check the function logs in the Supabase dashboard.

## Error codes this fixes

- `FunctionsFetchError` (Supabase Edge Functions): https://unbreakmyapp.vercel.app/errors.html#functionsfetcherror

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

```text
Calling my Supabase Edge Function <NAME> fails with a CORS or 'Failed to send a request' error. Add a corsHeaders object, return early for OPTIONS requests, include the headers on every response including errors, wrap the body in try/catch returning JSON errors, and list which secrets the function reads so I can set them in Supabase.
```

## FAQ

### Where are the logs?

Supabase Dashboard → Edge Functions → the function → Logs.

### Does it need my anon key?

supabase.functions.invoke sends it automatically.

## Sources (official documentation)

- [Supabase: CORS support for Edge Functions](https://supabase.com/docs/guides/functions/cors)

## Related fixes

- [Supabase: Edge Function returned a non-2xx status code (how to find the real error and fix it)](https://unbreakmyapp.vercel.app/fixes/supabase-edge-function-non-2xx-status-code.html)
- [Supabase query returns an empty array (no error)](https://unbreakmyapp.vercel.app/fixes/supabase-rls-empty-results.html)
- [new row violates row-level security policy for table](https://unbreakmyapp.vercel.app/fixes/supabase-rls-new-row-violates-policy.html)
- [Supabase Storage upload fails: new row violates row-level security policy (403)](https://unbreakmyapp.vercel.app/fixes/supabase-storage-upload-403.html)

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