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

# CLI reference

The Keybeat CLI is published on npm as `keybeat`. Run it with `npx`:

```bash
npx keybeat <command> [arguments] [flags]
```

It requires Node.js 20 or later.

## Authentication

The CLI looks for credentials in this order:

1. The `KEYBEAT_API_KEY` environment variable (an API key starting with `kb_`). Use this in CI.
2. `$XDG_CONFIG_HOME/keybeat/credentials.json` (default `~/.config/keybeat/credentials.json`), written by `keybeat login`.

## Environment variables

| Variable | Description |
|---|---|
| `KEYBEAT_API_KEY` | API key (`kb_...`). Takes precedence over saved credentials. |
| `KEYBEAT_API_URL` | API base URL. Default `https://api.keybeat.dev`. |
| `KEYBEAT_APP_URL` | Dashboard URL used by `keybeat open`. Default `https://app.keybeat.dev`. |
| `KEYBEAT_NO_BROWSER` | Set to `1` to never open a browser (`login`, `open` and billing links). |
| `XDG_CONFIG_HOME` | Credentials go to `$XDG_CONFIG_HOME/keybeat/credentials.json`. Default `~/.config`. |
| `NO_COLOR` | Disables colors. |

## Global flags

| Flag | Description |
|---|---|
| `--json` | Print one JSON document on stdout instead of human readable output. Progress goes to stderr. Supported by every command. Agents should always use it. |
| `-y`, `--yes` | Never prompt, and confirm destructive actions. |
| `--no-color` | Disable colors (also when `NO_COLOR` is set or stdout is not a TTY). |

The CLI never prompts when stdout is not a TTY. Destructive commands (`deploy --prune`, `monitors delete`, `channels delete`) then refuse to run without `--yes`: they exit with code 2 and the error code `confirmation_required`, and change nothing.

With `--json`, errors are printed in the API's error format, `{ "error": { "code": "...", "message": "...", "details": { ... } } }`. `details.hint` says how to fix the problem.

## Exit codes

| Code | Meaning |
|---|---|
| 0 | Success. |
| 1 | A run or local test failed, or another error. |
| 2 | Invalid arguments, `keybeat.json` or script, Playwright missing, or confirmation required. |
| 3 | Not logged in, invalid API key or missing permission. |
| 4 | Not found. |
| 5 | The plan or a monthly cap does not allow it (402 with a checkout URL, or the on-demand cap). |
| 6 | Keybeat unreachable, rate limited, or an infrastructure error (run status `error`). |

## Account

### `keybeat login`

Logs in with the device flow. The CLI prints a verification URL and a short code and tries to open the browser. A human logs in at `app.keybeat.dev`, checks that the code matches the one in the terminal, picks an organization and approves. The CLI then receives an API key for that organization and saves it to the credentials file (mode 0600). The request expires after 10 minutes.

| Flag | Description |
|---|---|
| `--no-browser` | Do not try to open a browser. |
| `--no-wait` | Print the URL and code, save the pending request and exit. |
| `--resume` | Continue polling a request started with `--no-wait`. |

Agents whose shell blocks until a command ends should use the two step form:

```bash
npx keybeat login --no-wait --json   # show verification_url and user_code to the human
npx keybeat login --resume --json    # after the human approved
```

### `keybeat logout`

Removes the saved credentials file.

### `keybeat whoami`

Shows the current identity, organization, plan, limits and usage this month (from `GET /v1/me`). Exit code 3 means not logged in or the key is invalid.

```bash
npx keybeat whoami --json
```

### `keybeat open`

Opens the Keybeat dashboard in your browser. `--no-browser` only prints the URL.

## Project

### `keybeat init`

