> ## Documentation Index
> Fetch the complete documentation index at: https://trigger-docs-max-duration.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Triggering tasks from Supabase database webhooks

> 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.

## Overview

Database webhooks allow you to send realtime data from your database to another system whenever an event occurs in your table e.g. when a row is inserted, updated, or deleted, or when a specific column is updated.

This guide shows you how to set up a Supabase database webhook and deploy a simple edge function that triggers a "Hello world" task every time a new row is inserted into your table.

## Prerequisites

* Ensure you have the [Supabase CLI](https://supabase.com/docs/guides/cli/getting-started) installed
* Ensure TypeScript is installed
* [Create a Trigger.dev account](https://cloud.trigger.dev)
* [Create a new Trigger.dev project](/guides/dashboard/creating-a-project)

## Initial setup

<Steps>
  <Step title="Optional step 1: create a new Supabase project">
    <Info> If you already have a Supabase project on your local machine you can skip this step.</Info>

    You can create a new project by running the following command in your terminal using the Supabase CLI:

    ```bash
    supabase init
    ```

    <Note>
      If you are using VS Code, ensure to answer 'y' when asked to generate VS Code settings for Deno,
      and install any recommended extensions.
    </Note>
  </Step>

  <Step title="Optional step 2: create package.json and tsconfig.json files">
    If your project does not already have `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.

    <Info> If your project has these files you can skip this step.</Info>

    Both of these files are required for the Trigger.dev SDK to work correctly.

    ```ts package.json
    {
      "devDependencies": {
        // This should be the version of typescript you are using
        "typescript": "^5.6.2"
      }
    }
    ```

    ```ts tsconfig.json
    {
      "compilerOptions": {
        "target": "esnext",
        "module": "NodeNext",
        "moduleResolution": "NodeNext",
        "esModuleInterop": true,
        "strict": true,
        "outDir": "dist",
        "skipLibCheck": true,
        "lib": ["DOM", "DOM.Iterable"],
        "noEmit": true
      },
      "include": ["./src/**/*.ts", "trigger.config.ts"]
    }
    ```
  </Step>

  <Step title="Run the CLI `init` command">
    The easiest way to get started is to use the CLI. It will add Trigger.dev to your existing project, create a `/trigger` folder and give you an example task.

    Run this command in the root of your project to get started:

    <CodeGroup>
      ```bash npm
      npx trigger.dev@latest init
      ```

      ```bash pnpm
      pnpm dlx trigger.dev@latest init
      ```

      ```bash yarn
      yarn dlx trigger.dev@latest init
      ```
    </CodeGroup>

    It will do a few things:

    1. Log you into the CLI if you're not already logged in.
    2. Create a `trigger.config.ts` file in the root of your project.
    3. Ask where you'd like to create the `/trigger` directory.
    4. Create the `/trigger` directory with an example task, `/trigger/example.[ts/js]`.

    Install the "Hello World" example task when prompted. We'll use this task to test the setup.
  </Step>

  <Step title="Run the CLI `dev` command">
    The CLI `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.

    <CodeGroup>
      ```bash npm
      npx trigger.dev@latest dev
      ```

      ```bash pnpm
      pnpm dlx trigger.dev@latest dev
      ```

      ```bash yarn
      yarn dlx trigger.dev@latest dev
      ```
    </CodeGroup>
  </Step>

  <Step title="Perform a test run using the dashboard">
    The CLI `dev` command spits out various useful URLs. Right now we want to visit the Test page <Icon icon="circle-1" iconType="solid" size={20} color="F43F47" />.

    You should see our Example task in the list <Icon icon="circle-2" iconType="solid" size={20} color="F43F47" />, select it. Most tasks have a "payload" which you enter in the JSON editor <Icon icon="circle-3" iconType="solid" size={20} color="F43F47" />, but our example task doesn't need any input.

    Press the "Run test" button <Icon icon="circle-4" iconType="solid" size={20} color="F43F47" />.

    ![Test page](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/test-page.png)
  </Step>

  <Step title="View your run">
    Congratulations, you should see the run page which will live reload showing you the current state of the run.

    ![Run page](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/run-page.png)

    If you go back to your terminal you'll see that the dev command also shows the task status and links to the run log.

    ![Terminal showing completed run](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/terminal-completed-run.png)
  </Step>
</Steps>

## Create and deploy a new Supabase edge function and create a new database webhook

<Steps>
  <Step title="Add your Trigger.dev prod secret key in Supabase">
    First, go to your Trigger.dev [project dashboard](https://cloud.trigger.dev) and copy the `prod` secret key from the API keys page.

    ![How to find your prod secret key](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/api-key-prod.png)

    Then, in [Supabase](https://supabase.com/dashboard/projects), select the project you want to use, navigate to 'Project settings' <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" />, click 'Edge functions' <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" /> in the configurations menu, and then click the 'Add new secret' <Icon icon="circle-3" iconType="solid" size={20} color="A8FF53" /> button.

    Add `TRIGGER_SECRET_KEY` <Icon icon="circle-4" iconType="solid" size={20} color="A8FF53" /> with the pasted value of your Trigger.dev `prod` secret key.

    ![Add secret key in Supabase](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-keys-1.png)
  </Step>

  <Step title="Create a new edge function using the Supabase CLI">
    Now create a new edge function using the Supabase CLI. We will call it `database-webhook`.

    ```bash
    supabase functions new database-webhook
    ```
  </Step>

  <Step title="Update the edge function code">
    Replace the `database-webhook` placeholder code with the following code:

    ```ts functions/database-webhook/index.ts
    import "jsr:@supabase/functions-js/edge-runtime.d.ts";
    // Import the Trigger.dev SDK - replace "<your-sdk-version>" with the version of the SDK you are using, e.g. "3.0.0". You can find this in your package.json file.
    import { tasks } from "npm:@trigger.dev/sdk@3.0.0/v3";
    // Import your task type from your /trigger folder
    import type { helloWorldTask } from "../../../src/trigger/example.ts";
    //     👆 **type-only** import

    console.log("Hello from 'database-webhook' function!");

    Deno.serve(async (req) => {
      // Listens for incoming JSON requests
      const payload = await req.json();
      // Triggers the "hello-world" task with the payload
      await tasks.trigger<typeof helloWorldTask>(
        // Your task id
        "hello-world",
        // Your task payload. This will be the data you receive from the database webhook
        payload
      );
      return new Response("ok");
    });
    ```

    This code sets up a Deno server that listens for incoming JSON requests, triggers a "hello-world" task, logs the received payload, and responds with "ok". This setup is typical for a webhook endpoint that processes incoming data and triggers some action (in this case, the "hello-world" task) based on that data.

    <Note>You can only import the `type` from the task.</Note>

    <Note>
      Tasks in the `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.
    </Note>
  </Step>

  <Step title="Deploy your edge function using the Supabase CLI">
    Now deploy your edge function with the following command:

    ```bash
    supabase functions deploy database-webhook
    ```

    Follow the CLI instructions, selecting the same project you added your `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`

    <Note>Replace `your-project-id` with your actual project ID.</Note>
  </Step>

  <Step title="Create a new table">
    Next, in your Supabase project dashboard, click on 'Table Editor' <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" /> in the left-hand menu and create a new table. <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" />

    ![How to create a new table](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-new-table-1.png)

    In this example we will call our table `skynet`. <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" />

    Add a new column called `name` with the type `text`. <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" />

    ![How to add a new column](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-new-table-2.png)
  </Step>

  <Step title="Configure JWT settings">
    By default, Supabase edge functions require a JSON Web Token [JWT](https://supabase.com/docs/guides/auth/jwts) in the authorization header. This is to ensure that only authorized users can access your edge functions.

    In your Supabase project dashboard, click 'Project settings' <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" />, then the 'API' tab <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" />, and copy the `anon` `public` API key from the table <Icon icon="circle-3" iconType="solid" size={20} color="A8FF53" />.

    ![How to find your Supabase API keys](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-api-key.png)

    Then, go to 'Database' <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" /> click on 'Webhooks' <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" />, and then click 'Create a new hook' <Icon icon="circle-3" iconType="solid" size={20} color="A8FF53" />.

    ![How to create a new webhook](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-create-webhook-1.png)

    <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" /> Call the hook `edge-function-hook`.

    <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" /> Select the new table you have created:
    `public` `skynet`.

    <Icon icon="circle-3" iconType="solid" size={20} color="A8FF53" /> Choose the `insert` event.

    ![How to create a new webhook 2](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-create-webhook-2.png)

    <Icon icon="circle-4" iconType="solid" size={20} color="A8FF53" /> Under 'Webhook configuration', select
    'Supabase Edge functions'{" "}

    <Icon icon="circle-5" iconType="solid" size={20} color="A8FF53" /> Under 'Edge function', choose `POST`
    and select the edge function you have created: `database-webhook`.{" "}

    <Icon icon="circle-6" iconType="solid" size={20} color="A8FF53" /> 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).

    <Icon icon="circle-7" iconType="solid" size={20} color="A8FF53" /> Click 'Create webhook'.{" "}

    ![How to create a new webhook 3](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-create-webhook-3.png)

    Your database webhook is now ready to use.
  </Step>
</Steps>

## Deploy your task and trigger it from your new `database-webhook` edge function

<Steps>
  <Step title="Deploy your 'Hello World' task">
    The next step is to deploy your `hello-world` task to [Trigger.dev cloud](https://cloud.trigger.dev).

    To do this, run the following command in the terminal:

    <CodeGroup>
      ```bash npm
      npx trigger.dev@latest deploy
      ```

      ```bash pnpm
      pnpm dlx trigger.dev@latest deploy
      ```

      ```bash yarn
      yarn dlx trigger.dev@latest deploy
      ```
    </CodeGroup>
  </Step>

  <Step title="Trigger the task from your edge function">
    Your `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' <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" /> in the left-hand menu, click on the `skynet` table <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" /> , and then click 'Insert', 'Insert Row' <Icon icon="circle-3" iconType="solid" size={20} color="A8FF53" />.

    ![How to insert a new row 1](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-new-table-3.png)

    Add a new item under `name`, with the value `Sarah Connor` (this can be any string). <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" />

    ![How to insert a new row 2](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-new-table-4.png)

    Go back to your edge function dashboard <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" /> , and under 'Logs' <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" /> you should see a new run of your `database-webhook` edge function. <Icon icon="circle-3" iconType="solid" size={20} color="A8FF53" />

    ![How to view the logs](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-logs.png)

    Then, check your [cloud.trigger.dev](http://cloud.trigger.dev) project 'Runs' list <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" /> and you should see a successful `hello-world` task <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" /> 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.

    ![How to insert a new row 2](https://mintlify.s3-us-west-1.amazonaws.com/trigger-docs-max-duration/images/supabase-trigger-screenshot.png)

    **Congratulations, you have successfully triggered a task from a Supabase edge function using a database webhook!**
  </Step>
</Steps>

## Learn more about Supabase and Trigger.dev

### Full walkthrough guides from development to deployment

<CardGroup cols={2}>
  <Card title="Edge function hello world" icon="bolt" href="/guides/frameworks/supabase-edge-functions-basic">
    Learn how to trigger a task from a Supabase edge function when a URL is visited.
  </Card>

  <Card title="Edge function database webhooks" icon="bolt" href="/guides/frameworks/supabase-edge-functions-database-webhooks">
    Learn how to trigger a task from a Supabase edge function when an event occurs in your database.
  </Card>
</CardGroup>

### Task examples with code you can copy and paste

<CardGroup cols={2}>
  <Card title="Supabase database operations" icon="bolt" href="/guides/examples/supabase-database-operations">
    Run basic CRUD operations on a table in a Supabase database using Trigger.dev.
  </Card>

  <Card title="Supabase Storage upload" icon="bolt" href="/guides/examples/supabase-storage-upload">
    Download a video from a URL and upload it to Supabase Storage using S3.
  </Card>
</CardGroup>
