Skip to content

Commit 349f2ba

Browse files
committed
Added nextjs webhook guide
1 parent 886429b commit 349f2ba

File tree

2 files changed

+145
-1
lines changed

2 files changed

+145
-1
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: "Setting up webhooks"
3+
sidebarTitle: "Webhooks"
4+
description: "Guides and examples for using webhooks with Trigger.dev."
5+
icon: "webhook"
6+
---
7+
8+
## Overview
9+
10+
Webhooks are a way to send and receive events from external services. Triggering tasks using webhooks allow you to add real-time, event driven functionality to your app.
11+
12+
A webhook handler is code that executes in response to an event. They can be endpoints in your framework's routing which will be triggered by an external service, for example.
13+
14+
In this guide you'll learn how to set up webhook handlers and trigger tasks from them using popular frameworks.
15+
16+
## Frameworks featured in this guide
17+
18+
- [Next.js](/guides/frameworks/webhooks#nextjs-webhook-configuration), pages or app router
19+
- [Remix](/guides/frameworks/webhooks#remix-webhook-configuration)
20+
21+
<Note>
22+
If you would like to see a webhook guide for your framework, please request it in our [Discord
23+
server](https://trigger.dev/discord).
24+
</Note>
25+
26+
## Next.js webhook configuration
27+
28+
### Prerequisites
29+
30+
- [A Next.js project, set up with Trigger.dev](/guides/frameworks/nextjs)
31+
- [cURL](https://curl.se/) installed on your local machine. This will be used to send a POST request to your webhook handler.
32+
33+
### Adding a webhook handler
34+
35+
The webhook handler will be an API route. The location of the route file will be different depending on whether you are using the pages router or the app router.
36+
37+
- **Pages router** - create a new file `pages/api/webhook-handler.ts` or `pages/api/webhook-hander.js`.
38+
39+
- **App router** - create a new file in the `app/api/webhook-handler/route.ts` or `app/api/webhook-handler/route.js`.
40+
41+
The handler code will be the same for both routers. In this guide will use the 'Hello World' task as the example.
42+
43+
In your new file, add the following code:
44+
45+
```ts
46+
import type { helloWorldTask } from "@/trigger/example";
47+
import { tasks } from "@trigger.dev/sdk/v3";
48+
import { NextResponse } from "next/server";
49+
50+
export async function POST(req: Request) {
51+
try {
52+
// Parse the webhook payload
53+
const webhookData = await req.json();
54+
55+
// Trigger the helloWorldTask with the webhook data as the payload
56+
const handle = await tasks.trigger<typeof helloWorldTask>("hello-world", webhookData);
57+
58+
return NextResponse.json({ result: handle, ok: true });
59+
} catch (error) {
60+
console.error(error);
61+
return NextResponse.json({ message: "something went wrong", ok: false }, { status: 500 });
62+
}
63+
}
64+
```
65+
66+
### Triggering a task locally
67+
68+
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.
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 with
118+
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>
138+
139+
## Remix webhook configuration
140+
141+
### Prerequisites
142+
143+
- [A Remix project, set up with Trigger.dev](/guides/frameworks/remix)

docs/mint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@
285285
"guides/frameworks/supabase-edge-functions-basic",
286286
"guides/frameworks/supabase-edge-functions-database-webhooks"
287287
]
288-
}
288+
},
289+
"guides/frameworks/webhooks"
289290
]
290291
},
291292
{

0 commit comments

Comments
 (0)