# Edge Functions Overview

Supabase Edge Functions are server-side TypeScript functions that run on-demand. The Exit WeWeb Portal uses Edge Functions for complex operations and external integrations.

# Base URL

https://edknwrcztqwhskjpoxta.supabase.co/functions/v1/

# Authentication

Edge Functions require authentication:

Authorization: Bearer YOUR_ANON_KEY
Content-Type: application/json

# Available Functions

# Core Functions

  1. invite-client-admin - Send invitations to new client administrators
  2. invite-client-manager - Send invitations to new client managers
  3. request-new-inspections - Create bulk inspection requests
  4. import-past-inspections - Import historical inspection data

# Fire Alert Functions

  1. fetch-fire-alerts - Fetch fire incidents from 1rwn.com API
  2. get-building-owner-actovia - Get building owner info from Actovia
  3. send-fire-alert-notification - Send email/SMS notifications
  4. process-fire-incidents - Orchestrate fire alert workflow

# Request Format

All Edge Functions accept POST requests with JSON payloads:

POST /functions/v1/function-name
Authorization: Bearer YOUR_ANON_KEY
Content-Type: application/json

{
  "param1": "value1",
  "param2": "value2"
}

# Response Format

Successful responses return JSON:

{
  "success": true,
  "data": {
    "result": "Operation completed"
  }
}

Error responses:

{
  "success": false,
  "error": "Error message",
  "details": {}
}

# CORS Configuration

All Edge Functions include CORS headers for browser compatibility:

import { corsHeaders } from '../_shared/cors.ts';

// In handler
return new Response(JSON.stringify(data), {
  headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  status: 200
});

# Environment Variables

Edge Functions can access environment variables:

  • SUPABASE_URL - Supabase project URL
  • SUPABASE_ANON_KEY - Anonymous key
  • SUPABASE_SERVICE_ROLE_KEY - Service role key (admin operations)
  • Custom variables for integrations (Twilio, Resend, etc.)

# Error Handling

Functions should handle errors gracefully:

try {
  // Function logic
  return new Response(JSON.stringify({ success: true, data }), {
    headers: { ...corsHeaders, 'Content-Type': 'application/json' },
    status: 200
  });
} catch (error) {
  return new Response(JSON.stringify({ 
    success: false, 
    error: error.message 
  }), {
    headers: { ...corsHeaders, 'Content-Type': 'application/json' },
    status: 400
  });
}

# Development

# Local Testing

supabase functions serve function-name

# Deployment

supabase functions deploy function-name

# Scheduled Functions

Some functions run on schedules via database triggers:

  • fetch-fire-alerts - Every 10 minutes
  • process-fire-incidents - Every 5 minutes

These are configured in the Supabase dashboard under Database → Extensions → pg_cron.

# Security Considerations

  1. Always validate input parameters
  2. Use RLS policies when accessing database
  3. Never expose service role key to clients
  4. Implement rate limiting for public endpoints
  5. Log security-relevant events

# Monitoring

Edge Functions provide automatic logging:

  • View logs in Supabase dashboard
  • Filter by function name and time range
  • Monitor error rates and performance