This guide can be followed for both App and Pages router as well as Server Actions.
Initial setup
Set your secret key locally
Set yourTRIGGER_SECRET_KEY environment variable in your .env.local file if using the Next.js App router or .env file if using Pages router. This key is used to authenticate with Trigger.dev, so you can trigger runs from your Next.js app. Visit the API Keys page in the dashboard and select the DEV secret key.
For more information on authenticating with Trigger.dev, see the API keys page.
Triggering your task in Next.js
Here are the steps to trigger your task in the Next.js App and Pages router and Server Actions. Alternatively, check out this repo for a full working example of a Next.js app with a Trigger.dev task triggered using a Server Action.- App Router
- App Router (Server Actions)
- Pages Router
1
Create a Route Handler
Add a Route Handler by creating a
route.ts file (or route.js file) in the app/api directory like this: app/api/hello-world/route.ts.2
Add your task
Add this code to your
route.ts file which imports your task along with NextResponse to handle the API route response:app/api/hello-world/route.ts
3
Trigger your task
Troubleshooting & extra resources
Revalidation from your Trigger.dev tasks
Revalidation allows you to purge the cache for an ISR route. To revalidate an ISR route from a Trigger.dev task, you have to set up a handler for therevalidate event. This is an API route that you can add to your Next.js app.
This handler will run the revalidatePath function from Next.js, which purges the cache for the given path.
The handlers are slightly different for the App and Pages router:
Revalidation handler: App Router
If you are using the App router, create a new revalidation route atapp/api/revalidate/path/route.ts:
app/api/revalidate/path/route.ts
Revalidation handler: Pages Router
If you are using the Pages router, create a new revalidation route atpages/api/revalidate/path.ts:
pages/api/revalidate/path.ts
Revalidation task
This task takes apath as a payload and will revalidate the path you specify, using the handler you set up previously.
To run this task locally you will need to set the
REVALIDATION_SECRET environment variable in your .env.local file (or .env file if using Pages router).To run this task in production, you will need to set the REVALIDATION_SECRET environment variable in Vercel, in your project settings, and also in your environment variables in the Trigger.dev dashboard.trigger/revalidate-path.ts
Testing the revalidation task
You can test your revalidation task in the Trigger.dev dashboard on the testing page, using the following payload.Additional resources for Next.js
Next.js - triggering tasks using webhooks
How to create a webhook handler in a Next.js app, and trigger a task from it.

