# Verify a webhook signature

> Make sure a message really comes from Mizanpage, with the Standard Webhooks libraries or by hand.

Each message is signed with the secret of its address. Verify the signature before processing the message.

## With a library

The signature follows the [Standard Webhooks](https://www.standardwebhooks.com) format. Their libraries (PHP, Node.js, Python, Go…) verify it as is with your `whsec_…` secret.

## By hand

1. Remove `whsec_` from the secret and decode the rest from base64: that is the key.
2. Compute the HMAC-SHA256 of `{webhook-id}.{webhook-timestamp}.{raw body}` with this key, then encode it in base64.
3. Compare it, in constant time, with the value after `v1,` in `webhook-signature`.
4. Refuse a `webhook-timestamp` more than 5 minutes away from your clock.

Use the **raw** body, as received, before any JSON parsing.

### PHP

```php
$key = base64_decode(substr($secret, 6));
$expected = base64_encode(hash_hmac('sha256', "$id.$timestamp.$body", $key, true));
$valid = hash_equals('v1,' . $expected, $signatureHeader)
    && abs(time() - (int) $timestamp) <= 300;
```

### Node.js

```js
import crypto from 'node:crypto'

function verify(secret, id, timestamp, body, signatureHeader) {
  const key = Buffer.from(secret.slice(6), 'base64')
  const expected = crypto.createHmac('sha256', key).update(`${id}.${timestamp}.${body}`).digest('base64')
  const a = Buffer.from(`v1,${expected}`)
  const b = Buffer.from(signatureHeader)
  const recent = Math.abs(Date.now() / 1000 - Number(timestamp)) <= 300
  return recent && a.length === b.length && crypto.timingSafeEqual(a, b)
}
```

## Change the secret

`POST /v1/webhooks/{id}/secret` creates a new secret, in service immediately. Update it on your server right away.

---

Documentation Mizanpage · Developers › Webhooks
https://mizanpage.com/en/docs/webhooks/signature/
