# Verify a webhook signature

Verify Hecaton’s HMAC-SHA256 signature on the raw request body before processing an outbound webhook.

## Set up a destination

In **Settings → Webhooks → Sent**, add a public HTTPS URL and select the events to receive. Copy the full signing secret that starts with `whsec_` when Hecaton shows it. Store it as a secret on the receiving server. Change the secret in Hecaton if you lose it. Keep the receiver's keys synchronized during a rotation. For 24 hours after rotation Hecaton can send signatures for both the current and previous secrets.

Hecaton sends a JSON POST and sets these headers.

| Header              | Value                                                                                                                        |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `Hecaton-Signature` | `t=<Unix seconds>,v1=<hex HMAC-SHA256>`. There may be several `v1` entries during rotation.                                  |
| `Hecaton-Delivery`  | Delivery ID, stable across automatic attempts.                                                                               |
| `Hecaton-Event`     | Subscribed event name, for example `workspace.asleep` when the body's type is `workspace.state`. A test uses `webhook.test`. |

## Verify the exact bytes

Read the body as a byte buffer before any JSON parser. Use the complete `whsec_` string as the UTF-8 HMAC key. Hash the timestamp text, a literal ASCII dot and the exact body bytes. Compare the digest against each `v1` with a constant-time comparison. The Node.js example accepts a timestamp within five minutes of the receiver's clock.

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

export function verifyHecatonSignature(rawBody, header, secret, now = Math.floor(Date.now() / 1000)) {
  if (!Buffer.isBuffer(rawBody) || typeof header !== 'string' || !secret) return false;

  const parts = header.split(',').map((part) => part.trim());
  const timestamps = parts.filter((part) => part.startsWith('t='));
  if (timestamps.length !== 1) return false;

  const timestamp = timestamps[0].slice(2);
  if (!/^\d{1,15}$/.test(timestamp) || Math.abs(now - Number(timestamp)) > 300) return false;

  const expected = createHmac('sha256', secret)
    .update(timestamp + '.')
    .update(rawBody)
    .digest();
  return parts.some((part) => {
    if (!/^v1=[a-f0-9]{64}$/i.test(part)) return false;
    return timingSafeEqual(expected, Buffer.from(part.slice(3), 'hex'));
  });
}
```

Pass the raw body buffer, the value of `Hecaton-Signature`, and the signing secret to `verifyHecatonSignature`. Reject a failed verification. After it passes, parse the JSON and use `Hecaton-Delivery` as a durable deduplication key before applying side effects. An explicit resend has a new delivery ID and keeps the original event body.

A delivery succeeds when Hecaton receives 2xx headers within 10 seconds. There are up to eight total attempts. Redirects fail. In **Sent**, send a test to check your receiver and inspect the delivery status. The [REST API reference](api.md) lists the webhook operations.

## Test vector

Synthetic secret `whsec_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`. Set the verifier’s `now` to `1796083200` for this vector. The body has no terminating newline.

```text
{"type":"webhook.test","id":"example","createdAt":"2026-12-01T00:00:00.000Z","data":{}}
```

```text
Hecaton-Signature: t=1796083200,v1=32aa5bee226aa678a38a37ad42a79f3210f7ca6192d9ac6d8d5a512a6c86e92b
```
