Prerequisites
Before you begin, make sure you have:- A Supabase project (create one)
- Supabase CLI installed
- Deno installed (optional, for local development)
Install Supabase CLI
Create a Function
Initialize Your Project
If you haven’t already, initialize Supabase in your project:supabase directory with the following structure:
Create Your First Function
Create a new function calledhello-world:
Write Function Code
Editsupabase/functions/hello-world/index.ts:
Test Locally
Start Supabase Services
Start the local Supabase stack (requires Docker):- PostgreSQL database
- Auth server
- Realtime server
- Storage server
- Edge Functions runtime
Serve Your Function
Start the function locally:Test with curl
In another terminal, invoke the function:Test with JavaScript
Create a test client:Deploy to Production
Login to Supabase
Authenticate with your Supabase account:Link Your Project
Link to your Supabase project:Deploy the Function
Deploy your function:Test Production Function
Invoke the deployed function:Replace
YOUR_ANON_KEY with your project’s anon key from Settings > API in the Dashboard.Add Business Logic
Connect to Database
Access your Supabase database from the function:Add Authentication
Enforce authentication in your function:Handle CORS
Enable CORS for browser requests:Real-World Example: Send Email
Create a function to send emails with Resend:Create the Function
Write the Code
Set Environment Variable
Deploy
Invoke
Development Workflow
Watch Mode
Auto-reload on file changes:--no-verify-jwt flag skips JWT verification for easier local testing.
Use Environment Variables Locally
Create.env.local:
Test with Postman
Import this cURL command into Postman:Next Steps
Deploy Functions
Learn advanced deployment strategies and CI/CD
Environment Variables
Manage secrets and configuration
Debugging
Debug and monitor your functions
Troubleshooting
Function Not Found
If you get a 404 error:- Check the function name matches the directory name
- Verify deployment:
supabase functions list - Ensure you’re using the correct URL
CORS Errors
If browser requests fail:- Add CORS headers to your response
- Handle OPTIONS requests
- Include the Authorization header in CORS config
Authentication Errors
If auth doesn’t work:- Verify you’re passing the Authorization header
- Check the JWT token is valid
- Use
--no-verify-jwtfor local testing
