Edge Functions
Deploy and manage Supabase Edge Functions
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):
Environment Setup
Edge functions use .env
files for local development and the Supabase dashboard for production secrets:
Best practices for environment variables:
- Never commit secrets: The
.env
file is git-ignored by default - Use different keys: Development and production should use different keys
- Limit service role usage: Only use
SERVICE_ROLE_KEY
when absolutely necessary - 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
2. Deploy Function
3. Set Production Secrets
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:
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:
- Error Handling
- CORS Headers
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:
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:
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:
Version management is also crucial for maintaining stable deployments. You can deploy specific versions and rollback if needed:
Environment variables and secrets should be managed carefully across different environments. The Supabase CLI provides commands to manage these securely:
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.