#mhiggie
1 messages ยท Page 1 of 1 (latest)
Hi ๐
A webhook registered on the Stripe dashboard will always need to point to a public URL. You can stream webhook events to your localhost server using the Stripe CLI and the command stripe listen
https://stripe.com/docs/cli/listen
yes, so i am using a node express backend
i have my server running locally
i have opened two additional terminals within vscode
in the first additional terminal i ran stripe listen --forward-to http://localhost:5000
does the forward -to have to point to my server or the literal /webhook endpoint?
The literal endpoint
I run stripe listen --forward-to http://localhost:8000/webhook2/ for my test integration
okay in my second additional terminal window i am running stripe trigger subscription_schedule.created
`router.post('/webhook', express.raw({type: 'application/json'}), async (request, response) => {
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
console.log(event)
} catch (err) {
response.status(400).send(Webhook Error: ${err.message});
return;
}
// Handle the event
if (event.type === 'subscription_schedule.created') {
const subscriptionScheduleCreated = event.data.object;
// Then define and call a function to handle the event subscription_schedule.created
console.log('stripe subscription created hurray.')
console.log(subscriptionScheduleCreated)
} else {
console.log(Unhandled event type ${event.type});
}
res.sendStatus(200)
})`
And the error from try catch block is firing
Okay, do you have more information on what is triggering the 400 error?
Are you passing in the correct webhook secret?
i think that is the issue
I would log the sig and endpointSecret values before you try to construct the event
Well for localhost you copy the endpoint secret value that is written to the console when you run stripe listen and make it a const value in your webhook handler
hah okay working great thank you
Great, happy to hear it ๐
first timer with webhooks here
They do take some getting used to
for that endpoint secret would i reference my production stripe key
Our general docs on them cover a fair amount and may make more sense now that you can test them out locally: https://stripe.com/docs/webhooks
wait no, id probably need to create a new webhook env variable in production and reference that
Not either
Each webhook listener you register in the Stripe dashboard (or API) will generate it's own secret value
You will need to make that available in your hosting environment
that makes sense thank you
then in each webhook point to that corresponding env variable from hosting
For node we see a lot of process.env.SECRET kind of stuff used to load the value from host configurations
yup yup thanks again this was super helpful