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

<ParamField path="id" type="string" required={true}>
  An identifier for this trigger. Must unique across your Trigger.dev
  organization.
</ParamField>

<ParamField path="name" type="string" required={true}>
  A display name for this trigger.
</ParamField>

<ParamField path="apiKey" type="string" required={false}>
  Your Trigger.dev API key. If not provided, the API key will be read from the
  `TRIGGER_API_KEY` environment variable.
</ParamField>

<ParamField path="endpoint" type="string" required={false}>
  The URL of the Trigger.dev WebSocket server. If not provided, the endpoint
  will point to the production server.
</ParamField>

<ParamField path="logLevel" type="string" required={false}>
  The log level for the Trigger. Defaults to `info`
</ParamField>

<ParamField path="triggerTTL" type="number" required={false}>
  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.
</ParamField>

<ParamField path="run" type="function" required={true}>
  The function that will be executed when the trigger is fired. The function
  will be passed the event data and a context object.
</ParamField>

### Context Object

<ParamField path="id" type="string">
  The identifier of the individual run of this trigger.
</ParamField>

<ParamField path="environment" type="string">
  Either `development` or `live`, depending on the API Key used.
</ParamField>

<ParamField path="apiKey" type="string">
  The API Key used to authenticate this trigger.
</ParamField>

<ParamField path="organizationId" type="string">
  The identifier of the organization that owns this trigger.
</ParamField>

<ParamField path="logger" type="object">
  The logger object for this trigger. See the [Logging](/functions/logging) page
  for more info.
</ParamField>

<ParamField path="isTest" type="boolean">
  Whether or not this trigger is being run as a test.
</ParamField>

<ParamField path="sendEvent" type="function">
  A function that can be used to send an event to trigger.dev. See the [Sending
  Events](/functions/send-event) page for more info.
</ParamField>

<ParamField path="waitFor" type="function">
  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.
</ParamField>

<ParamField path="waitUntil" type="function">
  A function that can be used to wait for a specific date/time before
  continuing. See the [Delays](/functions/delays) page for more info.
</ParamField>

<ParamField path="fetch" type="function">
  A function that can be used to make HTTP requests. See the
  [fetch](/functions/fetch) page for more info.
</ParamField>

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

<CodeGroup>

```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();
```

</CodeGroup>
