How can I verify webhooks with Node.js?

How can I verify webhooks with Node.js?

Harelk1015
Shopify Partner
46 1 6

Hey :),

im trying to veryfiy webhooks in my nodejs app. but it just doesnt work, here is my code : 

app.use(shopify.config.webhooks.path, async (req, res, next) => {
  if (!req.body) {
    return res.status(401).send("Couldn't verify incomming Webhook request!");
  }
  const hmac = req.headers["x-shopify-hmac-sha256"];

  const genHash = crypto
    .createHmac("sha256", process.env.SHOPIFY_API_SECRET)
    .update(JSON.stringify(req.body), "utf8", "hex")
    .digest("base64");

  console.log(hmac);
  console.log(genHash);

  if (genHash !== hmac) {
    return res.status(401).send("Couldn't verify incomming Webhook request!");
  }

  next();
});


Please help meee

Replies 2 (2)

oli_dev
Visitor
2 0 1

Did you sorted it ?
The update() method should be used without the encoding ("utf8") and the output encoding ("hex").
Try that:

 

const genHash = crypto .createHmac("sha256", process.env.SHOPIFY_API_SECRET) .update(JSON.stringify(req.body)) .digest("base64");

 

 

This is incorrect because the update() method only takes one argument, which is the data you want to hash. The encoding is specified when calling the digest() method, as you did correctly with .digest("base64").