Skip to main content

Prerequisites

Before you begin, make sure you have:

Install Supabase CLI

Verify installation:

Create a Function

Initialize Your Project

If you haven’t already, initialize Supabase in your project:
This creates a supabase directory with the following structure:

Create Your First Function

Create a new function called hello-world:
This creates:

Write Function Code

Edit supabase/functions/hello-world/index.ts:

Test Locally

Start Supabase Services

Start the local Supabase stack (requires Docker):
This starts:
  • PostgreSQL database
  • Auth server
  • Realtime server
  • Storage server
  • Edge Functions runtime

Serve Your Function

Start the function locally:
You should see:

Test with curl

In another terminal, invoke the function:
Response:

Test with JavaScript

Create a test client:

Deploy to Production

Login to Supabase

Authenticate with your Supabase account:
This opens a browser to generate an access token. Link to your Supabase project:
Find your project ref in the Supabase Dashboard under Settings > General.

Deploy the Function

Deploy your function:
Output:

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:
The --no-verify-jwt flag skips JWT verification for easier local testing.

Use Environment Variables Locally

Create .env.local:
Serve with environment variables:

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:
  1. Check the function name matches the directory name
  2. Verify deployment: supabase functions list
  3. Ensure you’re using the correct URL

CORS Errors

If browser requests fail:
  1. Add CORS headers to your response
  2. Handle OPTIONS requests
  3. Include the Authorization header in CORS config

Authentication Errors

If auth doesn’t work:
  1. Verify you’re passing the Authorization header
  2. Check the JWT token is valid
  3. Use --no-verify-jwt for local testing

Resources