---
title: Verifying webhook signatures
description: Every webhook we send is signed with your secret. Verify it before you trust the payload.
progress: complete
---

Every delivery carries an `X-Signature` header. It is the hex HMAC-SHA256 of the
exact request body, keyed with your webhook secret. Recompute it and compare.
Endpoints on Webhooks v3 also get `X-Signatures`, the same digest once for every
signing key the endpoint holds, which is what lets you change keys without
rejecting a single delivery.

> **Hash the bytes you received**
>
> Hash the raw body, not a re-serialised copy of it. `JSON.stringify(req.body)`
> will not reproduce our bytes, because key order and number formatting do not
> survive the round trip, and the signature will not match. Read the body as a
> string or a buffer before any JSON middleware touches it.

## The header

- **X-Signature** — Hex HMAC-SHA256 over the raw request body, keyed with your newest signing key. No prefix, no timestamp, no scheme label. Just the hex digest.
- **X-Signatures** — Webhooks v3 only. The same digest for every active signing key, newest first, separated by commas. With one key it equals X-Signature.

The payload version is set per endpoint and shown on its page in [the console](https://partner.btcdirect.eu/console).
Webhooks v2 sends `X-Signature` alone, exactly as it always has.

That is the whole scheme, so there is nothing else to parse. A valid signature
proves the payload came from us and was not altered on the way. It does not
prove the request is recent, because nothing signed carries a time. See
[what it does not prove](#what-a-signature-does-not-prove).

## Verify a signature

_verify_

```js
import { createHmac, timingSafeEqual } from 'node:crypto';

// rawBody is the exact body bytes, NOT JSON.stringify(req.body).
export function verify(rawBody, header, secret) {
  const expected = createHmac('sha256', secret).update(rawBody).digest('hex');

  // Constant-time: a length-dependent or short-circuiting comparison leaks how
  // much of the signature was correct.
  const a = Buffer.from(expected);
  const b = Buffer.from(header);
  return a.length === b.length && timingSafeEqual(a, b);
}
```

```php
<?php
// $rawBody is file_get_contents('php://input'), before any json_decode.
function verify(string $rawBody, string $header, string $secret): bool
{
    $expected = hash_hmac('sha256', $rawBody, $secret);

    // hash_equals is the constant-time comparison; === is not.
    return hash_equals($expected, $header);
}
```

```python
import hmac
from hashlib import sha256

# raw_body is request.get_data(), before any .json access.
def verify(raw_body: bytes, header: str, secret: str) -> bool:
    expected = hmac.new(secret.encode(), raw_body, sha256).hexdigest()

    # compare_digest is the constant-time comparison; == is not.
    return hmac.compare_digest(expected, header)
```

```java
// rawBody is the exact bytes read from the request, before any JSON parsing.
public static boolean verify(byte[] rawBody, String header, String secret)
        throws GeneralSecurityException {
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(new SecretKeySpec(secret.getBytes(UTF_8), "HmacSHA256"));
    String expected = HexFormat.of().formatHex(mac.doFinal(rawBody));

    // MessageDigest.isEqual is constant-time; String.equals is not.
    return MessageDigest.isEqual(expected.getBytes(UTF_8), header.getBytes(UTF_8));
}
```

```go
// body is the exact bytes read from the request, before any json.Unmarshal.
func Verify(body []byte, header, secret string) bool {
	mac := hmac.New(sha256.New, []byte(secret))
	mac.Write(body)
	expected := hex.EncodeToString(mac.Sum(nil))

	// hmac.Equal is constant-time; == on strings is not.
	return hmac.Equal([]byte(expected), []byte(header))
}
```

## Rotating your secret

Add and expire your signing keys yourself, in [the console](https://partner.btcdirect.eu/console). An endpoint can
hold more than one at a time, and each can carry an expiry date. A new key's
secret is shown once, when you add it.

### On Webhooks v3

Every delivery is signed with every active key, in `X-Signatures`. Accept a
delivery when any of them matches a secret you hold, and a rotation never
rejects a valid delivery:

1. Add a new key in the console and store its secret.
2. Deploy your handler with the new secret alongside the old one.
3. Expire or revoke the old key. Its signature drops out of `X-Signatures` and
   the new key is the only one left.

```js
import { createHmac, timingSafeEqual } from 'node:crypto';

// secrets: every signing secret you currently hold, old and new.
export function verifyAny(rawBody, signaturesHeader, secrets) {
  const received = signaturesHeader.split(',').map((s) => Buffer.from(s.trim()));
  return secrets.some((secret) => {
    const expected = Buffer.from(
      createHmac('sha256', secret).update(rawBody).digest('hex'),
    );
    return received.some(
      (sig) => sig.length === expected.length && timingSafeEqual(sig, expected),
    );
  });
}
```

> **Once the old key is gone, so is its signature**
>
> Expiring a key removes it from `X-Signatures` straight away. Keep the old secret
> in your handler until then and remove it afterwards; holding it a little longer
> does no harm.

### On Webhooks v2

Webhooks v2 sends only `X-Signature`, signed with your **newest** key, so what
you receive changes the moment you add one. Since the new secret only exists
once the key is added, a v2 endpoint cannot rotate without a short window in
which deliveries carry a signature your handler does not know yet.

> **Keep the window short, or move to v3**
>
> Add the key, deploy its secret straight away, then expire the old key.
> Deliveries that fail in between are retried on the [published
> schedule](https://partner.btcdirect.eu/v3/webhooks/delivery.md), so a quick deploy loses nothing. To rotate with no
> window at all, switch the endpoint to Webhooks v3 on its page in the console.

## What a signature does not prove

It proves the payload came from us and that nothing changed on the way. It does
not prove the request is recent, so a captured delivery replayed later still
verifies.

The `timestamp` in the envelope does not close that gap. It records when the
webhook was built, so every retry of one delivery carries the same value. It
tells you when the event happened, not when this request was sent.

> **Make your handling idempotent instead**
>
> Key on what the payload identifies and treat a repeat as doing nothing. You need
> that anyway: a lost `2xx` makes us retry something you already processed, so
> duplicates are normal rather than an attack. See
> [delivery and retries](https://partner.btcdirect.eu/v3/webhooks/delivery.md).

## Common mistakes

- Re-serialising the parsed body instead of hashing the raw bytes. Easily the
  most common cause of a signature that never matches.
- Comparing with `==`, `===` or `String.equals`. Use the constant-time
  comparison your language provides. Every example above names it.
- Expecting a prefix or a timestamp in the header. The value is bare hex.
- On Webhooks v3, checking only `X-Signature` during a rotation. It carries the
  newest key alone; `X-Signatures` is the header that covers both.
