Skip to content

Commit e635a9a

Browse files
committed
Updated guides and simplified example code
1 parent 770e117 commit e635a9a

File tree

3 files changed

+93
-28
lines changed

3 files changed

+93
-28
lines changed

docs/guides/frameworks/nextjs-webhooks.mdx

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ description: "Learn how to trigger a task from a webhook in a Next.js app."
99
- [A Next.js project, set up with Trigger.dev](/guides/frameworks/nextjs)
1010
- [cURL](https://curl.se/) installed on your local machine. This will be used to send a POST request to your webhook handler.
1111

12-
## Adding a webhook handler
12+
## Adding the webhook handler
1313

14-
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.
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.
1515

1616
- **Pages router** - create a new file `pages/api/webhook-handler.ts` or `pages/api/webhook-hander.js`.
1717

@@ -27,22 +27,19 @@ import { tasks } from "@trigger.dev/sdk/v3";
2727
import { NextResponse } from "next/server";
2828

2929
export async function POST(req: Request) {
30-
try {
31-
// Parse the webhook payload
32-
const webhookData = await req.json();
33-
34-
// Trigger the helloWorldTask with the webhook data as the payload
35-
const handle = await tasks.trigger<typeof helloWorldTask>("hello-world", webhookData);
36-
37-
return NextResponse.json({ result: handle, ok: true });
38-
} catch (error) {
39-
console.error(error);
40-
return NextResponse.json({ message: "something went wrong", ok: false }, { status: 500 });
41-
}
30+
// Parse the webhook payload
31+
const payload = await req.json();
32+
33+
// Trigger the helloWorldTask with the webhook data as the payload
34+
await tasks.trigger<typeof helloWorldTask>("hello-world", payload);
35+
36+
return NextResponse.json("OK", { status: 200 });
4237
}
4338
```
4439

45-
## Triggering a task locally
40+
This code will handle the webhook payload and trigger the 'Hello World' task.
41+
42+
## Triggering the task locally
4643

4744
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.
4845

@@ -90,11 +87,11 @@ Then, open up a second terminal window and start the Trigger.dev dev server:
9087

9188
<Step title="Trigger the webhook with some dummy data">
9289

93-
To send a POST request to your webhook handler, open up a terminal window on your local machine and run the following command:π
90+
To send a POST request to your webhook handler, open up a terminal window on your local machine and run the following command:
9491

9592
<Tip>
96-
If `http://localhost:3000` isn't the URL of your locally running Next.js app, replace the URL with
97-
that URL instead.
93+
If `http://localhost:3000` isn't the URL of your locally running Next.js app, replace the URL in
94+
the below command with that URL instead.
9895
</Tip>
9996

10097
```bash

docs/guides/frameworks/remix-webhooks.mdx

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ description: "Learn how to trigger a task from a webhook in a Remix app."
99
- [A Remix project, set up with Trigger.dev](/guides/frameworks/remix)
1010
- [cURL](https://curl.se/) installed on your local machine. This will be used to send a POST request to your webhook handler.
1111

12-
## Adding a webhook handler
12+
## Adding the webhook handler
1313

14-
The webhook handler will be an API route. Create a new file in `app/routes/api.webhook-handler.ts` or `app/routes/api.webhook-handler.js`.
14+
The webhook handler in this guide will be an API route. Create a new file `app/routes/api.webhook-handler.ts` or `app/routes/api.webhook-handler.js`.
1515

1616
In your new file, add the following code:
1717

@@ -20,19 +20,87 @@ import type { ActionFunctionArgs } from "@remix-run/node";
2020
import { tasks } from "@trigger.dev/sdk/v3";
2121
import { helloWorldTask } from "src/trigger/example";
2222

23-
// This function can handle GET requests to check if the webhook is active in the browser - not required for Trigger but helpful for testing
24-
export async function loader() {
25-
return new Response("Webhook endpoint is active", { status: 200 });
26-
}
27-
28-
// This function handles the actual webhook payload and triggers the helloWorldTask
2923
export async function action({ request }: ActionFunctionArgs) {
3024
const payload = await request.json();
31-
console.log("Received webhook payload:", payload);
3225

3326
// Trigger the helloWorldTask with the webhook data as the payload
3427
await tasks.trigger<typeof helloWorldTask>("hello-world", payload);
3528

3629
return new Response("OK", { status: 200 });
3730
}
3831
```
32+
33+
This code will handle the webhook payload and trigger the 'Hello World' task.
34+
35+
## Triggering the task locally
36+
37+
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.
38+
39+
<Steps>
40+
41+
<Step title="Run your Remix app and the Trigger.dev dev server">
42+
43+
First, run your Remix app.
44+
45+
<CodeGroup>
46+
47+
```bash npm
48+
npm run dev
49+
```
50+
51+
```bash pnpm
52+
pnpm run dev
53+
```
54+
55+
```bash yarn
56+
yarn dev
57+
```
58+
59+
</CodeGroup>
60+
61+
Then, open up a second terminal window and start the Trigger.dev dev server:
62+
63+
<CodeGroup>
64+
65+
```bash npm
66+
npx trigger.dev@latest dev
67+
```
68+
69+
```bash pnpm
70+
pnpm dlx trigger.dev@latest dev
71+
```
72+
73+
```bash yarn
74+
yarn dlx trigger.dev@latest dev
75+
```
76+
77+
</CodeGroup>
78+
79+
</Step>
80+
81+
<Step title="Trigger the webhook with some dummy data">
82+
83+
To send a POST request to your webhook handler, open up a terminal window on your local machine and run the following command:
84+
85+
<Tip>
86+
If `http://localhost:5173` isn't the URL of your locally running Remix app, replace the URL in the
87+
below command with that URL instead.
88+
</Tip>
89+
90+
```bash
91+
curl -X POST -H "Content-Type: application/json" -d '{"Name": "John Doe", "Age": "87"}' http://localhost:5173/api/webhook-handler
92+
```
93+
94+
This will send a POST request to your webhook handler, with a JSON payload.
95+
96+
</Step>
97+
98+
<Step title="Check the task ran successfully">
99+
100+
After running the command, you should see a successful dev run and a 200 response in your terminals.
101+
102+
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"}`.
103+
104+
</Step>
105+
106+
</Steps>

docs/guides/frameworks/webhooks-guides-overview.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: "Guides for using webhooks with Trigger.dev."
88

99
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.
1010

11-
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.
11+
A webhook handler is code that executes in response to an event. They can be endpoints in your framework's routing which can be triggered by an external service.
1212

1313
## Webhook guides
1414

0 commit comments

Comments
 (0)