Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
Hi everybody, when I tried to migrate to new Node template, the webhooks always throws this error: “Error: Could not validate request HMAC”.
Does anyone know how to fix this? I tried searching on Google but nothing works. This is my code snippet:
app.post( shopify.config.webhooks.path, express.text({ type: "*/*" }), // Even with this, it still throws the error No body was received when processing webhook function (req: any, res: any, next: any): any { // This one fix the error above req.body = JSON.stringify(req.body); next(); }, shopify.processWebhooks({ webhookHandlers: GDPRWebhookHandlers as any }) );
Hi Nam7911,
There's a couple things you could do here to troubleshoot this HMAC validation error:
Verify the webhook path: First off you should double-check that the shopify.config.webhooks.path value matches the webhook path you set in your Shopify Partner Dashboard or Admin. The webhook path should be the same in both places.
Verify the body parsing middleware: In your code snippet, you're using express.text({ type: "*/*" }) as the middleware to parse the request body. This might cause issues with the HMAC validation. Instead, you can use express.json() middleware to parse the request body as JSON.
You could also verify the webhook secret and ensure that you have correctly set the webhook secret in your Shopify Partner Dashboard or Admin.
Hope these options help - if not let us know here and we'll dig into this a bit more.
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Thanks for replying Liam,
I have double-checked the webhook path, they are identical, which is why I think they ran in the first place, which gave me the error.
For the "express.text({ type: "*/*" })" middleware, I followed the docs here https://github.com/Shopify/shopify-api-js/blob/main/docs/guides/webhooks.md#note-regarding-use-of-bo...
Basically how I understand is the new webhook will only take the rawBody, which is a string. Hence the use of the middleware (Although it doesn't work).
Can you point out to me where to find the set up for the webhook secret in Partner Dashboard? I tried looking everywhere and couldn't find anything.
Any update here? I'm getting the same issue.
I had fixed this by following this step -
I followed these steps and it's worked for me.