# Shopify HMAC Verification

**URL:** <https://community.shopify.dev/t/shopify-hmac-verification/19654>\
**Category:** Webhooks and Events\
**Tags:** troubleshooting\
**Created:** [July 22, 2025, 6:22am UTC](https://community.shopify.dev/t/shopify-hmac-verification/19654 "2025-07-22T06:22:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rushabh\_Shah](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@Rushabh\_Shah](https://community.shopify.dev/u/Rushabh_Shah)\
**Post date:** [July 22, 2025, 6:22am UTC](https://community.shopify.dev/t/shopify-hmac-verification/19654/1 "2025-07-22T06:22:27Z")

</div>

We are having our webhooks created on the Client’s end, and we are using our servers to handle the webhooks.

We are getting an HMAC token with a webhook, and we want to add verification using HMAC in our webhook server code.

What is the right way to do it?

---

<div class="post-metadata">

**Author:** ![kyle\_liu](https://sea1.discourse-cdn.com/shopifycommunity/user_avatar/community.shopify.dev/kyle_liu/32/14392_2.png) [@kyle\_liu](https://community.shopify.dev/u/kyle_liu)\
**Post date:** [July 22, 2025, 8:28am UTC](https://community.shopify.dev/t/shopify-hmac-verification/19654/2 "2025-07-22T08:28:04Z")

</div>

Hi @Rushabh_Shah

Are you using Node.js? You can try this code

```auto
import crypto from 'crypto';

export function verifyShopifyWebhook(secret) {
  return async (req, res, next) => {
    try {
      const hmacHeader = req.get('X-Shopify-Hmac-Sha256');

      const rawBody = await getRawBody(req);
      const generatedHmac = crypto
        .createHmac('sha256', secret)
        .update(rawBody, 'utf8')
        .digest('base64');

      if (crypto.timingSafeEqual(Buffer.from(generatedHmac), Buffer.from(hmacHeader))) {
        req.rawBody = rawBody;
        next();
      } else {
        return res.status(401).send('HMAC validation failed');
      }
    } catch (error) {
      return res.status(500).send('Server error');
    }
  };
}

// Helper function to get raw body
function getRawBody(req) {
  return new Promise((resolve, reject) => {
    let data = '';
    req.setEncoding('utf8');
    req.on('data', chunk => {
      data += chunk;
    });
    req.on('end', () => resolve(data));
    req.on('error', err => reject(err));
  });
}

```

---

<div class="post-metadata">

**Author:** ![kyle\_liu](https://sea1.discourse-cdn.com/shopifycommunity/user_avatar/community.shopify.dev/kyle_liu/32/14392_2.png) [@kyle\_liu](https://community.shopify.dev/u/kyle_liu)\
**Post date:** [July 22, 2025, 8:38am UTC](https://community.shopify.dev/t/shopify-hmac-verification/19654/3 "2025-07-22T08:38:51Z")

</div>

You can also refer to the Shopify api [source code](https://github.com/Shopify/shopify-app-js/blob/main/packages/apps/shopify-api/lib/utils/hmac-validator.ts)

---

<div class="post-metadata">

**Author:** ![Alan\_G](https://sea1.discourse-cdn.com/shopifycommunity/user_avatar/community.shopify.dev/alan_g/32/5321_2.png) [@Alan\_G](https://community.shopify.dev/u/Alan_G)\
**Post date:** [July 23, 2025, 8:49pm UTC](https://community.shopify.dev/t/shopify-hmac-verification/19654/4 "2025-07-23T20:49:09Z")

</div>

Hey folks 👋 just wanted to share this documentation here in case it’s helpful as well when it comes to calculating the HMAC signature:

> **[Deliver webhooks through HTTPS](https://shopify.dev/docs/apps/build/webhooks/subscribe/https#step-2-validate-the-origin-of-your-webhook-to-ensure-its-coming-from-shopify)**
>
> HTTPS delivery requires far more work to prepare for production. Here are key considerations when working with this delivery method.

We do provide a few steps in the guide above that go through how to verify the HMAC token too, but @kyle_liu is correct, that code there should work for Node. If you’re using our [Remix template](https://shopify.dev/docs/api/shopify-app-remix/latest/guide-webhooks#endpoints), the HMAC authentication should be handled automatically though.

Hope this helps!