Creates a `keybeat/` folder in the current directory with a `keybeat.json` file and an example spec. It never overwrites files. `--dir <path>` picks another folder name. See [Monitors as code](https://keybeat.dev/docs/monitors-as-code).

### `keybeat test [slug]`

Runs monitors locally with your installed Playwright, using the same configuration as the cloud runner: Chromium headless, one worker, no retries, trace on failure, the 60 second limit and the Keybeat user agent suffix. When you are logged in, requests also carry your organization's `X-Keybeat-Token` header. With a slug, runs only that monitor. Without one, runs every monitor in `keybeat.json`, one after another.

| Flag | Description |
|---|---|
| `--dir <path>` | Path to the keybeat folder. Default `./keybeat`. |
| `--headed` | Show the browser window. The cloud always runs headless. |
| `--no-token` | Do not send the `X-Keybeat-Token` allowlist header. |

```bash
LOGIN_EMAIL=me@example.com npx keybeat test login
```

Specs read environment variables from your shell. Cloud secrets are never downloaded. The command needs `@playwright/test` in the repository and Chromium (`npx playwright install chromium`). It exits with code 1 when a monitor fails, and 2 when Playwright or Chromium is missing or `keybeat.json` is invalid.

### `keybeat deploy`

Validates `keybeat.json` and every spec (nothing is deployed if one script is invalid), compares with the organization, then upserts each new or changed monitor by slug. Unchanged monitors are not touched. Secrets are not part of `keybeat.json` and persist across deploys.

| Flag | Description |
|---|---|
| `--dir <path>` | Path to the keybeat folder. Default `./keybeat`. |
| `--dry-run` | Show what would change, including the estimated billing change, without changing anything. |
| `--prune` | Also delete monitors in the organization that are not listed in `keybeat.json`. Deletes their history. Asks first on a TTY; elsewhere it needs `--yes`. |

Settings made in the dashboard are kept when `keybeat.json` leaves them out: alert channels, the status page flag, and a pause (see [Monitors as code](https://keybeat.dev/docs/monitors-as-code)).

Exit codes: 0 all done, 1 a monitor failed, 2 invalid file or script or confirmation needed, 5 a monitor needs a paid plan.

```bash
npx keybeat deploy --dry-run
npx keybeat deploy --prune --dry-run && npx keybeat deploy --prune --yes
```

## Monitors

### `keybeat monitors list`

Lists monitors with their state, interval and last run.

### `keybeat monitors get <slug>`

Shows one monitor (slug or id): `state`, `perf_state`, interval, whether it is enabled, env var names and its last run. `--script` also prints the script (always included with `--json`).

### `keybeat monitors pause <slug>`

Pauses scheduled runs. A paused monitor still occupies its plan slot.

### `keybeat monitors resume <slug>`

Resumes a paused monitor. Plan limits are checked on resume.

### `keybeat monitors delete <slug>`

Deletes the monitor and its run history. Needs `--yes` when not on a TTY.

## Runs

### `keybeat run <slug>`

Starts an on-demand run of a deployed monitor in the cloud, waits for the result and prints each step with its status and duration. On-demand runs count towards your monthly cap (10 on the free plan, 100 on the base plan). They never change the monitor's state and never send alerts.

| Flag | Description |
|---|---|
| `--region <region>` | `iad`, `fra` or `sin`. Default: random. |
| `--no-wait` | Start the run and return at once. The returned run has status `starting`. |

Exit codes: 0 passed, 1 failed, 5 monthly cap reached, 6 infrastructure error (status `error`).

```bash
npx keybeat run checkout --region sin --json
```

To try a spec that is not deployed yet, use `keybeat test` instead. Keybeat does not run unsaved drafts in the cloud.

### `keybeat runs list [slug]`

Lists recent runs, newest first, for one monitor or all of them.

| Flag | Description |
|---|---|
| `--status <status>` | Only runs with this status: `queued`, `starting`, `running`, `passed`, `failed` or `error`. |
| `--trigger <trigger>` | Only `schedule`, `retry` or `manual` runs. |
| `--limit <n>` | How many runs, 1 to 100. Default 20. |
| `--cursor <cursor>` | The next page (with a slug only). |

### `keybeat runs get <run_id>`

Shows the full run: trigger, region, status, duration, the error message, every step with timings, the masked logs, and short-lived URLs for the trace and screenshot when the run failed.

| Flag | Description |
|---|---|
| `--download <dir>` | Save the trace (`<run_id>-trace.zip`) and screenshot (`<run_id>-screenshot.png`) into this folder. |
| `--no-logs` | Do not print the logs (always included with `--json`). |

## Environment variables

### `keybeat env set <slug> [assignments...]`

Sets one or more encrypted environment variables on a monitor. Values are write only and masked as `***` in logs. Names must match `[A-Z_][A-Z0-9_]*`. Variables persist across deploys. Each assignment is one of:

| Form | Value |
|---|---|
| `NAME=value` | The literal value. It ends up in your shell history and process list. |
| `NAME=@file` | The contents of a file. |
| `NAME=@-` | Read from stdin. |
| `NAME` | Read from stdin, or from a hidden prompt on a TTY. |

One trailing newline is removed from file and stdin values. `--stdin` reads several `NAME=value` lines in dotenv format from stdin instead. Piping is the safe way to pass secrets, because the value never appears on the command line:

```bash
printf '%s' "$LOGIN_PASSWORD" | npx keybeat env set login LOGIN_PASSWORD
npx keybeat env set login LOGIN_EMAIL=@./email.txt
npx keybeat env set login --stdin < .env.monitors
```

### `keybeat env unset <slug> NAME [NAME ...]`

Removes environment variables.

### `keybeat env list <slug>`

Lists the names of a monitor's environment variables. Values are never shown.

## Alert channels

### `keybeat channels list`

Lists the organization's alert channels with their id, type, target and whether they are enabled.

### `keybeat channels add <email|webhook|sms> <target>`

Adds a channel. The target is an email address, an HTTPS URL or a phone number in international format (for example `+447700900123`). US and Canadian numbers (+1) are not supported. SMS alerts are only sent on the base plan. For webhooks, the output includes the signing secret. `--disabled` creates the channel disabled.

### `keybeat channels test <id>`

Sends a test notification through the channel.

### `keybeat channels delete <id>`

Deletes the channel. Needs `--yes` when not on a TTY.

See [Alerts](https://keybeat.dev/docs/alerts) for what each channel sends.

## Billing

### `keybeat billing`

Shows the plan, billing interval, subscription items and quantities, the next invoice estimate and the plan limits (from `GET /v1/billing`). Agents cannot pay: give checkout and portal links to a human.

| Flag | Description |
|---|---|
| `--checkout <month\|year>` | Create a Stripe Checkout link for a human to subscribe. |
| `--portal` | Create a Stripe billing portal link for a human. |
| `--no-browser` | Do not open the link. |

## Examples for agents

```bash
# Who am I, and what can this organization do?
npx keybeat whoami --json

# Preview, then deploy everything in keybeat/keybeat.json
npx keybeat deploy --dry-run --json
npx keybeat deploy --json

# Confirm a monitor passes in the cloud
npx keybeat run checkout --json

# Find the latest failure, read it and download its trace
npx keybeat runs list checkout --status failed --json
npx keybeat runs get 0b6c2d9e-5f1a-4c55-9a77-2f3e1d0c9b8a --download ./keybeat-artifacts --json
```
