trigger.dev init
command. Follow our Quickstart for a more streamlined setup.
Prerequisites
- Node.js 18.20+ (or Bun runtime)
- A Trigger.dev account (sign up at trigger.dev)
- TypeScript 5.0.4 or later (for TypeScript projects)
CLI Authentication
Before setting up your project, you need to authenticate the CLI with Trigger.dev:proj_abc123
).
Package installation
Install the required packages based on your package manager:Environment variables
For local development, you need to set up theTRIGGER_SECRET_KEY
environment variable. This key authenticates your application with Trigger.dev.
- Go to your project dashboard in Trigger.dev
- Navigate to the “API Keys” page
- Copy the DEV secret key
- Add it to your local environment file:
Self-hosted instances
If you’re using a self-hosted Trigger.dev instance, also set:CLI setup
You can run the Trigger.dev CLI in two ways:Option 1: Using npx/pnpm dlx/yarn dlx
Option 2: Add as dev dependency
Add the CLI to yourpackage.json
:
package.json
:
Version pinning
Make sure to pin the version of the CLI to the same version as the SDK that you are using:dev
or deploy
commands, the CLI will automatically detect mismatched versions and warn you.
Configuration file
Create atrigger.config.ts
file in your project root (or trigger.config.mjs
for JavaScript projects):
Using the Bun runtime
By default, Trigger.dev will use the Node.js runtime. If you’re using Bun, you can specify the runtime:Add your first task
Create atrigger
directory (matching the dirs
in your config) and add an example task:
src/trigger/example.ts
TypeScript config
If you’re using TypeScript, addtrigger.config.ts
to your tsconfig.json
include array:
Git config
Add.trigger
to your .gitignore
file to exclude Trigger.dev’s local development files:
.gitignore
file, create one with this content.
React hooks setup
If you’re building a React frontend application and want to display task status in real-time, install the React hooks package:Installation
Basic usage
- Generate a Public Access Token in your backend:
- Use hooks to monitor tasks:
Build extensions
Build extensions allow you to customize the build process. Ensure you have the@trigger.dev/build
package installed in your project (see package installation).
Now you can use any of the built-in extensions:
Monorepo setup
There are two main approaches for setting up Trigger.dev in a monorepo:- Tasks as a package: Create a separate package for your Trigger.dev tasks that can be shared across apps
- Tasks in apps: Install Trigger.dev directly in individual apps that need background tasks
Approach 1: Tasks as a package (Turborepo)
This approach creates a dedicated tasks package that can be consumed by multiple apps in your monorepo.1. Set up workspace configuration
Rootpackage.json
:
pnpm-workspace.yaml
:
turbo.json
:
2. Create the tasks package
packages/tasks/package.json
:
packages/tasks/trigger.config.ts
:
packages/tasks/src/index.ts
:
packages/tasks/src/trigger/index.ts
:
packages/tasks/src/trigger/example.ts
:
3. Use tasks in your apps
apps/web/package.json
:
apps/web/app/api/actions.ts
:
4. Development workflow
Run the development server for the tasks package:Approach 2: Tasks in apps (Turborepo)
This approach installs Trigger.dev directly in individual apps that need background tasks.1. Install in your app
apps/web/package.json
:
2. Add configuration
apps/web/trigger.config.ts
:
3. Create tasks
apps/web/src/trigger/example.ts
:
4. Use tasks in your app
apps/web/app/api/actions.ts
: