1. SDK Reference
  2. Trigger

Constructor

Usage

import { customEvent, Trigger } from "@trigger.dev/sdk";

const trigger = new Trigger({
  id: "user-created-notify-slack",
  name: "User Created - Notify Slack",
  on: customEvent({
    name: "user.created",
    schema: z.object({ id: z.string(), admin: z.boolean() }),
    filter: {
      admin: [false],
    },
  }),
  run: async (event, ctx) => {},
});

Options

idRequired
string

An identifier for this trigger. Must unique across your Trigger.dev organization.

nameRequired
string

A display name for this trigger.

apiKey
string

Your Trigger.dev API key. If not provided, the API key will be read from the TRIGGER_API_KEY environment variable.

endpoint
string

The URL of the Trigger.dev WebSocket server. If not provided, the endpoint will point to the production server.

logLevel
string

The log level for the Trigger. Defaults to info

triggerTTL
number

The TTL for the trigger in seconds. If the delay between the event timestamp and the current time is greater than the TTL, the trigger will not be executed. Defaults to 3600 seconds, or 1 hour.

runRequired
function

The function that will be executed when the trigger is fired. The function will be passed the event data and a context object.

Context Object

id
string

The identifier of the individual run of this trigger.

environment
string

Either development or live, depending on the API Key used.

apiKey
string

The API Key used to authenticate this trigger.

organizationId
string

The identifier of the organization that owns this trigger.

logger
object

The logger object for this trigger. See the Logging page for more info.

isTest
boolean

Whether or not this trigger is being run as a test.

sendEvent
function

A function that can be used to send an event to trigger.dev. See the Sending Events page for more info.

waitFor
function

A function that can be used to wait for a specific amount of time before continuing. See the Delays page for more info.

waitUntil
function

A function that can be used to wait for a specific date/time before continuing. See the Delays page for more info.

fetch
function

A function that can be used to make HTTP requests. See the fetch page for more info.

listen

The listen function will register the Trigger with Trigger.dev and start listening for events.

Usage

const trigger = new Trigger({
  id: "user-created-notify-slack",
  name: "User Created - Notify Slack",
  on: customEvent({
    name: "user.created",
    schema: z.object({ id: z.string(), admin: z.boolean() }),
    filter: {
      admin: [false],
    },
  }),
  run: async (event, ctx) => {},
});

trigger.listen();

You can split the creation of the trigger and the listening for events by calling trigger.listen() at a later time.

export const userCreatedNotifySlack = new Trigger({
  id: "user-created-notify-slack",
  name: "User Created - Notify Slack",
  on: customEvent({
    name: "user.created",
    schema: z.object({ id: z.string(), admin: z.boolean() }),
    filter: {
      admin: [false],
    },
  }),
  run: async (event, ctx) => {},
});