<!-- Source: https://keybeat.dev/docs/monitors-as-code -->

# Monitors as code

Keybeat monitors live in your repository, next to the code they check. A `keybeat/` folder holds a `keybeat.json` file and one spec file per monitor. `npx keybeat deploy` makes your Keybeat organization match the folder.

## Layout

```text
your-project/
  keybeat/
    keybeat.json
    login.spec.ts
    checkout.spec.ts
    signup.spec.js
```

Create it with `npx keybeat init`.

## keybeat.json

```json title="keybeat/keybeat.json"
{
  "monitors": [
    { "slug": "login", "name": "Login works", "file": "login.spec.ts", "interval": "15m" },
    { "slug": "checkout", "name": "Checkout reaches payment", "file": "checkout.spec.ts", "interval": "5m" },
    { "slug": "signup", "name": "Signup form loads", "file": "signup.spec.js", "interval": "60m", "enabled": false }
  ]
}
```

| Field | Required | Description |
|---|---|---|
| `slug` | Yes | Unique within your organization. Lowercase letters, digits and hyphens (`[a-z0-9-]`). Used in URLs, the CLI and the API. Changing it creates a new monitor. |
| `name` | Yes | Human readable name, shown in alerts and on your status page. |
| `file` | Yes | Path to the spec, relative to the `keybeat/` folder. |
| `interval` | Yes | How often to run: `60m`, `15m`, `5m` or `1m`. |
| `enabled` | No | `false` deploys the monitor paused, `true` resumes it. When left out, new monitors start enabled and existing monitors keep their current state, so a monitor paused in the dashboard stays paused. |
| `alert_channel_ids` | No | Channel ids to alert, or `null` for every channel in the organization. When left out, the monitor keeps the channels set in the dashboard (all channels for a new monitor). |
| `on_status_page` | No | Show the monitor on your status page. When left out, the monitor keeps the setting from the dashboard (off for a new monitor). |

### Intervals and cost

| Interval | Seconds | Free plan | Paid plan |
|---|---|---|---|
| `60m` | 3600 | Yes, one monitor | Yes, billed as a 15 minute monitor |
| `15m` | 900 | No | 3 included, then $2 per month each |
| `5m` | 300 | No | $5 per month each |
| `1m` | 60 | No | $19 per month each |

Paused monitors still occupy their slot and are still billed. Delete monitors you no longer need. See [Billing](https://keybeat.dev/docs/billing).

## Deploying

```bash
npx keybeat deploy
```

For each entry in `keybeat.json`, `deploy` reads the spec file and upserts the monitor by slug (`PUT /v1/monitors/:slug`). It creates monitors that do not exist and updates the name and script, the interval, and any of `enabled`, `alert_channel_ids` and `on_status_page` that the file sets. Settings the file leaves out keep the values made in the dashboard. Monitors in your organization that are not in the file are left alone. `npx keybeat deploy --dry-run` shows what would change, with the estimated billing change, without changing anything.

To make the organization match the file exactly, deleting monitors that are not listed:

```bash
npx keybeat deploy --prune
```

Deleting a monitor deletes its run history. Use `--prune` with care, and never in an automated job unless you are sure the file is the complete list.

Secrets are not part of `keybeat.json` and should never be committed. Set them once per monitor with `npx keybeat env set` (see [Writing monitors](https://keybeat.dev/docs/writing-monitors#environment-variables-and-secrets)). They persist across deploys.

## Deploying from CI

Create an API key in the dashboard and store it as a CI secret named `KEYBEAT_API_KEY`. The CLI reads it from the environment, so no login step is needed.

```yaml title=".github/workflows/keybeat.yml"
name: Deploy Keybeat monitors
on:
  push:
    branches: [main]
    paths: ['keybeat/**']

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npx keybeat deploy --json
        env:
          KEYBEAT_API_KEY: ${{ secrets.KEYBEAT_API_KEY }}
```

You can also run `npx keybeat test` in CI before deploying, to catch broken specs. That needs Chromium on the CI machine (`npx playwright install --with-deps chromium`) and the monitor's env vars as CI secrets.

## Billing changes on deploy

On a paid organization, a deploy that adds add-ons (for example a new 5 minute monitor) succeeds and updates your Stripe subscription with proration at once. On the free plan, a deploy that needs a paid plan fails for that monitor with HTTP 402 and a checkout link. See [Billing](https://keybeat.dev/docs/billing).
