--- title: "Trigger" sidebarTitle: "Trigger" description: "The Trigger class let's you define a workflow that is triggered by a specific event." --- ## Constructor ### Usage ```ts 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 An identifier for this trigger. Must unique across your Trigger.dev organization. A display name for this trigger. Your Trigger.dev API key. If not provided, the API key will be read from the `TRIGGER_API_KEY` environment variable. The URL of the Trigger.dev WebSocket server. If not provided, the endpoint will point to the production server. The log level for the Trigger. Defaults to `info` 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. 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 The identifier of the individual run of this trigger. Either `development` or `live`, depending on the API Key used. The API Key used to authenticate this trigger. The identifier of the organization that owns this trigger. The logger object for this trigger. See the [Logging](/functions/logging) page for more info. Whether or not this trigger is being run as a test. A function that can be used to send an event to trigger.dev. See the [Sending Events](/functions/send-event) page for more info. A function that can be used to wait for a specific amount of time before continuing. See the [Delays](/functions/delays) page for more info. A function that can be used to wait for a specific date/time before continuing. See the [Delays](/functions/delays) page for more info. A function that can be used to make HTTP requests. See the [fetch](/functions/fetch) page for more info. ## listen The `listen` function will register the Trigger with Trigger.dev and start listening for events. ### Usage ```ts 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. ```ts triggers.ts 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) => {}, }); ``` ```ts index.ts import { userCreatedNotifySlack } from "./triggers"; userCreatedNotifySlack.listen(); ```