Skip to main content

Overview

Supabase Edge Functions are serverless functions that run close to your users. They’re used for:
  • Webhook handlers
  • Background processing
  • API endpoints that need server-side logic
Base URL: https://ymdccxqzmhxgbjbppywf.supabase.co/functions/v1

Authentication

Edge Functions can be called with:
  1. Anon Key (public, RLS enforced)
  2. Service Role Key (admin, bypasses RLS)
curl -H "Authorization: Bearer YOUR_SUPABASE_ANON_KEY" \
  https://ymdccxqzmhxgbjbppywf.supabase.co/functions/v1/function-name

Available Functions

shopify-webhook

Handles Shopify webhooks for order and product sync. Endpoint: POST /functions/v1/shopify-webhook Headers:
X-Shopify-Hmac-Sha256: [signature]
X-Shopify-Topic: orders/create
Supported Topics:
  • orders/create
  • orders/updated
  • products/create
  • products/update
  • products/delete

scrape-instagram

Triggers Instagram scraping for a specific source. Endpoint: POST /functions/v1/scrape-instagram Body:
{
  "source_id": "uuid-of-ig-source",
  "limit": 10
}
Response:
{
  "scraped": 10,
  "source": "beautifuldestinations",
  "errors": []
}

generate-article

Generates an article from scraped content. Endpoint: POST /functions/v1/generate-article Body:
{
  "ig_post_ids": ["uuid1", "uuid2"],
  "vertical_id": "uuid-of-vertical",
  "style": "listicle"
}
Response:
{
  "article_id": "uuid-of-new-article",
  "title": "10 Amazing Travel Destinations",
  "status": "draft"
}

sync-viator

Syncs Viator affiliate products for a destination. Endpoint: POST /functions/v1/sync-viator Body:
{
  "destination": "Paris",
  "limit": 50
}
Response:
{
  "synced": 47,
  "destination": "Paris",
  "products": [...]
}

Creating New Functions

1. Create Function

supabase functions new my-function

2. Write Handler

// supabase/functions/my-function/index.ts
import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'

serve(async (req) => {
  try {
    const supabase = createClient(
      Deno.env.get('SUPABASE_URL')!,
      Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
    )
    
    const { data, error } = await supabase
      .from('table_name')
      .select('*')
    
    if (error) throw error
    
    return new Response(
      JSON.stringify({ data }),
      { headers: { 'Content-Type': 'application/json' } }
    )
  } catch (error) {
    return new Response(
      JSON.stringify({ error: error.message }),
      { status: 500 }
    )
  }
})

3. Deploy

supabase functions deploy my-function --project-ref ymdccxqzmhxgbjbppywf

Environment Variables

Set secrets for functions:
supabase secrets set MY_SECRET=value --project-ref ymdccxqzmhxgbjbppywf
Access in function:
const mySecret = Deno.env.get('MY_SECRET')

Local Development

# Start local Supabase
supabase start

# Serve functions locally
supabase functions serve

# Test locally
curl http://localhost:54321/functions/v1/my-function

Logs

View function logs:
# Via CLI
supabase functions logs my-function --project-ref ymdccxqzmhxgbjbppywf

# Or in Dashboard
# Supabase Dashboard → Edge Functions → [function] → Logs

Best Practices

Error Handling

try {
  // Function logic
} catch (error) {
  console.error('Function error:', error)
  return new Response(
    JSON.stringify({ error: error.message }),
    { status: 500, headers: { 'Content-Type': 'application/json' } }
  )
}

CORS

const corsHeaders = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}

// Handle OPTIONS preflight
if (req.method === 'OPTIONS') {
  return new Response('ok', { headers: corsHeaders })
}

Database Access

// Use service role for server-side operations
const supabaseAdmin = createClient(
  Deno.env.get('SUPABASE_URL')!,
  Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
)

// Use user's JWT for RLS-enforced queries
const authHeader = req.headers.get('Authorization')!
const supabase = createClient(
  Deno.env.get('SUPABASE_URL')!,
  Deno.env.get('SUPABASE_ANON_KEY')!,
  { global: { headers: { Authorization: authHeader } } }
)

Pricing

Edge Functions are billed by:
  • Invocations: First 500K/month free
  • Execution time: First 500K GB-seconds free
See Supabase Pricing for details.