# Webhook signatures

> Verify that each webhook delivery came from Workwae, and rotate an endpoint's signing secret without downtime.

URL: https://docs.workwae.com/guides/webhook-signatures

Every webhook delivery is signed with its endpoint's secret. Verify the signature before trusting the body, and rotate the secret with a grace period when you need a new one.

## Verify the signature

Signatures follow [Standard Webhooks](https://www.standardwebhooks.com/), so any Standard Webhooks library verifies them:

- The algorithm is HMAC-SHA256, keyed with the base64-decoded part of the secret after `whsec_`.
- The signed content is `{webhook-id}.{webhook-timestamp}.{body}`.
- `webhook-signature` may carry several space-separated `v1,<base64>` signatures; one match is enough.

> **Warning: Verify the raw body**
>
> Verify the request body exactly as received, before parsing it. Re-serialized JSON does not match the signature.

`parseWebhook` from the [TypeScript SDK](/guides/sdk) checks the signature and the timestamp (5 minutes of tolerance), then parses the body. `verifyWebhook` only returns a boolean.

```ts tab="parseWebhook"
import { parseWebhook, type WebhookEvent } from "@workwae/sdk";

export async function POST(request: Request): Promise<Response> {
  const body = await request.text();
  let event: WebhookEvent;
  try {
    event = await parseWebhook(process.env.WORKWAE_WEBHOOK_SECRET!, request.headers, body);
  } catch {
    return new Response("Invalid webhook", { status: 400 });
  }
  if (await alreadyHandled(event.id)) return new Response(null, { status: 204 });
  if (event.known && event.type === "message.created") {
    await enqueue(event.id, event.data.message.subject);
  }
  return new Response(null, { status: 204 });
}
```

```ts tab="verifyWebhook"
import { verifyWebhook } from "@workwae/sdk";

const valid = await verifyWebhook(secret, request.headers, body);
if (!valid) return new Response("Invalid signature", { status: 400 });
const event = JSON.parse(body);
```

```ts tab="By hand (Node)"
import { createHmac, timingSafeEqual } from "node:crypto";

const TOLERANCE_SECONDS = 300;

export function verify(secret: string, headers: Headers, body: string): boolean {
  const id = headers.get("webhook-id");
  const timestamp = headers.get("webhook-timestamp");
  const signatures = headers.get("webhook-signature");
  if (!id || !timestamp || !signatures || !/^[0-9]+$/.test(timestamp)) return false;
  // Rejects replays of an old, captured request.
  if (Math.abs(Date.now() / 1000 - Number(timestamp)) > TOLERANCE_SECONDS) return false;

  const key = Buffer.from(secret.replace(/^whsec_/, ""), "base64");
  const expected = createHmac("sha256", key).update(`${id}.${timestamp}.${body}`).digest();
  return signatures.split(" ").some((candidate) => {
    const [version, value] = candidate.split(",", 2);
    if (version !== "v1" || !value) return false;
    const given = Buffer.from(value, "base64");
    return given.length === expected.length && timingSafeEqual(given, expected);
  });
}
```

What `parseWebhook` returns:

- `event.createdAt` is a `Date`.
- For a known type, checking `event.type` narrows `event.data` to that type's data.
- A type added after your SDK build arrives with `known: false` and unchecked `data`.

## Rotate the secret

`POST /v1/webhooks/{endpointId}/rotate-secret` (or Rotate secret in the console) returns a new secret, which signs every delivery from then on.

```bash tab="cURL"
curl -X POST "https://api.workwae.com/v1/webhooks/$ENDPOINT_ID/rotate-secret" \
  -H "Authorization: Bearer $WORKWAE_API_KEY" \
  -H "content-type: application/json" \
  -d '{ "graceSeconds": 86400 }'
```

```ts tab="TypeScript"
const { secret, previousSecretExpiresAt } = await workwae.webhooks.rotateSecret({
  endpointId,
  graceSeconds: 86_400,
});
```

During the grace period the old secret keeps signing too:

| `graceSeconds` | Effect |
| --- | --- |
| Omitted | 24 hours of grace. |
| Up to 604800 (7 days) | Deliveries carry two signatures, `v1,<new> v1,<old>`; a receiver holding either secret accepts them. |
| `0` | The old secret stops signing at once. |

- `previousSecretExpiresAt`, in the response and on the endpoint, says when the old secret stops signing. Switch your receiver to the new secret before then.
- `verifyWebhook`, `parseWebhook` and every Standard Webhooks library accept a delivery when any of its signatures matches the secret they are given.
- A rotation during another rotation's grace ends that grace: only the secret it replaces keeps signing.
