There are currently four ways you can trigger any task from your own code:

FunctionWhere does this work?What it does
yourTask.trigger()AnywhereTriggers a task and gets a handle you can use to monitor and manage the run. It does not wait for the result.
yourTask.batchTrigger()AnywhereTriggers a task multiple times and gets a handle you can use to monitor and manage the runs. It does not wait for the results.
yourTask.triggerAndWait()Inside a taskTriggers a task and then waits until it’s complete. You get the result data to continue with.
yourTask.batchTriggerAndWait()Inside a taskTriggers a task multiple times in parallel and then waits until they’re all complete. You get the resulting data to continue with.

Additionally, scheduled tasks get automatically triggered on their schedule and webhooks when receiving a webhook.

From outside of a task

You can trigger any task from your backend code, using either trigger() or batchTrigger().

Do not trigger tasks directly from your frontend. If you do, you will leak your private Trigger.dev API key to the world.

You can use Next.js Server Actions but you need to be careful with bundling.

Authentication

When you trigger a task from your backend code, you need to set the TRIGGER_SECRET_KEY environment variable. You can find the value on the API keys page in the Trigger.dev dashboard. More info on API keys.

trigger()

Triggers a single run of a task with the payload you pass in, and any options you specify. It does NOT wait for the result, you cannot do that from outside a task.

batchTrigger()

Triggers multiples runs of a task with the payloads you pass in, and any options you specify. It does NOT wait for the results, you cannot do that from outside a task.

From inside a task

You can trigger tasks from other tasks using trigger() or batchTrigger(). You can also trigger and wait for the result of triggered tasks using triggerAndWait() and batchTriggerAndWait(). This is a powerful way to build complex tasks.

trigger()

This works the same as from outside a task. You call it and you get a handle back, but it does not wait for the result.

/trigger/my-task.ts
import { myOtherTask } from "~/trigger/my-other-task";

export const myTask = task({
  id: "my-task",
  run: async (payload: string) => {
    const handle = await myOtherTask.trigger({ payload: "some data" });

    //...do other stuff
  },
});

batchTrigger()

This works the same as from outside a task. You call it and you get a handle back, but it does not wait for the results.

/trigger/my-task.ts
import { myOtherTask } from "~/trigger/my-other-task";

export const myTask = task({
  id: "my-task",
  run: async (payload: string) => {
    const batchHandle = await myOtherTask.batchTrigger({ items: [{ payload: "some data" }] });

    //...do other stuff
  },
});

triggerAndWait()

This is where it gets interesting. You can trigger a task and then wait for the result. This is useful when you need to call a different task and then use the result to continue with your task.

/trigger/parent.ts
export const parentTask = task({
  id: "parent-task",
  run: async (payload: string) => {
    const result = await batchChildTask.triggerAndWait({ payload: "some-data" });
    console.log("Result", result);

    //...do stuff with the result
  },
});

batchTriggerAndWait()

You can batch trigger a task and wait for all the results. This is useful for the fan-out pattern, where you need to call a task multiple times and then wait for all the results to continue with your task.

/trigger/nested.ts
export const batchParentTask = task({
  id: "parent-task",
  run: async (payload: string) => {
    const results = await childTask.batchTriggerAndWait({
      items: [{ payload: "item4" }, { payload: "item5" }, { payload: "item6" }],
    });
    console.log("Results", results);

    //...do stuff with the result
  },
});

Next.js Server Actions

Server Actions allow you to call your backend code without creating API routes. This is very useful for triggering tasks but you need to be careful you don’t accidentally bundle the Trigger.dev SDK into your frontend code.

If you see an error like this then you’ve bundled @trigger.dev/sdk into your frontend code:

Module build failed: UnhandledSchemeError: Reading from "node:crypto" is not handled by plugins (Unhandled scheme).
Module build failed: UnhandledSchemeError: Reading from "node:process" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.

When you use server actions that use @trigger.dev/sdk:

  • The file can’t have any React components in it.
  • The file should have "use server" on the first line.

Here’s an example of how to do it with a component that calls the server action and the actions file: