What it means
Before a cross-origin POST with custom headers, the browser sends an OPTIONS preflight asking “may I send these headers?”. Edge Functions don't answer CORS for you: your code must reply to OPTIONS and list every header the client will send. supabase-js sends more than you might think: authorization, apikey, x-client-info and content-type on normal calls, x-retry-count on automatic retries in newer versions, and traceparent, tracestate, baggage when trace propagation is turned on. If one is missing from your list, the browser blocks the request and your function never sees it.
Fix: use the SDK's header list (supabase-js 2.95.0 or later)
Supabase's CORS guide recommends importing the headers from the SDK, so the list stays in sync when the client starts sending a new header:
import { corsHeaders } from 'npm:@supabase/supabase-js@^2/cors';
Deno.serve(async (req) => {
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders });
}
try {
const { name } = await req.json();
return Response.json({ message: `Hello ${name}!` }, { headers: corsHeaders });
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
return Response.json({ error: message }, { status: 400, headers: corsHeaders });
}
});Or write the list yourself (older SDK versions)
// supabase/functions/_shared/cors.ts
export const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers':
'authorization, x-client-info, apikey, content-type, x-retry-count, traceparent, tracestate, baggage',
'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
};This is the list from Supabase's CORS guide. If you add your own header (say x-request-id), add it here too.
Other CORS messages and what they really mean
No 'Access-Control-Allow-Origin' header is present on the requested resourceon the actual request (not the preflight): your function returned a response without the CORS headers. Usually an error path: a thrown exception, a 401 or 500 built withoutheaders: corsHeaders. Add the headers to everyResponse.Response to preflight request doesn't pass access control check: It does not have HTTP ok status: theOPTIONSrequest got a 4xx/5xx. If it is a 404, the function name or project is wrong (see the related guide below).Method PUT is not allowed by Access-Control-Allow-Methods: add the method to the list.The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include': you setcredentials: 'include'onfetch. Either drop it (Supabase auth uses the Authorization header, not cookies) or return your exact origin instead of*.
Lock the origin down in production
* is fine while developing. For production, answer only your own origins:
const ALLOWED = new Set(['https://app.example.com', 'http://localhost:3000']);
function cors(req: Request) {
const origin = req.headers.get('Origin') ?? '';
return {
...corsHeaders,
'Access-Control-Allow-Origin': ALLOWED.has(origin) ? origin : 'https://app.example.com',
Vary: 'Origin',
};
}CORS only controls which browsers may read the response. It is not authentication: curl ignores it. Your function still has to check who is calling.
How to verify
curl -i -X OPTIONS https://<project-ref>.supabase.co/functions/v1/hello \
-H "Origin: http://localhost:3000" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: authorization, x-client-info, apikey, content-type"Expect a 200 whose Access-Control-Allow-Headers contains every header you asked about. Then check the real request in the browser's Network tab: its response must carry Access-Control-Allow-Origin too, also when it is an error.
Related guides
- Supabase Edge Function returns 404 NOT_FOUND on the CORS preflight
- Supabase Edge Function 401 “Invalid JWT” or “Missing authorization header”
- Supabase cron (pg_cron) job not running – a checklist with the SQL for each step
- Supabase Database Webhooks and pg_net requests that never arrive – how to debug them
More in Edge Functions, webhooks and APIs · all guides