Edge Functions

Getting Started with Edge Functions

Learn how to create, test, and deploy your first Edge Function using the Supabase CLI.


Before getting started, make sure you have the Supabase CLI installed. Check out the CLI installation guide for installation methods and troubleshooting.


Step 1: Create or configure your project

If you don't have a project yet, initialize a new Supabase project in your current directory.

1
2
supabase init my-edge-functions-projectcd my-edge-functions-project

Or, if you already have a project locally, navigate to your project directory. If your project hasn't been configured for Supabase yet, make sure to run the supabase init command.

1
2
cd your-existing-projectsupabase init # Initialize Supabase, if you haven't already

Step 2: Create your first function

Within your project, generate a new Edge Function with a basic template:

1
supabase functions new hello-world

This creates a new function at supabase/functions/hello-world/index.ts with this starter code:

1
2
3
4
5
6
7
8
Deno.serve(async (req) => { const { name } = await req.json() const data = { message: `Hello ${name}!`, } return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } })})

This function accepts a JSON payload with a name field and returns a greeting message.


Step 3: Test your function locally

Start the local development server to test your function:

1
2
supabase start # Start all Supabase servicessupabase functions serve hello-world

Your function is now running at http://localhost:54321/functions/v1/hello-world. Hot reloading is enabled, which means that the server will automatically reload when you save changes to your function code.


Step 4: Send a test request

Open a new terminal and test your function with curl:

1
2
3
4
curl -i --location --request POST 'http://localhost:54321/functions/v1/hello-world' \ --header 'Authorization: Bearer SUPABASE_ANON_KEY' \ --header 'Content-Type: application/json' \ --data '{"name":"Functions"}'

After running this curl command, you should see:

1
{ "message": "Hello Functions!" }

You can also try different inputs. Change "Functions" to "World" in the curl command and run it again to see the response change.


Step 5: Connect to your Supabase project

To deploy your function globally, you need to connect your local project to a Supabase project.

First, login to the CLI if you haven't already, and authenticate with Supabase. This opens your browser to authenticate with Supabase; complete the login process in your browser.

1
supabase login

Next, list your Supabase projects to find your project ID:

1
supabase projects list

Next, copy your project ID from the output, then connect your local project to your remote Supabase project. Replace YOUR_PROJECT_ID with the ID from the previous step.

1
supabase link --project-ref [YOUR_PROJECT_ID]

Step 6: Deploy to production

Deploy your function to Supabase's global edge network:

1
2
3
4
supabase functions deploy hello-world# If you want to deploy all functions, run the `deploy` command without specifying a function name:supabase functions deploy

If you want to skip JWT verification, you can add the --no-verify-jwt flag for webhooks that don't need authentication:

1
supabase functions deploy hello-world --no-verify-jwt

When the deployment is successful, your function is automatically distributed to edge locations worldwide.


Step 7: Test your live function

🎉 Your function is now live! Test it with your project's anon key:

1
2
3
4
curl --request POST 'http://[YOUR_PROJECT_ID].supabase.co/functions/v1/hello-world' \ --header 'Authorization: Bearer SUPABASE_ANON_KEY' \ --header 'Content-Type: application/json' \ --data '{"name":"Production"}'

Expected response:

1
{ "message": "Hello Production!" }

Finally, you should have a fully deployed Edge Function that you can call from anywhere in the world.


Usage

Now that your function is deployed, you can invoke it from within your app:

1
2
3
4
5
6
7
8
9
import { createClient } from '@supabase/supabase-js'const supabase = createClient('http://[YOUR_PROJECT_ID].supabase.co', 'YOUR_ANON_KEY')const { data, error } = await supabase.functions.invoke('hello-world', { body: { name: 'JavaScript' },})console.log(data) // { message: "Hello JavaScript!" }