This guide applies to both the App Router and Pages Router, as well as Server Actions.
Prerequisites
- A Next.js project (v13 or later recommended)
- A Trigger.dev account with a project created
- Node.js 18 or later
Initial setup
1
Run the CLI init command
In the root of your Next.js project, run the following command to initialize Trigger.dev:This will create a
trigger.config.ts file and an example task at src/trigger/example.ts.2
Start the dev server
Run the Trigger.dev dev server alongside your Next.js app:
Run
next dev and trigger.dev dev concurrently. You can use a tool like
concurrently or simply open two terminal tabs.3
Run a test
Open the Trigger.dev dashboard, navigate to your project, and click
Test in the sidebar. Select the
hello-world task and click Run test.4
View your run
After triggering a test, click through to the run page to see logs, status, and the output of
your task in real time.
Set your secret key locally
Set yourTRIGGER_SECRET_KEY environment variable in your .env.local file (App Router) or .env
file (Pages Router). This key authenticates requests from your Next.js app to Trigger.dev.
Find your DEV secret key on the API Keys page of the dashboard:
Find your DEV API key under API Keys in the Trigger.dev dashboard. For more details on key types, see the Introduction.
Triggering tasks from Next.js
- App Router
- App Router (Server Actions)
- Pages Router
1
Create a Route Handler
Add a Route Handler at
app/api/hello-world/route.ts:app/api/hello-world/route.ts
2
Test your route
Start both servers, then visit
http://localhost:3000/api/hello-world in your browser. The
task will be triggered and the run ID will be returned as JSON.Add environment variables
Before deploying, add any environment variables your tasks need to the Trigger.dev dashboard under Environment Variables. YourTRIGGER_SECRET_KEY is added automatically when you deploy.
Deploying your task
Deploy your Trigger.dev tasks independently of your Next.js app:Automatically sync Vercel environment variables (optional)
If you deploy to Vercel, you can sync environment variables from your Vercel project to Trigger.dev automatically using thesyncVercelEnvVars build extension.
You need to set
VERCEL_ACCESS_TOKEN and VERCEL_PROJECT_ID environment variables (and
optionally VERCEL_TEAM_ID for team projects). You can generate a token in your Vercel account
settings.trigger.config.ts
Troubleshooting
ISR revalidation from a Trigger.dev task
ISR revalidation purges the Next.js cache for a specific path. Because tasks run outside Next.js, you need to call a revalidation API route from your task. App Router handler (app/api/revalidate/path/route.ts):
app/api/revalidate/path/route.ts
pages/api/revalidate/path.ts):
pages/api/revalidate/path.ts
trigger/revalidate-path.ts
Missing API key error
If you see an authentication error when triggering tasks locally, check thatTRIGGER_SECRET_KEY is
set in your .env.local (App Router) or .env (Pages Router) file and that the value matches the
DEV secret key in the dashboard.
Button onClick not working with Server Actions
Make sure your page or component file includes"use client"; at the top when using onClick
handlers that call server actions. Server actions themselves must have "use server"; at the top.
Additional resources
Build extensions
Add Python, browsers, FFmpeg, and other system dependencies to your tasks
Deployment guide
Learn how to deploy your tasks to production