Overview

Supabase Edge Functions are server-side TypeScript functions that run on Deno, deployed globally at the edge - closer to your users. They provide a powerful way to execute server-side code without managing traditional server infrastructure.

Edge Functions combine the best of serverless computing with edge computing capabilities. By running your code at the edge, closer to your users, you get significantly lower latency compared to traditional server deployments. These functions are built on Deno’s secure runtime, giving you access to a modern JavaScript environment with built-in TypeScript support.

Key Benefits

Edge Functions provide automatic scaling without any configuration needed - they scale from zero to handling thousands of requests seamlessly. Security is built into the core, with JWT verification and secure environment variables management coming out of the box.

Key features include:

  • Global Distribution: Functions are deployed across multiple regions for low-latency responses
  • TypeScript/JavaScript: Write in familiar TypeScript with full Deno support
  • Secure: Built-in security with JWT verification and environment variables

Getting Started

Project Structure

Launchtoday includes a pre-configured edge function for secure user account deletion in the following structure (which you can find here):

supabase/
├── functions/
│   └── user-self-delete/
│       ├── index.ts        # Main function code
│       ├── .env           # Local environment variables (git-ignored)
│       └── .env.example   # Template for environment variables
├── config.toml            # Supabase configuration
└── seed.sql              # Database seeds

Environment Setup

Edge functions use .env files for local development and the Supabase dashboard for production secrets:

# .env.example
SUPABASE_URL=your-project-url
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key

Best practices for environment variables:

  1. Never commit secrets: The .env file is git-ignored by default
  2. Use different keys: Development and production should use different keys
  3. Limit service role usage: Only use SERVICE_ROLE_KEY when absolutely necessary
  4. Validate early: Check for required variables at the start of your function

User Self-Delete Function

Launchtoday includes a pre-built edge function for secure user account deletion. This function handles both the auth user and related profile data deletion securely. Here’s how to deploy and use it:

1. Install Supabase CLI

# Using npm
npm install -g supabase

# Using yarn
yarn global add supabase

# Verify installation
supabase --version

2. Deploy Function

# Navigate to the edge functions directory
cd supabase/functions

# Deploy to your Supabase instance
supabase functions deploy user-self-deletion

3. Set Production Secrets

# Set multiple secrets from .env file
supabase secrets set --env-file ./supabase/functions/user-self-delete/.env

# List all secrets
supabase secrets list

4. Invoke from Client

The function is already integrated with the Launchtoday template which you can find here. Here’s an example of how to invoke it:

import { supabase } from "@/lib/supabase";

const deleteAccount = async () => {
  try {
    const { data, error } = await supabase.functions.invoke(
      "user-self-deletion"
    );
    if (error) throw error;
    console.log("Account deleted successfully");
  } catch (error) {
    console.error("Error deleting account:", error);
  }
};

Creating Custom Functions

Common Use Cases

Edge Functions excel at handling real-time operations and complex business logic. Common applications include:

  • Processing data transformations before or after database operations
  • Implementing custom authentication flows
  • Integrating with third-party APIs securely
  • Managing background jobs and scheduled tasks

Best Practices

When creating additional edge functions, follow these patterns:

  1. Error Handling
try {
  // Your function logic
} catch (error) {
  return new Response(
    JSON.stringify({
      error: error.message,
      stack: process.env.NODE_ENV === "development" ? error.stack : undefined,
    }),
    { status: 500 }
  );
}
  1. CORS Headers
const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Methods": "POST",
  "Access-Control-Allow-Headers":
    "authorization, x-client-info, apikey, content-type",
};

Development & Monitoring

During development, you’ll want to test your edge functions locally before deploying them to production. The Supabase CLI provides powerful tools for local development and testing. Start by running your functions locally using supabase functions serve. This command starts a local server that mimics the production environment, allowing you to test your functions as if they were deployed.

For the user-self-delete function specifically, you can run:

supabase functions serve user-self-delete --env-file ./supabase/functions/user-self-delete/.env

Testing Your Functions

Testing edge functions can be done through various methods. The simplest approach is using cURL from your terminal. For example, to test the user-self-delete function locally:

curl -L -X POST 'http://localhost:54321/functions/v1/user-self-delete' \
-H 'Authorization: Bearer your-jwt-token'

For more complex testing scenarios, Postman provides a user-friendly interface. Simply set up your request with the appropriate headers and body, and you can save these configurations for repeated testing.

Monitoring and Debugging

Monitoring your edge functions is crucial for maintaining a healthy production environment. The Supabase Dashboard provides comprehensive logging and monitoring capabilities. Navigate to Edge Functions → Logs in your dashboard to view detailed execution logs, including error messages and stack traces.

During local development, your functions will output logs directly to your terminal. Use console.log() statements strategically to debug issues, but remember to remove or condition them before deploying to production. For production errors, console.error() statements are automatically captured in the dashboard logs.

The dashboard also provides insights into your function’s performance metrics, including execution time and memory usage. While function alerts are coming soon, you can monitor these metrics manually to ensure optimal performance.

Deployment Strategy

When deploying edge functions, it’s important to follow a structured approach. Start by testing thoroughly in your local environment, then deploy to a staging project before pushing to production. Here’s a typical deployment workflow:

# Deploy to staging
supabase functions deploy user-self-delete --project-ref your-test-project

# After testing, deploy to production
supabase functions deploy user-self-delete --project-ref your-prod-project

Version management is also crucial for maintaining stable deployments. You can deploy specific versions and rollback if needed:

supabase functions deploy user-self-delete:v1

Environment variables and secrets should be managed carefully across different environments. The Supabase CLI provides commands to manage these securely:

# Manage your secrets
supabase secrets set MY_API_KEY=xxxxxx
supabase secrets list

Remember to keep your production secrets separate from development and never commit sensitive information to version control. The .env file in your local environment should mirror the structure of your production secrets while using development-safe values.