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

# Triggering tasks from Supabase edge functions

> This guide will show you how to trigger a task from a Supabase edge function, and then view the run in our dashboard.

## Overview

Supabase edge functions allow you to trigger tasks either when an event is sent from a third party (e.g. when a new Stripe payment is processed, when a new user signs up to a service, etc), or when there are any changes or updates to your Supabase database.

This guide shows you how to set up and deploy a simple Supabase edge function example that triggers a task when an edge function URL is accessed.

## 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 theme={null}
    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 theme={null}
    {
      "devDependencies": {
        // This should be the version of typescript you are using
        "typescript": "^5.6.2"
      }
    }
    ```

    ```ts tsconfig.json theme={null}
    {
      "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 theme={null}
      npx trigger.dev@latest init
      ```

      ```bash pnpm theme={null}
      pnpm dlx trigger.dev@latest init
      ```

      ```bash yarn theme={null}
      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 theme={null}
      npx trigger.dev@latest dev
      ```

      ```bash pnpm theme={null}
      pnpm dlx trigger.dev@latest dev
      ```

      ```bash yarn theme={null}
      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" />.

    <img src="https://mintcdn.com/trigger-docs-build-extensions/ZPd_QyYv8NlAc2Gq/images/test-page.png?fit=max&auto=format&n=ZPd_QyYv8NlAc2Gq&q=85&s=8a5d4de2905bc9f6df3b36108eb3d3f2" alt="Test page" width="2660" height="1720" data-path="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.

    <img src="https://mintcdn.com/trigger-docs-build-extensions/ZPd_QyYv8NlAc2Gq/images/run-page.png?fit=max&auto=format&n=ZPd_QyYv8NlAc2Gq&q=85&s=1a15f6945d94bccbc418f55149ba67a5" alt="Run page" width="2978" height="2110" data-path="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.

    <img src="https://mintcdn.com/trigger-docs-build-extensions/ZPd_QyYv8NlAc2Gq/images/terminal-completed-run.png?fit=max&auto=format&n=ZPd_QyYv8NlAc2Gq&q=85&s=06396e185cf257a9d30bdbd911ca01cb" alt="Terminal showing completed run" width="955" height="197" data-path="images/terminal-completed-run.png" />
  </Step>
</Steps>

## Create a new Supabase edge function and deploy it

<Steps>
  <Step title="Create a new Supabase edge function">
    We'll call this example `edge-function-trigger`.

    In your project, run the following command in the terminal using the Supabase CLI:

    ```bash theme={null}
    supabase functions new edge-function-trigger
    ```
  </Step>

  <Step title="Update the edge function code">
    Replace the placeholder code in your `edge-function-trigger/index.ts` file with the following:

    ```ts functions/edge-function-trigger/index.ts theme={null}
    // Setup type definitions for built-in Supabase Runtime APIs
    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

    Deno.serve(async () => {
      await tasks.trigger<typeof helloWorldTask>(
        // Your task id
        "hello-world",
        // Your task payload
        "Hello from a Supabase Edge Function!"
      );
      return new Response("OK");
    });
    ```

    <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">
    You can now deploy your edge function with the following command in your terminal:

    ```bash theme={null}
    supabase functions deploy edge-function-trigger --no-verify-jwt
    ```

    <Note>
      `--no-verify-jwt` removes the JSON Web Tokens requirement from the authorization header. By
      default this should be on, but it is not required for this example. Learn more about JWTs
      [here](https://supabase.com/docs/guides/auth/jwts).
    </Note>

    Follow the CLI instructions and once complete you should now 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>
</Steps>

## Set your Trigger.dev prod secret key in the Supabase dashboard

To trigger a task from your edge function, you need to set your Trigger.dev secret key in the Supabase dashboard.

To do this, first go to your Trigger.dev [project dashboard](https://cloud.trigger.dev) and copy the `prod` secret key from the API keys page.

<img src="https://mintcdn.com/trigger-docs-build-extensions/ZPd_QyYv8NlAc2Gq/images/api-key-prod.png?fit=max&auto=format&n=ZPd_QyYv8NlAc2Gq&q=85&s=874c0c1918ab47469e6751a3609c9e9d" alt="How to find your prod secret key" width="1624" height="924" data-path="images/api-key-prod.png" />

Then, in [Supabase](https://supabase.com/dashboard/projects), select your project, 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.

<img src="https://mintcdn.com/trigger-docs-build-extensions/ZPd_QyYv8NlAc2Gq/images/supabase-keys-1.png?fit=max&auto=format&n=ZPd_QyYv8NlAc2Gq&q=85&s=471f304e03dc3b1439c28e7fbdac2323" alt="Add secret key in Supabase" width="1624" height="924" data-path="images/supabase-keys-1.png" />

## Deploy your task and trigger it from your edge function

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

    <CodeGroup>
      ```bash npm theme={null}
      npx trigger.dev@latest deploy
      ```

      ```bash pnpm theme={null}
      pnpm dlx trigger.dev@latest deploy
      ```

      ```bash yarn theme={null}
      yarn dlx trigger.dev@latest deploy
      ```
    </CodeGroup>
  </Step>

  <Step title="Trigger a prod run from your deployed edge function">
    To do this all you need to do is simply open the `edge-function-trigger` URL.

    `https://supabase.com/dashboard/project/<your-project-id>/functions`

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

    In your Supabase project, go to your Edge function dashboard, find `edge-function-trigger`, copy the URL, and paste it into a new window in your browser.

    Once loaded you should see ‘OK’ on the new screen.

    <img src="https://mintcdn.com/trigger-docs-build-extensions/ZPd_QyYv8NlAc2Gq/images/supabase-function-url.png?fit=max&auto=format&n=ZPd_QyYv8NlAc2Gq&q=85&s=945c7fc931a620524f85d61ef5747078" alt="Edge function URL" width="1624" height="924" data-path="images/supabase-function-url.png" />

    The task will be triggered when your edge function URL is accessed.

    Check your [cloud.trigger.dev](http://cloud.trigger.dev) dashboard and you should see a succesful `hello-world` task.

    **Congratulations, you have run a simple Hello World task from a Supabase edge function!**
  </Step>
</Steps>

## Useful next steps

<CardGroup cols={2}>
  <Card title="Tasks overview" icon="diagram-subtask" href="/tasks-overview">
    Learn what tasks are and their options
  </Card>

  <Card title="Writing tasks" icon="pen-nib" href="/writing-tasks-introduction">
    Learn how to write your own tasks
  </Card>

  <Card title="Deploy using the CLI" icon="terminal" href="/cli-deploy">
    Learn how to deploy your task manually using the CLI
  </Card>

  <Card title="Deploy using GitHub actions" icon="github" href="/github-actions">
    Learn how to deploy your task using GitHub actions
  </Card>
</CardGroup>
