This guide will show you how to trigger a task when a row is added to a table using a Supabase database webhook and edge function.
Optional step 1: create a new Supabase project
Optional step 2: create package.json and tsconfig.json files
package.json
or/and tsconfig.json
files (e.g. if you are using Deno), create them manually and add them to your project root folder.Run the CLI `init` command
/trigger
folder and give you an example task.Run this command in the root of your project to get started:trigger.config.ts
file in the root of your project./trigger
directory./trigger
directory with an example task, /trigger/example.[ts/js]
.Run the CLI `dev` command
dev
command runs a server for your tasks. It watches for changes in your /trigger
directory and communicates with the Trigger.dev platform to register your tasks, perform runs, and send data back and forth.It can also update your @trigger.dev/*
packages to prevent version mismatches and failed deploys. You will always be prompted first.Perform a test run using the dashboard
dev
command spits out various useful URLs. Right now we want to visit the Test page .You should see our Example task in the list , select it. Most tasks have a “payload” which you enter in the JSON editor , but our example task doesn’t need any input.Press the “Run test” button .View your run
Add your Trigger.dev prod secret key in Supabase
prod
secret key from the API keys page.TRIGGER_SECRET_KEY
with the pasted value of your Trigger.dev prod
secret key.Create a new edge function using the Supabase CLI
database-webhook
.Update the edge function code
database-webhook
placeholder code with the following code:type
from the task.trigger
folder use Node, so they must stay in there or they will not run,
especially if you are using a different runtime like Deno. Also do not add “npm:
” to imports
inside your task files, for the same reason.Deploy your edge function using the Supabase CLI
prod
secret key to, and once complete you should see your new edge function deployment in your Supabase edge functions dashboard.There will be a link to the dashboard in your terminal output, or you can find it at this URL:https://supabase.com/dashboard/project/<your-project-id>/functions
your-project-id
with your actual project ID.Create a new table
skynet
. Add a new column called name
with the type text
. Configure JWT settings
anon
public
API key from the table .edge-function-hook
. Select the new table you have created:
public
skynet
. Choose the insert
event.POST
and select the edge function you have created: database-webhook
. Under ‘HTTP Headers’, add a new header with the key Authorization
and the value Bearer <your-api-key>
(replace <your-api-key>
with the anon
public
API key you copied earlier). Click ‘Create webhook’.database-webhook
edge functionDeploy your 'Hello World' task
hello-world
task to Trigger.dev cloud.To do this, run the following command in the terminal:Trigger the task from your edge function
database-webhook
edge function is now set up to trigger the hello-world
task every time a new row is inserted into your skynet
table.To do this, go back to your Supabase project dashboard, click on ‘Table Editor’ in the left-hand menu, click on the skynet
table , and then click ‘Insert’, ‘Insert Row’ .name
, with the value Sarah Connor
(this can be any string). database-webhook
edge function. hello-world
task which has been triggered when you added a new row with the name
Sarah Connor
to your skynet
Supabase table.Inside that run you will see the payload that was sent from the database webhook, including the name
and other table information.