Trigger Supabase edge functions from database changes
How to use a webhook sink to trigger Supabase edge functions from database changes.
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:
- A Supabase account
- Sequin installed locally or a Sequin Cloud account
- A Supabase database connected to Sequin
Create a Supabase Edge Function
Create a new edge function
Using the Supabase CLI, create a new edge function:
This will create a new TypeScript function in the supabase/functions/webhook-handler
directory.
Add code to process webhook requests
Replace the contents of supabase/functions/webhook-handler/index.ts
with:
This function will:
- Parse the incoming webhook payload from Sequin
- Log the database change details
- Allow you to add custom business logic if you’d like
- Return a success/error response
Test the function locally
Start the function locally using the Supabase CLI:
The function will be available at http://localhost:54321/functions/v1/webhook-handler
Deploy the edge function and retrieve the URL
Using the Supabase CLI, deploy your edge function:
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:
You can also get the URL by running:
Create a Sequin webhook sink
Create a new sink
Navigate to the “Sinks” tab, click the “Create Sink” button, and select “Webhook Sink”.
Select source table
Select your Supabase database and the table you want to capture changes from.
Choose message type
Specify whether you want to receive changes or rows from the table. In this case, you want to receive changes.
Specify change types
In “Records to process”, set which operations (INSERT
, UPDATE
, and/or DELETE
) you want to capture.
Configure backfill
Leave “Backfill” toggled off for now.
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.
Configure delivery settings
Under “Delivery configuration”, choose a conservative value for “Request timeout” and set “Batch size” to 1.
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.
Name and create sink
Give your sink a name and click “Create Webhook Sink”.
Test end-to-end
Add a row to your table
Insert a row into your table. For example:
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.
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.
Was this page helpful?