|
| 1 | +--- |
| 2 | +title: "Trigger a task from Stripe webhook events" |
| 3 | +sidebarTitle: "Stripe webhook" |
| 4 | +description: "This example demonstrates how to handle Stripe webhook events using Trigger.dev." |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +This example shows how to set up a webhook handler for incoming Stripe events. The handler triggers a task when a `checkout.session.completed` event is received. This is easily customisable to handle other Stripe events. |
| 10 | + |
| 11 | +## Key features |
| 12 | + |
| 13 | +- Shows how to create a Stripe webhook handler |
| 14 | +- Triggers a task from your backend when a `checkout.session.completed` event is received |
| 15 | + |
| 16 | +## Environment variables |
| 17 | + |
| 18 | +You'll need to configure the following environment variables for this example to work: |
| 19 | + |
| 20 | +- `STRIPE_WEBHOOK_SECRET` The secret key used to verify the Stripe webhook signature. |
| 21 | +- `TRIGGER_API_URL` Your Trigger.dev API url: `https://api.trigger.dev` |
| 22 | +- `TRIGGER_SECRET_KEY` Your Trigger.dev secret key |
| 23 | + |
| 24 | +## Setting up the Stripe webhook handler |
| 25 | + |
| 26 | +First you'll need to create a [Stripe webhook](https://stripe.com/docs/webhooks) handler route that listens for POST requests and verifies the Stripe signature. |
| 27 | + |
| 28 | +Here are examples of how you can set up a handler using different frameworks: |
| 29 | + |
| 30 | +<CodeGroup> |
| 31 | + |
| 32 | +```ts Next.js |
| 33 | +// app/api/stripe-webhook/route.ts |
| 34 | +import { NextResponse } from "next/server"; |
| 35 | +import { tasks } from "@trigger.dev/sdk/v3"; |
| 36 | +import Stripe from "stripe"; |
| 37 | +import type { stripeCheckoutCompleted } from "@/trigger/stripe-checkout-completed"; |
| 38 | +// 👆 **type-only** import |
| 39 | + |
| 40 | +export async function POST(request: Request) { |
| 41 | + const signature = request.headers.get("stripe-signature"); |
| 42 | + const payload = await request.text(); |
| 43 | + |
| 44 | + if (!signature || !payload) { |
| 45 | + return NextResponse.json( |
| 46 | + { error: "Invalid Stripe payload/signature" }, |
| 47 | + { |
| 48 | + status: 400, |
| 49 | + } |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + const event = Stripe.webhooks.constructEvent( |
| 54 | + payload, |
| 55 | + signature, |
| 56 | + process.env.STRIPE_WEBHOOK_SECRET as string |
| 57 | + ); |
| 58 | + |
| 59 | + // Perform the check based on the event type |
| 60 | + switch (event.type) { |
| 61 | + case "checkout.session.completed": { |
| 62 | + // Trigger the task only if the event type is "checkout.session.completed" |
| 63 | + const { id } = await tasks.trigger<typeof stripeCheckoutCompleted>( |
| 64 | + "stripe-checkout-completed", |
| 65 | + event.data.object |
| 66 | + ); |
| 67 | + return NextResponse.json({ runId: id }); |
| 68 | + } |
| 69 | + default: { |
| 70 | + // Return a response indicating that the event is not handled |
| 71 | + return NextResponse.json( |
| 72 | + { message: "Event not handled" }, |
| 73 | + { |
| 74 | + status: 200, |
| 75 | + } |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +```ts Remix |
| 83 | +// app/webhooks.stripe.ts |
| 84 | +import { type ActionFunctionArgs, json } from "@remix-run/node"; |
| 85 | +import type { stripeCheckoutCompleted } from "src/trigger/stripe-webhook"; |
| 86 | +// 👆 **type-only** import |
| 87 | +import { tasks } from "@trigger.dev/sdk/v3"; |
| 88 | +import Stripe from "stripe"; |
| 89 | + |
| 90 | +export async function action({ request }: ActionFunctionArgs) { |
| 91 | + // Validate the Stripe webhook payload |
| 92 | + const signature = request.headers.get("stripe-signature"); |
| 93 | + const payload = await request.text(); |
| 94 | + |
| 95 | + if (!signature || !payload) { |
| 96 | + return json({ error: "Invalid Stripe payload/signature" }, { status: 400 }); |
| 97 | + } |
| 98 | + |
| 99 | + const event = Stripe.webhooks.constructEvent( |
| 100 | + payload, |
| 101 | + signature, |
| 102 | + process.env.STRIPE_WEBHOOK_SECRET as string |
| 103 | + ); |
| 104 | + |
| 105 | + // Perform the check based on the event type |
| 106 | + switch (event.type) { |
| 107 | + case "checkout.session.completed": { |
| 108 | + // Trigger the task only if the event type is "checkout.session.completed" |
| 109 | + const { id } = await tasks.trigger<typeof stripeCheckoutCompleted>( |
| 110 | + "stripe-checkout-completed", |
| 111 | + event.data.object |
| 112 | + ); |
| 113 | + return json({ runId: id }); |
| 114 | + } |
| 115 | + default: { |
| 116 | + // Return a response indicating that the event is not handled |
| 117 | + return json({ message: "Event not handled" }, { status: 200 }); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +</CodeGroup> |
| 124 | + |
| 125 | +## Task code |
| 126 | + |
| 127 | +This task is triggered when a `checkout.session.completed` event is received from Stripe. |
| 128 | + |
| 129 | +```ts trigger/stripe-checkout-completed.ts |
| 130 | +import { task } from "@trigger.dev/sdk/v3"; |
| 131 | +import type stripe from "stripe"; |
| 132 | + |
| 133 | +export const stripeCheckoutCompleted = task({ |
| 134 | + id: "stripe-checkout-completed", |
| 135 | + run: async (payload: stripe.Checkout.Session) => { |
| 136 | + // Add your custom logic for handling the checkout.session.completed event here |
| 137 | + }, |
| 138 | +}); |
| 139 | +``` |
| 140 | + |
| 141 | +## Testing your task locally |
| 142 | + |
| 143 | +To test everything is working you can use the Stripe CLI to send test events to your endpoint: |
| 144 | + |
| 145 | +1. Install the [Stripe CLI](https://stripe.com/docs/stripe-cli#install), and login |
| 146 | +2. Follow the instructions to [test your handler](https://docs.stripe.com/webhooks#test-webhook). This will include a temporary `STRIPE_WEBHOOK_SECRET` that you can use for testing. |
| 147 | +3. When triggering the event, use the `checkout.session.completed` event type. With the Stripe CLI: `stripe trigger checkout.session.completed` |
| 148 | +4. If your endpoint is set up correctly, you should see the Stripe events logged in your console with a status of `200`. |
| 149 | +5. Then, check the [Trigger.dev](https://cloud.trigger.dev) dashboard and you should see the successful run of the `stripe-webhook` task. |
| 150 | + |
| 151 | +For more information on setting up and testing Stripe webhooks, refer to the [Stripe Webhook Documentation](https://stripe.com/docs/webhooks). |
0 commit comments