# How to verify Shopify webhooks (X-Shopify-Hmac-Sha256) in Node.js

> Verify X-Shopify-Hmac-Sha256 with your app's client secret and the raw body, skip duplicates with X-Shopify-Webhook-Id, and answer within five seconds.

- Published 2026-11-29, updated 2026-11-29
- URL: https://softreuse.com/guides/verify-shopify-webhook-hmac-nodejs

**Short answer:** compute HMAC-SHA256 of the **raw** request body with your app's **client secret**, base64-encode it, and compare it in constant time with `X-Shopify-Hmac-Sha256`. Then skip duplicates by `X-Shopify-Webhook-Id` and answer within five seconds.

## What Shopify sends

| Header | Meaning |
|---|---|
| `X-Shopify-Hmac-Sha256` | base64 HMAC-SHA256 of the raw body, keyed with the app's client secret |
| `X-Shopify-Webhook-Id` | Unique per delivery; the key for skipping duplicates |
| `X-Shopify-Event-Id` | Shared by the deliveries of one event to several subscriptions |
| `X-Shopify-Topic` | The topic, e.g. `orders/create` |
| `X-Shopify-Shop-Domain` | The shop the event belongs to |

## Verifying in a Next.js route handler

```ts
// app/api/webhooks/shopify/route.ts
import { createHmac, timingSafeEqual } from "node:crypto";

export const runtime = "nodejs";

export async function POST(request: Request) {
  const raw = Buffer.from(await request.arrayBuffer()); // exact bytes
  const received = Buffer.from(request.headers.get("x-shopify-hmac-sha256") ?? "", "base64");
  const expected = createHmac("sha256", process.env.SHOPIFY_CLIENT_SECRET!).update(raw).digest();
  if (received.length !== expected.length || !timingSafeEqual(received, expected)) {
    return new Response("invalid hmac", { status: 401 });
  }
  const webhookId = request.headers.get("x-shopify-webhook-id")!;
  const topic = request.headers.get("x-shopify-topic")!;
  const payload = JSON.parse(raw.toString("utf8"));
  // store (webhookId, topic, payload) with a unique key on webhookId, answer, then process
  return new Response(null, { status: 200 });
}
```

Compare the **decoded bytes**, not the base64 strings, and check the lengths first: `timingSafeEqual` throws on buffers of different length.

## Why verification fails

- **A body parser ran first.** `express.json()` or a framework's JSON parsing hands you an object; hashing `JSON.stringify(body)` gives a different result. Use `express.raw({ type: "application/json" })` on the webhook route.
- **The wrong credential.** It is the client secret, not the API key or an access token.
- **A rotated secret.** After rotating the client secret, Shopify can take up to an hour to sign with the new one. Accept both while rotating.

The [webhook signature debugger](https://softreuse.com/tools/webhook-signature-debugger) checks a Shopify delivery in your browser and tells you which of these it is.

## Timeouts, retries and duplicates

Shopify allows **one second to connect and five seconds** for the whole request. Any response outside `2xx` (redirects included) is a failure, and Shopify **retries 8 times over the next 4 hours**. After 8 consecutive failures, a subscription created through the Admin API is **deleted**.

So verify, store the event, answer `200`, and process it afterwards. Retries and network timeouts mean the same delivery can arrive twice: store `X-Shopify-Webhook-Id` with a unique constraint and skip what you've seen, or make your processing idempotent.

## With Reliable Webhooks

[Reliable Webhooks](https://softreuse.com/products/reliable-webhooks) verifies Shopify through its generic HMAC provider, stores each event before answering, skips duplicates and retries your handler:

```ts
import { Pool } from "pg";
import { createWebhookReceiver, hmacProvider, PostgresStore } from "@softreuse/webhook-kit";

const shopify = createWebhookReceiver({
  provider: hmacProvider({
    name: "shopify",
    secret: [process.env.SHOPIFY_CLIENT_SECRET!], // add the old secret here while rotating
    signatureHeader: "x-shopify-hmac-sha256",
    encoding: "base64",
    identify: ({ headers }) => ({ id: headers.get("x-shopify-webhook-id")!, type: headers.get("x-shopify-topic")! }),
  }),
  store: new PostgresStore(new Pool({ connectionString: process.env.DATABASE_URL })),
  handlers: {
    "orders/create": async ({ event, idempotencyKey }) => { /* fulfil once per idempotencyKey */ },
    "*": () => {},
  },
});

// app/api/webhooks/shopify/route.ts
export const POST = (request: Request) => shopify.handle(request);
```

## Sources

- [Shopify: Verify webhook deliveries](https://shopify.dev/docs/apps/build/webhooks/verify-deliveries)
- [Shopify: Webhook delivery structure and headers](https://shopify.dev/docs/apps/build/webhooks/delivery-structure)

## Frequently asked questions

### Which secret does Shopify use to sign webhooks?

Your app's client secret (the API secret key), not the API key or an access token. Shopify computes HMAC-SHA256 of the raw request body with it and sends the result base64-encoded in X-Shopify-Hmac-Sha256.

### Why does my Shopify HMAC verification fail?

Most often a body parser such as express.json() ran first and you hashed a re-serialised body, or you used the API key instead of the client secret. After rotating the client secret, Shopify can keep signing with the old one for up to an hour, so accept both during that window.

### How do I handle duplicate Shopify webhooks?

Store X-Shopify-Webhook-Id with a unique constraint and skip deliveries you have seen. If you have several subscriptions for the same topic, each delivery has its own X-Shopify-Webhook-Id but they share X-Shopify-Event-Id.

### What happens if my endpoint is slow or down?

Shopify allows one second to connect and five seconds for the whole request. Anything outside 2xx, including redirects, counts as a failure, and Shopify retries 8 times over the next 4 hours. After 8 consecutive failures, a subscription created through the Admin API is deleted.
