Skip to content

Commit 74b5660

Browse files
committed
Added webhook handler for pages and split the docs
1 parent 329b6ba commit 74b5660

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

docs/guides/frameworks/nextjs-webhooks.mdx

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,41 @@ description: "Learn how to trigger a task from a webhook in a Next.js app."
1111

1212
## Adding the webhook handler
1313

14-
The webhook handler in this guide will be an API route. The location of the route file will be different depending on whether you are using the Next.js pages router or the app router.
14+
The webhook handler in this guide will be an API route.
1515

16-
- **Pages router** - create a new file `pages/api/webhook-handler.ts` or `pages/api/webhook-hander.js`.
16+
This will be different depending on whether you are using the Next.js pages router or the app router.
1717

18-
- **App router** - create a new file in the `app/api/webhook-handler/route.ts` or `app/api/webhook-handler/route.js`.
18+
### Pages router: creating the webhook handler
1919

20-
The handler code will be the same for both routers. In this guide will use the 'Hello World' task as the example.
20+
Create a new file `pages/api/webhook-handler.ts` or `pages/api/webhook-hander.js`.
2121

2222
In your new file, add the following code:
2323

24-
```ts
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
2549
import type { helloWorldTask } from "@/trigger/example";
2650
import { tasks } from "@trigger.dev/sdk/v3";
2751
import { NextResponse } from "next/server";
@@ -41,7 +65,7 @@ This code will handle the webhook payload and trigger the 'Hello World' task.
4165

4266
## Triggering the task locally
4367

44-
Now that you have a webhook handler set up, you can trigger the 'Hello World' task from it. We will do this locally using cURL.
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.
4569

4670
<Steps>
4771

0 commit comments

Comments
 (0)