Stripe webhook on localhost
Stripe uses webhooks to report payments, subscriptions and refunds. For local development, Stripe has its own CLI: stripe listen --forward-to localhost:4242/webhook. tuna comes in handy when you need a real endpoint in the Dashboard: to check the event setup, show the integration to a colleague, or see requests in the inspector with replay.
Step 1. Install the clientโ
Instructions are available on the installation page.
Step 2. Run the appโ
Stripe signs every request with the Stripe-Signature header. The signature is computed over the raw request body, so the handler has to get the raw body rather than already parsed JSON. An example with Node.js and Express:
const express = require('express');
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const app = express();
app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
let event;
try {
event = stripe.webhooks.constructEvent(
req.body,
req.headers['stripe-signature'],
process.env.STRIPE_WEBHOOK_SECRET,
);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
console.log(event.type);
res.sendStatus(200);
});
app.listen(4242);
tuna passes the body unchanged, so the signature matches. The --verify-webhook flag doesn't support Stripe โ your app verifies the signature.
Step 3. Start tunaโ
tuna http 4242 --subdomain=<your-subdomain>
The free permanent subdomain keeps the address the same across restarts, so you don't have to edit the endpoint in Stripe.
Step 4. Add an endpointโ
In the Dashboard, in test mode, open Webhooks and add an endpoint:
- address:
https://<your-subdomain>.a.tuna.free/webhook; - events: for example,
payment_intent.succeededandcheckout.session.completed.
After saving, copy the whsec_โฆ signing secret into the STRIPE_WEBHOOK_SECRET variable and restart the app.
Step 5. Checkโ
Create a test event with the Stripe CLI or make a payment with a test cardOpens in a new tab:
stripe trigger payment_intent.succeeded
The request shows up in the inspector at http://127.0.0.1:4040 along with the app's response. Stripe shows delivery attempts and responses on the endpoint's page in the Dashboard.
Replay from the inspector sends the same body and the same signature. The Stripe signature includes the time it was sent, and constructEvent rejects events older than 5 minutes by default. So you can replay a request from the inspector within 5 minutes; after that, create a new event.
If it doesn't arrive, follow the steps in Webhook doesn't reach the app.