Skip to content

Edge Functions Setup Guide

This guide explains how to run and test Supabase Edge Functions locally and deploy them to production.

  1. Supabase CLI - Install the Supabase CLI:

    Terminal window
    npm install -g supabase
    # or
    brew install supabase/tap/supabase
  2. Docker - Required for running Edge Functions locally:

  3. Deno (Optional) - For better development experience:

    Terminal window
    curl -fsSL https://deno.land/install.sh | sh

Edge Functions are located in /supabase/functions/:

supabase/
├── functions/
│ ├── api-jobs/
│ │ └── index.ts
│ ├── api-parts/
│ │ └── index.ts
│ ├── api-export/ # NEW: Data export function
│ │ └── index.ts
│ └── ... (other functions)
└── config.toml

First, ensure you’re in the project root directory:

Terminal window
cd /home/user/eryxon-flow

Initialize Supabase (if not already done):

Terminal window
supabase init

Start the local Supabase stack:

Terminal window
supabase start

This will start:

  • PostgreSQL database
  • Authentication service
  • Edge Functions runtime
  • Storage service
  • Realtime service

Note: The first run may take several minutes as it downloads Docker images.

To run all Edge Functions locally:

Terminal window
supabase functions serve

To run a specific function:

Terminal window
supabase functions serve api-export

To run with environment variables:

Terminal window
supabase functions serve --env-file .env.local

Local Edge Functions will be available at:

http://localhost:54321/functions/v1/[function-name]

For example:

  • Jobs API: http://localhost:54321/functions/v1/api-jobs
  • Data Export: http://localhost:54321/functions/v1/api-export

You can test the new data export function using curl:

Terminal window
TOKEN="your-local-supabase-anon-key"
curl -X GET \
"http://localhost:54321/functions/v1/api-export?entities=jobs,parts&format=json" \
-H "Authorization: Bearer $TOKEN"

Get your local anon key: When you run supabase start, it displays the anon key. You can also get it with:

Terminal window
supabase status
Terminal window
supabase login
supabase link --project-ref your-project-ref

Find your project ref:

  • Go to your Supabase dashboard: https://app.supabase.com
  • Select your project
  • Project ref is in the URL: https://app.supabase.com/project/[PROJECT_REF]

Deploy all Edge Functions at once:

Terminal window
supabase functions deploy

Deploy only the data export function:

Terminal window
supabase functions deploy api-export

If your functions need environment variables (secrets):

Terminal window
supabase secrets set MY_SECRET_KEY=value

To set multiple secrets:

Terminal window
supabase secrets set \
SECRET_ONE=value1 \
SECRET_TWO=value2

List all deployed functions:

Terminal window
supabase functions list

View function details:

Terminal window
supabase functions inspect api-export

When running locally, logs appear in the terminal where you ran supabase functions serve.

View real-time logs from production:

Terminal window
supabase functions logs api-export

View logs with filters:

Terminal window
supabase functions logs api-export --limit 100
supabase functions logs api-export --follow

In your function code:

console.log('Debug info:', someVariable);
console.error('Error occurred:', error);

Run with inspect flag:

Terminal window
supabase functions serve --inspect-brk api-export

Then connect with Chrome DevTools:

  1. Open Chrome and go to chrome://inspect
  2. Click “inspect” on your function
  3. Set breakpoints and debug
Terminal window
curl -X GET http://localhost:54321/functions/v1/api-export
curl -X POST http://localhost:54321/functions/v1/api-jobs \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"job_number": "TEST-001"}'

Create a .env.local file in the project root:

SUPABASE_URL=http://localhost:54321
SUPABASE_ANON_KEY=your-local-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-local-service-role-key

Environment variables are automatically available in production:

  • SUPABASE_URL - Your project URL
  • SUPABASE_ANON_KEY - Anonymous key
  • SUPABASE_SERVICE_ROLE_KEY - Service role key

Access them in your function:

const supabaseUrl = Deno.env.get('SUPABASE_URL');

Solution: Start Docker Desktop and wait for it to fully start.

Solution: Stop the existing Supabase instance:

Terminal window
supabase stop
supabase start

Solution: Ensure the function is in the correct directory structure:

supabase/functions/[function-name]/index.ts

Solution: Add CORS headers to your function response:

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(null, { headers: corsHeaders });
}
// Add to all responses
return new Response(JSON.stringify(data), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});

Solution: Ensure you’re passing the correct Authorization header:

Terminal window
curl -H "Authorization: Bearer YOUR_ANON_KEY" ...
GET /functions/v1/api-export
  • entities - Comma-separated list of entities to export (or “all”)
    • Example: entities=jobs,parts,operations
  • format - Export format (json or csv)
    • Example: format=json
  • Requires authenticated user (Bearer token)
  • User must have admin role
  • RLS automatically filters data by tenant
Terminal window
curl -X GET \
"https://your-project.supabase.co/functions/v1/api-export?entities=all&format=json" \
-H "Authorization: Bearer $USER_TOKEN"
curl -X GET \
"https://your-project.supabase.co/functions/v1/api-export?entities=jobs,parts&format=csv" \
-H "Authorization: Bearer $USER_TOKEN"
  1. Start Supabase:

    Terminal window
    supabase start
  2. Get a test user token:

    Terminal window
    # Create test user (if needed)
    supabase auth signup --email test@example.com --password testpass123
    # Get session token (use the web UI or SDK)
  3. Test the endpoint:

    Terminal window
    curl -X GET \
    "http://localhost:54321/functions/v1/api-export?entities=jobs&format=json" \
    -H "Authorization: Bearer $TOKEN"
Terminal window
supabase start
supabase functions serve
supabase functions serve api-export
supabase functions deploy
supabase functions deploy api-export
supabase functions logs api-export
supabase stop
supabase db reset
  1. Start local Supabase: supabase start
  2. Serve functions: supabase functions serve
  3. Test the data export function in the UI at: http://localhost:5173/admin/data-export
  4. Deploy to production: supabase functions deploy api-export

For more help, run:

Terminal window
supabase functions --help