Custom webhook handler with Next.js
Handle a webhook in your own Next.js API route and trigger a task from it.
Trigger.dev has built-in webhook endpoints that verify and route events for you. See Webhooks. This guide is for when you want to handle webhooks in your own app and trigger tasks yourself.
Prerequisites
- A Next.js project, set up with Trigger.dev
- cURL installed on your local machine. This will be used to send a POST request to your webhook handler.
GitHub repo
View the project on GitHub
Click here to view the full code for this project in our examples repository on GitHub. You can fork it and use it as a starting point for your own project.
Deploy this example to your project
Copies my-app from GitHub, installs it, and deploys its tasks to your project’s production environment.
npx degit triggerdotdev/examples/nextjs-webhooks/my-app my-app
cd my-app
npm install
npx trigger.dev@latest login
npx trigger.dev@latest deploy --project-ref <your-project-ref>If the example has a .env.example, set those variables in your project’s environment variables before deploying. To run the tasks on your machine instead, use dev in place of deploy.
Adding the webhook handler
The webhook handler in this guide will be an API route.
This will be different depending on whether you are using the Next.js pages router or the app router.
Pages router: creating the webhook handler
Create a new file pages/api/webhook-handler.ts or pages/api/webhook-hander.js.
In your new file, add the following code:
import { } from "@/trigger/example";
import { } from "@trigger.dev/sdk";
import type { NextApiRequest, } from "next";
export default async function handler(req: NextApiRequest, res: ) {
// Parse the webhook payload
const payload = req.body;
// Trigger the helloWorldTask with the webhook data as the payload
await .trigger<typeof >("hello-world", payload);
res.status(200).({ message: "OK" });
}This code will handle the webhook payload and trigger the 'Hello World' task.
App router: creating the webhook handler
Create a new file in the app/api/webhook-handler/route.ts or app/api/webhook-handler/route.js.
In your new file, add the following code:
import type { } from "@/trigger/example";
import { } from "@trigger.dev/sdk";
import { } from "next/server";
export async function POST(req: Request) {
// Parse the webhook payload
const payload = await req.();
// Trigger the helloWorldTask with the webhook data as the payload
await .trigger<typeof >("hello-world", payload);
return .("OK", { status: 200 });
}This code will handle the webhook payload and trigger the 'Hello World' task.
Triggering the task locally
Now that you have your webhook handler set up, you can trigger the 'Hello World' task from it. We will do this locally using cURL.
First, run your Next.js app.
npm run devpnpm run devyarn devThen, open up a second terminal window and start the Trigger.dev dev server:
npx trigger.dev@latest devpnpm dlx trigger.dev@latest devyarn dlx trigger.dev@latest devTo send a POST request to your webhook handler, open up a terminal window on your local machine and run the following command:
If http://localhost:3000 isn't the URL of your locally running Next.js app, replace the URL in
the below command with that URL instead.
curl -X POST -H "Content-Type: application/json" -d '{"Name": "John Doe", "Age": "87"}' http://localhost:3000/api/webhook-handlerThis will send a POST request to your webhook handler, with a JSON payload.
After running the command, you should see a successful dev run and a 200 response in your terminals.
If you now go to your Trigger.dev dashboard, you should also see a successful run for the 'Hello World' task, with the payload you sent, in this case; {"name": "John Doe", "age": "87"}.
Learn more about Next.js and Trigger.dev
Walk-through guides from development to deployment
Next.js - setup guide
Learn how to setup Trigger.dev with Next.js, using either the pages or app router.
Custom webhook handler with Next.js
Learn how to create a webhook handler for incoming webhooks in a Next.js app, and trigger a task from it.
Task examples
Fal.ai with Realtime in Next.js
Generate an image from a prompt using Fal.ai and Trigger.dev Realtime.
Generate a cartoon using Fal.ai in Next.js
Convert an image to a cartoon using Fal.ai.
Vercel sync environment variables
Learn how to automatically sync environment variables from your Vercel projects to Trigger.dev.
Vercel AI SDK
Learn how to use the Vercel AI SDK, which is a simple way to use AI models from different providers, including OpenAI, Anthropic, Amazon Bedrock, Groq, Perplexity etc.
Was this page helpful?
Last updated
Edit on GitHub