Supabase Edge Functions
Supabase is an open-source alternative to Firebase, offering a suite of tools similar to Firebase's capabilities, including database, authentication, storage, and now, serverless functions.
Supabase Edge Functions are server-side TypeScript functions that are distributed globally, running closer to your users for improved performance. These functions are developed using Deno, which brings several benefits, including improved security and a modern JavaScript/TypeScript runtime.
Here's how you can get started with Supabase Edge Functions:
1. Setup
Prerequisites
Before you begin, make sure you have the Supabase CLI installed. If you haven't installed it yet, follow the instructions in the official documentation.
Creating a New Project
Open your terminal or command prompt.
Create a new Supabase project in a directory on your local machine by running:
supabase init
This command initializes a new Supabase project in the current directory.
Adding an Edge Function
- Inside your Supabase project, create a new Edge Function named
hello-world
:
supabase functions new hello-world
This command creates a new Edge Function with the specified name in your project.
2. Hello World
Edit the hello-world
function by modifying the file supabase/functions/hello-world/index.ts
:
import { Hono } from 'jsr:@hono/hono'
// change this to your function name
const functionName = 'hello-world'
const app = new Hono().basePath(`/${functionName}`)
app.get('/hello', (c) => c.text('Hello from hono-server!'))
Deno.serve(app.fetch)
3. Run
To run the function locally, use the following command:
- Use the following command to serve the function:
supabase start # start the supabase stack
supabase functions serve --no-verify-jwt # start the Functions watcher
The --no-verify-jwt
flag allows you to bypass JWT verification during local development.
- Make a GET request using cURL or Postman to
http://127.0.0.1:54321/functions/v1/hello-world/hello
:
curl --location 'http://127.0.0.1:54321/functions/v1/hello-world/hello'
This request should return the text "Hello from hono-server!".
4. Deploy
You can deploy all of your Edge Functions in Supabase with a single command:
supabase functions deploy
Alternatively, you can deploy individual Edge Functions by specifying the name of the function in the deploy command:
supabase functions deploy hello-world
For more deployment methods, visit the Supabase documentation on Deploying to Production.