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

<CodeGroup>

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

```bash pnpm
pnpm install @trigger.dev/sdk @trigger.dev/express
```

```bash yarn
yarn add @trigger.dev/sdk @trigger.dev/express
```

</CodeGroup>

<br />

<Note>Ensure that you execute this command within a Express project.</Note>

## Obtaining the Development Server API Key

To locate your development Server API key, login to the [Trigger.dev
dashboard](https://cloud.trigger.dev) 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:

```bash
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)`

```ts 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:

<CodeGroup>

```typescript app middleware
//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
```

```typescript entire app
//if the entire app is just for Trigger.dev
import { client } from "./trigger";
import { createExpressServer } from "@trigger.dev/express";

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

//this creates an app
createExpressServer(client);
```

</CodeGroup>

## Creating the Example Job

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

```typescript 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:

```json
"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:

<CodeGroup>

```bash npm
npm run dev
```

```bash pnpm
pnpm run dev
```

```bash yarn
yarn run dev
```

</CodeGroup>

<Note>You might use `npm run start` instead of dev</Note>

### Run the CLI 'dev' command

In a **_separate terminal window or tab_** run:

<CodeGroup>

```bash npm
npx @trigger.dev/cli@latest dev
```

```bash pnpm
pnpm dlx @trigger.dev/cli@latest dev
```

```bash yarn
yarn dlx @trigger.dev/cli@latest dev
```

</CodeGroup>
<br />
<Note>
  You can optionally pass the port if you're not running on 3000 by adding
  `--port 3001` to the end
</Note>

<Note>
  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`.
</Note>
