-
-
Notifications
You must be signed in to change notification settings - Fork 722
Webhook guides #1396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Webhook guides #1396
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
349f2ba
Added nextjs webhook guide
D-K-P 605ba79
Updated intro page
D-K-P dbfbcc2
Copy improvement
D-K-P f9ea034
Added repo links to both supabase guides
D-K-P 770e117
Updated structure and added an overview page
D-K-P e635a9a
Updated guides and simplified example code
D-K-P 329b6ba
Merge branch 'main' into docs/webhook-guides
D-K-P 74b5660
Added webhook handler for pages and split the docs
D-K-P bab25f4
Updated links
D-K-P File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
--- | ||
title: "Triggering tasks with webhooks in Next.js" | ||
sidebarTitle: "Next.js webhooks" | ||
description: "Learn how to trigger a task from a webhook in a Next.js app." | ||
--- | ||
|
||
## Prerequisites | ||
|
||
- [A Next.js project, set up with Trigger.dev](/guides/frameworks/nextjs) | ||
- [cURL](https://curl.se/) installed on your local machine. This will be used to send a POST request to your webhook handler. | ||
|
||
## 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: | ||
|
||
```ts /pages/api/webhook-handler.ts | ||
import { helloWorldTask } from "@/trigger/example"; | ||
import { tasks } from "@trigger.dev/sdk/v3"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
// Parse the webhook payload | ||
const payload = req.body; | ||
|
||
// Trigger the helloWorldTask with the webhook data as the payload | ||
await tasks.trigger<typeof helloWorldTask>("hello-world", payload); | ||
|
||
res.status(200).json({ 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: | ||
|
||
```ts /app/api/webhook-handler/route.ts | ||
import type { helloWorldTask } from "@/trigger/example"; | ||
import { tasks } from "@trigger.dev/sdk/v3"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function POST(req: Request) { | ||
// Parse the webhook payload | ||
const payload = await req.json(); | ||
|
||
// Trigger the helloWorldTask with the webhook data as the payload | ||
await tasks.trigger<typeof helloWorldTask>("hello-world", payload); | ||
|
||
return NextResponse.json("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. | ||
|
||
<Steps> | ||
|
||
<Step title="Run your Next.js app and the Trigger.dev dev server"> | ||
|
||
First, run your Next.js app. | ||
|
||
<CodeGroup> | ||
|
||
```bash npm | ||
npm run dev | ||
``` | ||
|
||
```bash pnpm | ||
pnpm run dev | ||
``` | ||
|
||
```bash yarn | ||
yarn dev | ||
``` | ||
|
||
</CodeGroup> | ||
|
||
Then, open up a second terminal window and start the Trigger.dev dev server: | ||
|
||
<CodeGroup> | ||
|
||
```bash npm | ||
npx trigger.dev@latest dev | ||
``` | ||
|
||
```bash pnpm | ||
pnpm dlx trigger.dev@latest dev | ||
``` | ||
|
||
```bash yarn | ||
yarn dlx trigger.dev@latest dev | ||
``` | ||
|
||
</CodeGroup> | ||
|
||
</Step> | ||
|
||
<Step title="Trigger the webhook with some dummy data"> | ||
|
||
To send a POST request to your webhook handler, open up a terminal window on your local machine and run the following command: | ||
|
||
<Tip> | ||
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. | ||
</Tip> | ||
|
||
```bash | ||
curl -X POST -H "Content-Type: application/json" -d '{"Name": "John Doe", "Age": "87"}' http://localhost:3000/api/webhook-handler | ||
``` | ||
|
||
This will send a POST request to your webhook handler, with a JSON payload. | ||
|
||
</Step> | ||
|
||
<Step title="Check the task ran successfully"> | ||
|
||
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](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"}`. | ||
|
||
</Step> | ||
|
||
</Steps> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
title: "Triggering tasks with webhooks in Remix" | ||
sidebarTitle: "Remix webhooks" | ||
description: "Learn how to trigger a task from a webhook in a Remix app." | ||
--- | ||
|
||
## Prerequisites | ||
|
||
- [A Remix project, set up with Trigger.dev](/guides/frameworks/remix) | ||
- [cURL](https://curl.se/) installed on your local machine. This will be used to send a POST request to your webhook handler. | ||
|
||
## Adding the webhook handler | ||
|
||
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`. | ||
|
||
In your new file, add the following code: | ||
|
||
```ts /api/webhook-handler.ts | ||
import type { ActionFunctionArgs } from "@remix-run/node"; | ||
import { tasks } from "@trigger.dev/sdk/v3"; | ||
import { helloWorldTask } from "src/trigger/example"; | ||
|
||
export async function action({ request }: ActionFunctionArgs) { | ||
const payload = await request.json(); | ||
|
||
// Trigger the helloWorldTask with the webhook data as the payload | ||
await tasks.trigger<typeof helloWorldTask>("hello-world", payload); | ||
|
||
return new Response("OK", { status: 200 }); | ||
} | ||
``` | ||
|
||
This code will handle the webhook payload and trigger the 'Hello World' task. | ||
|
||
## Triggering the task locally | ||
|
||
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. | ||
|
||
<Steps> | ||
|
||
<Step title="Run your Remix app and the Trigger.dev dev server"> | ||
|
||
First, run your Remix app. | ||
|
||
<CodeGroup> | ||
|
||
```bash npm | ||
npm run dev | ||
``` | ||
|
||
```bash pnpm | ||
pnpm run dev | ||
``` | ||
|
||
```bash yarn | ||
yarn dev | ||
``` | ||
|
||
</CodeGroup> | ||
|
||
Then, open up a second terminal window and start the Trigger.dev dev server: | ||
|
||
<CodeGroup> | ||
|
||
```bash npm | ||
npx trigger.dev@latest dev | ||
``` | ||
|
||
```bash pnpm | ||
pnpm dlx trigger.dev@latest dev | ||
``` | ||
|
||
```bash yarn | ||
yarn dlx trigger.dev@latest dev | ||
``` | ||
|
||
</CodeGroup> | ||
|
||
</Step> | ||
|
||
<Step title="Trigger the webhook with some dummy data"> | ||
|
||
To send a POST request to your webhook handler, open up a terminal window on your local machine and run the following command: | ||
|
||
<Tip> | ||
If `http://localhost:5173` isn't the URL of your locally running Remix app, replace the URL in the | ||
below command with that URL instead. | ||
</Tip> | ||
|
||
```bash | ||
curl -X POST -H "Content-Type: application/json" -d '{"Name": "John Doe", "Age": "87"}' http://localhost:5173/api/webhook-handler | ||
``` | ||
|
||
This will send a POST request to your webhook handler, with a JSON payload. | ||
|
||
</Step> | ||
|
||
<Step title="Check the task ran successfully"> | ||
|
||
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](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"}`. | ||
|
||
</Step> | ||
|
||
</Steps> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
title: "Using webhooks with Trigger.dev" | ||
sidebarTitle: "Overview" | ||
description: "Guides for using webhooks with Trigger.dev." | ||
--- | ||
|
||
## Overview | ||
|
||
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. | ||
|
||
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. | ||
|
||
## Webhook guides | ||
|
||
<CardGroup cols={2}> | ||
<Card | ||
title="Next.js - triggering tasks using webhooks" | ||
icon="N" | ||
href="/guides/frameworks/nextjs-webhooks" | ||
> | ||
How to create a webhook handler in a Next.js app, and trigger a task from it. | ||
</Card> | ||
<Card | ||
title="Remix - triggering tasks using webhooks" | ||
icon="R" | ||
href="/guides/frameworks/remix-webhooks" | ||
> | ||
How to create a webhook handler in a Remix app, and trigger a task from it. | ||
</Card> | ||
</CardGroup> | ||
|
||
<Note> | ||
If you would like to see a webhook guide for your framework, please request it in our [Discord | ||
server](https://trigger.dev/discord). | ||
</Note> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix grammatical issue in the overview section
The overview provides a clear explanation of webhooks and their handlers. However, there's a grammatical issue in line 9 that needs to be addressed.
Please apply the following change:
This change corrects the subject-verb agreement and adds a hyphen to "event-driven" as per the static analysis suggestion.
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool