> ## Documentation Index
> Fetch the complete documentation index at: https://docs.technicallyfit.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Webhook verification

> Check that a webhook delivery came from TechnicallyFit, with the SDKs.

Every [webhook](https://docs.technicallyfit.com/guides/webhooks.md) delivery is signed. The SDKs check the signature the
way the API makes it: an HMAC-SHA256 of `<timestamp>.<raw body>` with your endpoint's
secret, compared in constant time, with a timestamp no further than five minutes from
your clock. While you rotate a secret, pass both: a delivery passes with either.

Verify the body exactly as it arrived. Parsing the JSON and writing it out again changes
the bytes, and the signature no longer matches.

## TypeScript

`@technicallyfit/sdk/webhooks` uses only Web Crypto, so it runs in Node, edge runtimes and
workers. This handler takes a standard `Request`:

```ts theme={null}
import { WebhookVerificationError, verifyWebhook } from '@technicallyfit/sdk/webhooks';

export async function handleWebhook(request: Request): Promise<Response> {
  try {
    const delivery = await verifyWebhook({
      rawBody: await request.text(),
      headers: request.headers,
      secrets: [process.env.TF_WEBHOOK_SECRET ?? '', process.env.TF_WEBHOOK_PREVIOUS_SECRET ?? ''].filter(Boolean),
    });
    if (delivery.type === 'teammate.disconnected') console.log('stop sending events for', delivery.data.teammate);
    return new Response(null, { status: 204 });
  } catch (error) {
    if (error instanceof WebhookVerificationError) return new Response(null, { status: 400 });
    throw error;
  }
}
```

`headers` also takes a plain object such as Node's `req.headers`, and `rawBody` takes a
string or bytes.

## Python

```python theme={null}
import os

from technicallyfit import WebhookVerificationError, verify_webhook


def handle_webhook(raw_body: bytes, headers: dict[str, str]) -> int:
    secrets = [value for value in (os.environ.get("TF_WEBHOOK_SECRET"), os.environ.get("TF_WEBHOOK_PREVIOUS_SECRET")) if value]
    try:
        delivery = verify_webhook(raw_body, headers, secrets)
    except WebhookVerificationError as error:
        print("refused:", error.reason)
        return 400
    if delivery["type"] == "teammate.disconnected":
        print("stop sending events for", delivery["data"]["teammate"])
    return 204
```

## What a failure means

| Reason | Meaning |
| - | - |
| `missing_header` | The `TF-Webhook-Timestamp` or `TF-Webhook-Signature` header is missing. |
| `invalid_timestamp` | The timestamp is not a number of seconds. |
| `timestamp_out_of_tolerance` | The delivery is more than five minutes from your clock: possibly a replay. |
| `signature_mismatch` | No signature matches your secret: the body changed or the secret is wrong. |
| `invalid_payload` | The signature passed and the body is not JSON. |

Answer a delivery that fails with a `4xx` and do not act on it. Deduplicate on the
`TF-Webhook-Id` header (the payload's `id`): a delivery can arrive more than once.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.