|
| 1 | +--- |
| 2 | +title: "Triggering tasks with webhooks in Next.js" |
| 3 | +sidebarTitle: "Next.js webhooks" |
| 4 | +description: "Learn how to trigger a task from a webhook in a Next.js app." |
| 5 | +--- |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- [A Next.js project, set up with Trigger.dev](/guides/frameworks/nextjs) |
| 10 | +- [cURL](https://curl.se/) installed on your local machine. This will be used to send a POST request to your webhook handler. |
| 11 | + |
| 12 | +## Adding the webhook handler |
| 13 | + |
| 14 | +The webhook handler in this guide will be an API route. |
| 15 | + |
| 16 | +This will be different depending on whether you are using the Next.js pages router or the app router. |
| 17 | + |
| 18 | +### Pages router: creating the webhook handler |
| 19 | + |
| 20 | +Create a new file `pages/api/webhook-handler.ts` or `pages/api/webhook-hander.js`. |
| 21 | + |
| 22 | +In your new file, add the following code: |
| 23 | + |
| 24 | +```ts /pages/api/webhook-handler.ts |
| 25 | +import { helloWorldTask } from "@/trigger/example"; |
| 26 | +import { tasks } from "@trigger.dev/sdk/v3"; |
| 27 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 28 | + |
| 29 | +export default async function handler(req: NextApiRequest, res: NextApiResponse) { |
| 30 | + // Parse the webhook payload |
| 31 | + const payload = req.body; |
| 32 | + |
| 33 | + // Trigger the helloWorldTask with the webhook data as the payload |
| 34 | + await tasks.trigger<typeof helloWorldTask>("hello-world", payload); |
| 35 | + |
| 36 | + res.status(200).json({ message: "OK" }); |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +This code will handle the webhook payload and trigger the 'Hello World' task. |
| 41 | + |
| 42 | +### App router: creating the webhook handler |
| 43 | + |
| 44 | +Create a new file in the `app/api/webhook-handler/route.ts` or `app/api/webhook-handler/route.js`. |
| 45 | + |
| 46 | +In your new file, add the following code: |
| 47 | + |
| 48 | +```ts /app/api/webhook-handler/route.ts |
| 49 | +import type { helloWorldTask } from "@/trigger/example"; |
| 50 | +import { tasks } from "@trigger.dev/sdk/v3"; |
| 51 | +import { NextResponse } from "next/server"; |
| 52 | + |
| 53 | +export async function POST(req: Request) { |
| 54 | + // Parse the webhook payload |
| 55 | + const payload = await req.json(); |
| 56 | + |
| 57 | + // Trigger the helloWorldTask with the webhook data as the payload |
| 58 | + await tasks.trigger<typeof helloWorldTask>("hello-world", payload); |
| 59 | + |
| 60 | + return NextResponse.json("OK", { status: 200 }); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +This code will handle the webhook payload and trigger the 'Hello World' task. |
| 65 | + |
| 66 | +## Triggering the task locally |
| 67 | + |
| 68 | +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. |
| 69 | + |
| 70 | +<Steps> |
| 71 | + |
| 72 | +<Step title="Run your Next.js app and the Trigger.dev dev server"> |
| 73 | + |
| 74 | +First, run your Next.js app. |
| 75 | + |
| 76 | +<CodeGroup> |
| 77 | + |
| 78 | + ```bash npm |
| 79 | + npm run dev |
| 80 | + ``` |
| 81 | + |
| 82 | + ```bash pnpm |
| 83 | + pnpm run dev |
| 84 | + ``` |
| 85 | + |
| 86 | + ```bash yarn |
| 87 | + yarn dev |
| 88 | + ``` |
| 89 | + |
| 90 | +</CodeGroup> |
| 91 | + |
| 92 | +Then, open up a second terminal window and start the Trigger.dev dev server: |
| 93 | + |
| 94 | +<CodeGroup> |
| 95 | + |
| 96 | + ```bash npm |
| 97 | + npx trigger.dev@latest dev |
| 98 | + ``` |
| 99 | + |
| 100 | + ```bash pnpm |
| 101 | + pnpm dlx trigger.dev@latest dev |
| 102 | + ``` |
| 103 | + |
| 104 | + ```bash yarn |
| 105 | + yarn dlx trigger.dev@latest dev |
| 106 | + ``` |
| 107 | + |
| 108 | + </CodeGroup> |
| 109 | + |
| 110 | +</Step> |
| 111 | + |
| 112 | +<Step title="Trigger the webhook with some dummy data"> |
| 113 | + |
| 114 | +To send a POST request to your webhook handler, open up a terminal window on your local machine and run the following command: |
| 115 | + |
| 116 | +<Tip> |
| 117 | + If `http://localhost:3000` isn't the URL of your locally running Next.js app, replace the URL in |
| 118 | + the below command with that URL instead. |
| 119 | +</Tip> |
| 120 | + |
| 121 | +```bash |
| 122 | +curl -X POST -H "Content-Type: application/json" -d '{"Name": "John Doe", "Age": "87"}' http://localhost:3000/api/webhook-handler |
| 123 | +``` |
| 124 | + |
| 125 | +This will send a POST request to your webhook handler, with a JSON payload. |
| 126 | + |
| 127 | +</Step> |
| 128 | + |
| 129 | +<Step title="Check the task ran successfully"> |
| 130 | + |
| 131 | +After running the command, you should see a successful dev run and a 200 response in your terminals. |
| 132 | + |
| 133 | +If you now go to your [Trigger.dev dashboard](https://cloud.trigger.dev), 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"}`. |
| 134 | + |
| 135 | +</Step> |
| 136 | + |
| 137 | +</Steps> |
0 commit comments