Jobs are the core of the system. They allow you to run code when some event occurs. They are built using a combination of Triggers and Tasks.

Pre-requisites

Make sure your Project is set up with Trigger.dev. We recommend using the CLI to do this.

How to write a Job in code

1. Create a Job file in your Project

This is where you will write your Job code. E.g. my-job.ts.

//this path might be different depending on your project
import { client } from "@/trigger";

client.defineJob({
  // This is the unique ID for your Job's end-point
  id: "your-job-id",
  // This is the name of your Job
  name: "Your Job name",
  // This is the version of our SDK you are using
  version: "0.0.1",
  ...

The id and name are important because they are used to create and identify your Job in the app.

This Job must be imported in the trigger file in order to be registered when the CLI dev command is run. If you’re using Next.js, this can be found in either the app/api/trigger/route.ts file for projects using the App Router, or pages/api/trigger.ts if you’re using the Pages Router.

2. Choose a Trigger

This is what kicks-off a Job. There are a few different types of Triggers you can use:

  • Scheduled

  • Webhook

  • Event

  • Dynamic

Run a Job on a repeating schedule, using intervalTrigger

client.defineJob({
...
trigger: intervalTrigger({
    seconds: 60,
  }),
...

Or with CRON syntax, using cronTrigger:

client.defineJob({
...
trigger: cronTrigger({
    cron: "30 14 * * 1",
  }),
...

3. Create the Job Tasks

A Task is a resumable unit of a Run that can be retried, resumed and is logged.

You can use just regular code in your Jobs. But you don’t get the benefits of retrying, logging and resumability. More info on Tasks vs regular code.

You can string together multiple Tasks and regular code in any order you want.

Useful built-in Tasks:

TaskDescriptionTask code
DelayWait for a period of timeawait io.wait("wait", 60);
LogLog a messageawait io.logger.log("Hello");
Send EventSend an event (for eventTrigger)await io.sendEvent("my-event", { name: "my.event", payload: { hello: "world" } });
Run taskWrap your own code in this to create a Taskawait io.runTask("My Task", async () => { console.log("Hello"); });
Background fetchFetch data from a URL that can take longer that the serverless timeout.await io.backgroundFetch("fetch-some-data", { url: "https://example.com" });

For a full list of built-in Tasks, see the io SDK reference.

Integration Task examples:

To use our integrations you will need to set them up in the app first. Our guide is here.

These are just a few examples of Integration Tasks. For many more, browse our Integrations section.

4. Register your Jobs

While your app is running, open a new terminal window or tab and run:

npx @trigger.dev/cli@latest dev

This will register all of your Jobs, they should appear in your dashboard.

Not seeing your Job in the web app? It might be because you forgot to import it. This will need to be either in app/api/trigger/route.ts file if you’re using the Next,js App Router, or pages/api/trigger.ts if you’re using the Next,js Pages Router.

If you are having trouble getting your job running, please reach out to us and we will help you fix any issues:


Next steps

We recommend exploring all of the below sections to fully understand how to create and run Jobs using Trigger.dev.