apps/docs/content/troubleshooting/unable-to-call-edge-function.mdx
If you're having trouble invoking an Edge Function or experiencing CORS issues, follow these steps to diagnose and resolve the problem.
Make sure your function handles OPTIONS preflight requests.
For @supabase/supabase-js v2.95.0 and later: Import CORS headers directly from the SDK to ensure they stay synchronized with any new headers added to the client libraries.
// Recommended approach
import { corsHeaders } from '@supabase/supabase-js/cors' // v2.95.0+
Deno.serve(async (req) => {
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders })
}
// Your function logic here
return new Response(JSON.stringify({ status: 'Success' }), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
})
})
If you're using @supabase/supabase-js before v2.95.0, you'll need to hardcode the CORS headers. Add a cors.ts file within a _shared folder:
// _shared/cors.ts
export const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}
Then import it in your function:
import { corsHeaders } from '../_shared/cors.ts'
Deno.serve(async (req) => {
// Handle CORS preflight requests
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders })
}
// Your function logic here
return new Response(JSON.stringify({ status: 'Success' }), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
})
})
Supabase provides two debugging tools for Edge Functions:
Access these tools by navigating to Functions > [Your Function] in the dashboard.
Access-Control-Allow-Origin header in response