Supabase Edge Functions are serverless functions that run on Supabase’s global edge network.

Often, you want to trigger a Supabase Edge Function when a database row changes. For example, you may want to trigger a function as a side-effect of a database change, or fan out work to multiple services.

In this guide, you will learn how to setup a Sequin webhook sink to trigger a Supabase Edge Function when a database row changes. This is an alternative to triggering Supabase Edge Functions with Supabase webhooks - and offers additional features like retries and filtering built in.

Prerequisites

You are about to create a simple Supabase Edge Function that logs a message to the console. You’ll trigger this function by setting up a webhook sink in Sequin that sends a HTTP POST request to the function’s URL with the payload of the database row that changed.

You’ll need the following:

Create a Supabase Edge Function

1

Create a new edge function

Using the Supabase CLI, create a new edge function:

supabase functions new webhook-handler

This will create a new TypeScript function in the supabase/functions/webhook-handler directory.

2

Add code to process webhook requests

Replace the contents of supabase/functions/webhook-handler/index.ts with:

import { serve } from "https://deno.land/std@0.168.0/http/server.ts"

serve(async (req) => {
  try {
    const { data } = await req.json()

    // Process each change in the batch
    for (const change of data) {
      // Log the database change
      console.log('Database change:', {
        action: change.action,
        table: change.metadata.table_name,
        schema: change.metadata.table_schema,
        record: change.record,
        changes: change.changes,
        timestamp: change.metadata.commit_timestamp,
        consumer: change.metadata.consumer
      })

      // Add your business logic here
      // For example, you could:
      // - Send notifications
      // - Update other services
      // - Transform data
    }

    return new Response(
      JSON.stringify({ message: 'Webhook processed successfully' }),
      {
        status: 200,
        headers: { 'Content-Type': 'application/json' }
      }
    )

  } catch (error) {
    console.error('Error processing webhook:', error)
    return new Response(
      JSON.stringify({ error: 'Error processing webhook' }),
      {
        status: 400,
        headers: { 'Content-Type': 'application/json' }
      }
    )
  }
})

This function will:

  1. Parse the incoming webhook payload from Sequin
  2. Log the database change details
  3. Allow you to add custom business logic if you’d like
  4. Return a success/error response
3

Test the function locally

Start the function locally using the Supabase CLI:

supabase start
supabase functions serve webhook-handler

The function will be available at http://localhost:54321/functions/v1/webhook-handler

4

Deploy the edge function and retrieve the URL

Using the Supabase CLI, deploy your edge function:

supabase functions deploy webhook-handler

Once deployed, you can retrieve the URL of the edge function by logging into the Supabase Dashboard and navigating to the Edge Functions page. The URL will be in the format:

https://<project-ref>.functions.supabase.co/webhook-handler

You can also get the URL by running:

supabase functions list

Create a Sequin webhook sink

1

Create a new sink

Navigate to the “Sinks” tab, click the “Create Sink” button, and select “Webhook Sink”.

2

Select source table

Select your Supabase database and the table you want to capture changes from.

3

Choose message type

Specify whether you want to receive changes or rows from the table. In this case, you want to receive changes.

4

Specify change types

In “Records to process”, set which operations (INSERT, UPDATE, and/or DELETE) you want to capture.

5

Configure backfill

Leave “Backfill” toggled off for now.

6

Configure message grouping

Under “Message grouping”, leave the default option selected to ensure events for the same row are sent to your webhook endpoint in order.

7

Configure delivery settings

Under “Delivery configuration”, choose a conservative value for “Request timeout” and set “Batch size” to 1.

8

Configure HTTP endpoint

Add your Supabase edge function URL and add an Authorization header with the value Bearer YOUR_SUPABASE_ANON_KEY.

The Authorization header is required to authenticate the request with the edge function. You can find your SUPABASE_ANON_KEY in the “Settings” > “API” page in the Supabase Dashboard. If you don’t want to authenticate your request (i.e. make the edge function public), you can deploy the edge function with the --no-verify-jwt flag.

9

Name and create sink

Give your sink a name and click “Create Webhook Sink”.

Test end-to-end

1

Add a row to your table

Insert a row into your table. For example:

insert into
users (name)
values
  (
    'John Doe'
  );
2

Trace the change in the Sequin dashboard

In the Sequin console, open the “Messages” tab on your webhook sink and confirm that a message was delivered.

3

Confirm the event was received by your edge function

In the Supabase Dashboard, navigate to “Edge Functions” and click on your function. You should see the event logged in the function logs.

You’ve successfully triggered a Supabase Edge Function from a database change!

Next steps

From here, you may want to:

  • Refine your webhook sink by using more specific filters. Perhaps you can move logic out of your edge function and into your consumer filters.
  • Use Sequin’s replay and backfill features to run historical data through your edge function.
  • Use Sequin’s observability features to monitor and debug your webhooks and triggers.