Installing Required Packages

Start by installing the necessary packages in your Express.js project directory. You can use npm, pnpm, or yarn as your package manager.

npm install @trigger.dev/sdk @trigger.dev/express

Ensure that you execute this command within a Express project.

Obtaining the Development Server API Key

To locate your development Server API key, login to the Trigger.dev dashboard and select the Project you want to connect to. Then click on the Environments & API Keys tab in the left menu. You can copy your development Server API Key from the field at the top of this page. (Your development key will start with tr_dev_).

Adding Environment Variables

Create a .env file at the root of your project and include your Trigger API key and URL like this:

TRIGGER_API_KEY=ENTER_YOUR_DEVELOPMENT_API_KEY_HERE
TRIGGER_API_URL=https://api.trigger.dev # this is only necessary if you are self-hosting

Replace ENTER_YOUR_DEVELOPMENT_API_KEY_HERE with the actual API key obtained from the previous step.

Configuring the Trigger Client

Create a file for your Trigger client, in this case we create it at <root>/trigger.(ts/js)

trigger.(ts/js)
import { TriggerClient } from "@trigger.dev/sdk";

export const client = new TriggerClient({
  id: "my-app",
  apiKey: process.env.TRIGGER_API_KEY!,
  apiUrl: process.env.TRIGGER_API_URL!,
});

Replace "my-app" with an appropriate identifier for your project.

Adding the API endpoint

There are a few different options depending on how your Express project is configured.

  • App middleware
  • Entire app for Trigger.dev (only relevant if it's the only thing your project is for)

Select the appropriate code example from below:

//import the client from the other file
import { client } from "./trigger";
import { createMiddleware } from "@trigger.dev/express";

//import your job files
import "./jobs/example";

//..your existing Express code
const app: Express = express();

//add the middleware
app.use(createMiddleware(client));
// app.use(express.json()); //if you're parsing JSON, you need to add the Trigger middleware before this

//..the rest of your Express code

Creating the Example Job

Create a Job file. In this case created <root>/jobs/example.(ts/js)

jobs/example.(ts/js)
import { eventTrigger } from "@trigger.dev/sdk";
import { client } from "../trigger";

// your first job
client.defineJob({
  id: "example-job",
  name: "Example Job",
  version: "0.0.1",
  trigger: eventTrigger({
    name: "example.event",
  }),
  run: async (payload, io, ctx) => {
    await io.logger.info("Hello world!", { payload });

    return {
      message: "Hello world!",
    };
  },
});

Adding Configuration to package.json

Inside the package.json file, add the following configuration under the root object:

"trigger.dev": {
  "endpointId": "my-app"
}

Replace "my-app" with the appropriate identifier you used in the trigger.js configuration file.

Running

Run your Express app

Run your Express app locally, like you normally would. For example:

npm run dev

You might use npm run start instead of dev

Run the CLI 'dev' command

In a separate terminal window or tab run:

npx @trigger.dev/cli@latest dev

You can optionally pass the port if you're not running on 3000 by adding --port 3001 to the end

You can optionally pass the hostname if you're not running on localhost by adding --hostname <host>. Example, in case your Express is running on 0.0.0.0: --hostname 0.0.0.0.