Verify the events that Stripe sends to your webhook endpoints. Additional details in the Stripe docs.
Available examples:
You’ll need the following:
- Node.js >=14.0.0
- Stripe account to accept payments (sign up for free).
- Stripe CLI or ngrok to tunnel requests to your local server.
In the sample directory (cd examples/webhook-signing/express).
Install dependencies:
npm install
Update .env with your own Stripe API keys.
Next, follow these installation steps to install the Stripe CLI which we'll use for webhook forwarding.
After the installation has finished, authenticate the CLI with your Stripe account:
stripe login
To start the webhook forwarding run:
stripe listen --forward-to localhost:3000/webhook
The Stripe CLI will let you know that webhook forwarding is ready and output your webhook signing secret:
> Ready! Your webhook signing secret is whsec_xxx
Copy the webhook signing secret (whsec_xxx) to your .env file.
In a separate terminal window, start the local sample server:
`./main.ts`
In another separate terminal window, trigger an event, for example:
stripe trigger payment_intent.succeeded
You should now see some webhook event details being logged to your Node.js console.
To add a new example:
- Create a new subfolder using the server library name
- Copy
package.jsonfrom one of the existing samples. Remove extra dependencies. - Create a
main.tsfile with sample server code and runchmod +x main.tsso it's directly executable. - The
main.tshas to follow some rules: - Written in typescript using
import .. from ..import syntax. - Must have
#!/usr/bin/env -S npm run-script runheader to be directly runable. - Must print
Webhook endpoint available at <url>when the server is started. - Must return
{"received":true}and 200 status code forcharge.succeededevent. - Test your example
cd examples/webhook-signing/test./main.ts ../<your test directory>- Add a test to
/tests/Integration.spec.ts