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

# Webhooks

> Verify webhook signatures and handle event notifications with the SDK

The SDK ships helpers for verifying webhook signatures and parsing event payloads. For the full list of webhook events and payload shapes, see the [Webhooks reference](/api-reference/endpoint/webhooks/introduction).

## Setup

Pass your webhook public key when initializing the SDK:

```typescript theme={null}
const afriex = new AfriexSDK({
  apiKey: "your-api-key",
  webhookPublicKey: "your-afriex-public-key",
});
```

## Verify and Parse

Validate the signature and parse the payload in one call:

```typescript theme={null}
app.post("/webhook", (req, res) => {
  const signature = req.headers["x-webhook-signature"];
  const payload = req.body; // Raw string body

  try {
    const event = afriex.webhooks.verifyAndParse(payload, signature);

    switch (event.event) {
      case "CUSTOMER.CREATED":
      case "CUSTOMER.UPDATED":
      case "CUSTOMER.DELETED":
        console.log("Customer event:", event.data);
        break;
      case "TRANSACTION.CREATED":
      case "TRANSACTION.UPDATED":
        console.log("Transaction event:", event.data);
        break;
      case "PAYMENT_METHOD.CREATED":
      case "PAYMENT_METHOD.UPDATED":
      case "PAYMENT_METHOD.DELETED":
        console.log("Payment method event:", event.data);
        break;
      case "CHECKOUT_SESSION.CREATED":
        console.log("Checkout session event:", event.data);
        break;
    }

    res.status(200).send("OK");
  } catch (error) {
    console.error("Invalid signature");
    res.status(400).send("Invalid signature");
  }
});
```

## Verify Only

Validate the signature without parsing:

```typescript theme={null}
const isValid = afriex.webhooks.verify(payload, signature);

if (isValid) {
  const event = JSON.parse(payload);
  // Handle event
}
```

## Trigger Test Webhook (Sandbox)

Send a test webhook to your endpoint in sandbox:

```typescript theme={null}
const result = await afriex.webhooks.triggerTestWebhook({
  event: "TRANSACTION.UPDATED",
  resourceId: "transaction-id",
});

console.log(result); // { success: true, message: "..." }
```

For `CHECKOUT_SESSION.CREATED`, use the checkout session UUID as `resourceId`.

<Note>
  Test webhook triggers only work in the sandbox environment.
</Note>
