<!-- Source: https://keybeat.dev/docs/alerts -->

# Alerts

Keybeat is built to wake you up only when something is really broken. A single failed run is never enough: the failure must be confirmed from a second region first.

## The alert rule

1. A **scheduled** run fails.
2. Keybeat immediately starts a **retry** run from a different region. Nothing changes yet and nobody is notified.
3. If the retry **passes**, it was a blip. The monitor stays up and you are not alerted.
4. If the retry **fails** too, the monitor becomes `down`, an incident opens and a **down** alert goes to the monitor's channels.
5. The next run that **passes** marks the monitor `up`, resolves the incident and sends a **recovered** alert with the downtime duration.

You get exactly one "down" alert and one "recovered" alert per incident. Keybeat does not send reminders while a monitor stays down.

Some runs never change state or alert:

- **On-demand runs** (`keybeat run`, the dashboard's run button, `run_monitor`) are for checking things yourself. They never alert.
- **Runs with status `error`** mean trouble on Keybeat's side, such as a machine that failed to start. They are not your site's fault, so they never count as failures. A scheduled run that ends in `error` is retried once.

A run that hits the 60 second limit counts as `failed`.

## Channels

Each organization has a list of alert channels. New organizations start with an email channel for the owner. By default a monitor alerts every enabled channel in the organization. You can limit a monitor to specific channels with its `alert_channel_ids`.

| Channel | Sends | Notes |
|---|---|---|
| Email | down, recovered, slow, slow recovered | Plain email with the monitor name, failing step, error, region and a link to the run. |
| Webhook | down, recovered, slow, slow recovered | JSON POST signed with HMAC-SHA256. Retried 3 times on failure. |
| SMS | down, recovered | Short text message. 20 per month on the base plan. On the free plan you can add an SMS channel, but its messages are skipped until you subscribe. When the cap is reached further messages are skipped. Messages come from the sender name "Keybeat". US and Canadian (+1) numbers are not supported yet. |

Add channels in the dashboard, with `keybeat channels add`, with `POST /v1/alert-channels` or with the `create_alert_channel` MCP tool. Send a test notification with `keybeat channels test <id>` or `POST /v1/alert-channels/:id/test`.

## Webhooks

Keybeat sends a `POST` request with a JSON body and these headers:

```http
Content-Type: application/json
X-Keybeat-Signature: sha256=<hex HMAC-SHA256 of the raw body, keyed with the channel secret>
```

The body has five top level fields: `event`, `monitor`, `incident`, `run` and `org`. `event` is one of `down`, `recovered`, `slow` or `slow_recovered`. An example:

```json
{
  "event": "down",
  "monitor": { "id": "7d3f0c2a-...", "slug": "checkout", "name": "Checkout reaches payment" },
  "incident": { "id": "c1a9e4b0-...", "kind": "down", "opened_at": "2026-12-05T03:14:07Z", "resolved_at": null },
  "run": {
    "id": "0b6c2d9e-...",
    "trigger": "retry",
    "region": "sin",
    "status": "failed",
    "error_message": "Timed out 5000ms waiting for expect(locator).toHaveText(expected)"
  },
  "org": { "id": "5e2b7a11-...", "name": "Acme" }
}
```

Field names inside the nested objects follow the API's monitor, incident and run resources. Treat unknown fields as optional, because new fields may be added.

### Verifying the signature

Each webhook channel has its own secret, shown when you create the channel. Always verify the signature against the **raw** request body before trusting a webhook.

```ts title="Node.js"
import { createHmac, timingSafeEqual } from 'node:crypto';

export function verifyKeybeatSignature(rawBody: string, header: string | null, secret: string): boolean {
  if (!header?.startsWith('sha256=')) return false;
  const expected = createHmac('sha256', secret).update(rawBody).digest('hex');
  const received = header.slice('sha256='.length);
  if (received.length !== expected.length) return false;
  return timingSafeEqual(Buffer.from(received), Buffer.from(expected));
}

// Example with a Fetch API style handler (Cloudflare Workers, Hono, Next.js route handlers)
export async function handle(request: Request): Promise<Response> {
  const raw = await request.text();
  if (!verifyKeybeatSignature(raw, request.headers.get('X-Keybeat-Signature'), process.env.KEYBEAT_WEBHOOK_SECRET!)) {
    return new Response('invalid signature', { status: 401 });
  }
  const payload = JSON.parse(raw);
  console.log(`Keybeat: ${payload.monitor.name} is ${payload.event}`);
  return new Response('ok');
}
```

```bash title="Shell"
# Compute the expected signature of a saved body
printf '%s' "$(cat body.json)" | openssl dgst -sha256 -hmac "$KEYBEAT_WEBHOOK_SECRET" | sed 's/^.* /sha256=/'
```

Respond with a 2xx status quickly. Keybeat makes up to 3 delivery attempts in total, so make your handler safe to call more than once for the same event.

## Slowness detection

Keybeat also tells you when a flow gets slow, without you choosing a threshold.

In plain terms: Keybeat learns how long each monitor normally takes in each region, and alerts when it is clearly slower than normal three runs in a row.

The details:

- **Baseline.** For each monitor and region, Keybeat takes the passed scheduled and retry runs from the last 7 days. It needs at least 20 of them, so new monitors have no slowness alerts for their first few days.
- **Threshold.** The median duration, plus the larger of five times the median absolute deviation (a robust measure of normal variation) or half the median. A monitor that normally takes 4 seconds with little variation is considered slow above about 6 seconds.
- **Slow.** After **3 consecutive** runs over the threshold, the monitor's `perf_state` becomes `slow`, a slow incident opens and a **slow** alert is sent.
- **Recovered.** The first run at or under the threshold sets `perf_state` back to `normal`, resolves the incident and sends **slow recovered**.

Only passed runs count. Failed runs are handled by the alert rule above. Each region has its own baseline, because a site hosted in Europe is naturally slower from Asia. The duration measured is the Playwright test duration, not machine start up time. SMS is never used for slowness alerts.
