Set your TRIGGER_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.
Add a Route Handler at app/api/hello-world/route.ts:
app/api/hello-world/route.ts
import type { helloWorldTask } from "@/trigger/example";import { tasks } from "@trigger.dev/sdk";import { NextResponse } from "next/server";// tasks.trigger also works with the edge runtime:// export const runtime = "edge";export async function GET() { const handle = await tasks.trigger<typeof helloWorldTask>( "hello-world", "James" ); return NextResponse.json(handle);}
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.
1
Create an actions.ts file
Create app/actions.ts and add a server action that triggers your task:
app/actions.ts
"use server";import type { helloWorldTask } from "@/trigger/example";import { tasks } from "@trigger.dev/sdk";export async function myTask() { try { const handle = await tasks.trigger<typeof helloWorldTask>( "hello-world", "James" ); return { handle }; } catch (error) { console.error(error); return { error: "something went wrong" }; }}
Before deploying, add any environment variables your tasks need to the Trigger.dev dashboard under
Environment Variables. Your TRIGGER_SECRET_KEY is added automatically when you deploy.
If you deploy to Vercel, you can sync environment variables from your Vercel project to Trigger.dev
automatically using the syncVercelEnvVars 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.
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):
If you see an authentication error when triggering tasks locally, check that TRIGGER_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.
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.