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

# Webhook testing

> Capture the webhooks your application sends during a test, assert on their payloads, and inspect each request

Empirical lets you verify your application sends the correct webhooks. It
gives each test a unique capture URL on the Empirical inbox, lets you query the
payloads it receives, and includes a custom Playwright assertion for use in tests.

Every test repository managed by Empirical includes it, with no setup.

## Usage

### Create a webhook URL

Use `getWebhookUrl` to generate a unique capture URL for your test.
Configure your application to send webhooks to this URL.

```ts theme={null}
import { getWebhookUrl } from "@empiricalrun/playwright-utils";

const webhookUrl = await getWebhookUrl();
// e.g. "https://inbox.empirical.run/hooks/9981f9f4-657a-4ebf-be7c-1915bedd4775"
```

### Custom assertion

Use the `toHaveReceivedWebhook` assertion to verify that a webhook was received.
Pass a capture URL to `expect()`, either one from `getWebhookUrl` or one you
created earlier and saved, with the content to match in the body.

```ts theme={null}
import { getWebhookUrl } from "@empiricalrun/playwright-utils";
import { expect } from "@empiricalrun/playwright-utils/test";

// Works with a newly generated URL
const webhookUrl = await getWebhookUrl();
await expect(webhookUrl).toHaveReceivedWebhook({
  content: orderId,
});

// Or with a capture URL you created earlier
await expect("https://inbox.empirical.run/hooks/your-token-id").toHaveReceivedWebhook({
  content: [roomId, "recording.started"],
});
```

The assertion polls the inbox until a matching webhook is found or the
timeout is reached. When multiple content strings are provided, all of them
must be present in the webhook body.

### Querying webhooks

Use `queryWebhookRequests` to fetch matching webhook requests directly. This
is useful when you need to inspect the payload for further assertions.

```ts theme={null}
import { getWebhookUrl, queryWebhookRequests } from "@empiricalrun/playwright-utils";

const webhookUrl = await getWebhookUrl();
const requests = await queryWebhookRequests(webhookUrl, [roomId, "recording.started"]);

// Access the parsed payload
const body = JSON.parse(requests[0].content);
expect(body.status).toBe("completed");
```

Each request in the returned array is a `WebhookRequest` with the following
properties:

| Property | Type | Description |
| - | - | - |
| `content` | `string` | Raw request body (parse with `JSON.parse` for JSON payloads) |
| `method` | `string` | HTTP method (e.g. `POST`) |
| `headers` | `Record<string, string[]>` | Request headers |
| `created_at` | `string` | Timestamp of when the request was received |

## Assertion options

| Option | Type | Default | Description |
| - | - | - | - |
| `content` | `string \| string[]` | Required | One or more terms to match against the webhook body. Multiple terms are AND'd together. |
| `timeout` | `number` | `300000` | Maximum time in milliseconds to wait for the webhook (default: 5 minutes) |


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