Edge Functions Setup Guide
This guide explains how to run and test Supabase Edge Functions locally and deploy them to production.
Prerequisites
Section titled “Prerequisites”-
Supabase CLI - Install the Supabase CLI:
Terminal window npm install -g supabase# orbrew install supabase/tap/supabase -
Docker - Required for running Edge Functions locally:
- Install Docker Desktop: https://www.docker.com/products/docker-desktop/
-
Deno (Optional) - For better development experience:
Terminal window curl -fsSL https://deno.land/install.sh | sh
Project Structure
Section titled “Project Structure”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.tomlRunning Edge Functions Locally
Section titled “Running Edge Functions Locally”1. Start Supabase Local Development
Section titled “1. Start Supabase Local Development”First, ensure you’re in the project root directory:
cd /home/user/eryxon-flowInitialize Supabase (if not already done):
supabase initStart the local Supabase stack:
supabase startThis 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.
2. Serve Edge Functions
Section titled “2. Serve Edge Functions”To run all Edge Functions locally:
supabase functions serveTo run a specific function:
supabase functions serve api-exportTo run with environment variables:
supabase functions serve --env-file .env.local3. Access Local Edge Functions
Section titled “3. Access Local Edge Functions”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
4. Testing the Data Export Function
Section titled “4. Testing the Data Export Function”You can test the new data export function using curl:
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:
supabase statusDeployment to Production
Section titled “Deployment to Production”1. Link to Your Supabase Project
Section titled “1. Link to Your Supabase Project”supabase loginsupabase link --project-ref your-project-refFind 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]
2. Deploy All Functions
Section titled “2. Deploy All Functions”Deploy all Edge Functions at once:
supabase functions deploy3. Deploy a Specific Function
Section titled “3. Deploy a Specific Function”Deploy only the data export function:
supabase functions deploy api-export4. Set Environment Variables
Section titled “4. Set Environment Variables”If your functions need environment variables (secrets):
supabase secrets set MY_SECRET_KEY=valueTo set multiple secrets:
supabase secrets set \ SECRET_ONE=value1 \ SECRET_TWO=value25. View Deployed Functions
Section titled “5. View Deployed Functions”List all deployed functions:
supabase functions listView function details:
supabase functions inspect api-exportMonitoring and Logs
Section titled “Monitoring and Logs”View Function Logs (Local)
Section titled “View Function Logs (Local)”When running locally, logs appear in the terminal where you ran supabase functions serve.
View Function Logs (Production)
Section titled “View Function Logs (Production)”View real-time logs from production:
supabase functions logs api-exportView logs with filters:
supabase functions logs api-export --limit 100
supabase functions logs api-export --followDebugging Edge Functions
Section titled “Debugging Edge Functions”1. Add Console Logs
Section titled “1. Add Console Logs”In your function code:
console.log('Debug info:', someVariable);console.error('Error occurred:', error);2. Use Deno Debugger (Local)
Section titled “2. Use Deno Debugger (Local)”Run with inspect flag:
supabase functions serve --inspect-brk api-exportThen connect with Chrome DevTools:
- Open Chrome and go to
chrome://inspect - Click “inspect” on your function
- Set breakpoints and debug
3. Test with Different HTTP Methods
Section titled “3. Test with Different HTTP Methods”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"}'Environment Variables
Section titled “Environment Variables”Local Development (.env.local)
Section titled “Local Development (.env.local)”Create a .env.local file in the project root:
SUPABASE_URL=http://localhost:54321SUPABASE_ANON_KEY=your-local-anon-keySUPABASE_SERVICE_ROLE_KEY=your-local-service-role-keyProduction
Section titled “Production”Environment variables are automatically available in production:
SUPABASE_URL- Your project URLSUPABASE_ANON_KEY- Anonymous keySUPABASE_SERVICE_ROLE_KEY- Service role key
Access them in your function:
const supabaseUrl = Deno.env.get('SUPABASE_URL');Common Issues and Solutions
Section titled “Common Issues and Solutions”Issue: “Docker is not running”
Section titled “Issue: “Docker is not running””Solution: Start Docker Desktop and wait for it to fully start.
Issue: “Port already in use”
Section titled “Issue: “Port already in use””Solution: Stop the existing Supabase instance:
supabase stopsupabase startIssue: “Function not found”
Section titled “Issue: “Function not found””Solution: Ensure the function is in the correct directory structure:
supabase/functions/[function-name]/index.tsIssue: “CORS errors”
Section titled “Issue: “CORS errors””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 preflightif (req.method === 'OPTIONS') { return new Response(null, { headers: corsHeaders });}
// Add to all responsesreturn new Response(JSON.stringify(data), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }});Issue: “Authentication failed”
Section titled “Issue: “Authentication failed””Solution: Ensure you’re passing the correct Authorization header:
curl -H "Authorization: Bearer YOUR_ANON_KEY" ...Data Export Function Specifics
Section titled “Data Export Function Specifics”Endpoint
Section titled “Endpoint”GET /functions/v1/api-exportQuery Parameters
Section titled “Query Parameters”entities- Comma-separated list of entities to export (or “all”)- Example:
entities=jobs,parts,operations
- Example:
format- Export format (json or csv)- Example:
format=json
- Example:
Authentication
Section titled “Authentication”- Requires authenticated user (Bearer token)
- User must have admin role
- RLS automatically filters data by tenant
Example Usage
Section titled “Example Usage”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"Testing Locally
Section titled “Testing Locally”-
Start Supabase:
Terminal window supabase start -
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) -
Test the endpoint:
Terminal window curl -X GET \"http://localhost:54321/functions/v1/api-export?entities=jobs&format=json" \-H "Authorization: Bearer $TOKEN"
Resources
Section titled “Resources”- Supabase Edge Functions Docs: https://supabase.com/docs/guides/functions
- Deno Documentation: https://deno.land/manual
- Deno Deploy: https://deno.com/deploy/docs
Quick Reference Commands
Section titled “Quick Reference Commands”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 resetNext Steps
Section titled “Next Steps”- Start local Supabase:
supabase start - Serve functions:
supabase functions serve - Test the data export function in the UI at:
http://localhost:5173/admin/data-export - Deploy to production:
supabase functions deploy api-export
For more help, run:
supabase functions --help