---
title: "Webhooks"
description: "Verify Affinity webhook signatures with the TypeScript SDK."
---

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

# Webhooks

Verify the signature before you parse the JSON body. Pass the unmodified request bytes to the SDK.

```typescript
import { AffinityWebhookVerificationError, verifyAffinityWebhook } from "@affinity-health/sdk";

export async function handleAffinityWebhook(request: Request) {
  try {
const event = await verifyAffinityWebhook({
  body: await request.arrayBuffer(),
  secret: process.env.AFFINITY_WEBHOOK_SECRET!,
  signature: request.headers.get("affinity-signature"),
});

await saveEvent(event);
return new Response(null, { status: 204 });
  } catch (error) {
if (error instanceof AffinityWebhookVerificationError) {
  return Response.json({ error: error.code }, { status: 400 });
}

throw error;
  }
}
```

The SDK checks the timestamp and HMAC-SHA256 digest. Store each event ID before you apply its state change.

Read the [webhook guide](/guides/webhooks/) to configure endpoints, subscriptions, retries, and replay.

Source: https://docs.affinityrx.com/guides/reference/sdks/typescript-webhooks/index.mdx
