Supabase Edge Function CORS errors from the browser – headers, preflight and error responses

By Abheesht Tripathi · Published

Access to fetch at 'https://<ref>.supabase.co/functions/v1/hello' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field x-client-info is not allowed by Access-Control-Allow-Headers in preflight response.

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

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

More in Edge Functions, webhooks and APIs · all guides