I am getting this error when trying to use express.raw() as a middleware with stripe. I am following the stripe documentation and did exactly as they said.
const express = require('express')
//other stuff
router.post(
"/webhook",
express.raw({ type: "application/json" }),
function (request, response) {
const sig = request.headers["stripe-signature"];
console.log(request.body);
let event;
try {
event = stripe.webhooks.constructEvent(
request.body,
sig,
process.env.WEBHOOK_SECRET
);
} catch (err) {
console.log(err);
return response.status(400).send(`Webhook Error: ${err.message}`);
}
response.status(200).end();
}
);
Specifications
Any help in the positive direction is appreciated.
The express.raw()
function was added in Express v4.17.0 per this doc. Based on the error you get it's likely that you are using an older version of Express and would need to upgrade. If you can't upgrade you might have to rely on body-parser instead.
Note that verifying signature with Stripe's webhooks can be quite complex as the right configuration depends on your exact version of Express and how you configured the root. You can also look at this Github issue on stripe-node where a lot of developers have shared what worked for their own configuration specifically.