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

> ## Agent Instructions
> To set up traceroot in a repository, follow the setup prompt in https://traceroot.ai/docs/getting-started/coding-agents.md.

# Scripting and CI

> JSON output, exit codes, JSON request bodies, and shared flags for scripts and CI

## Run in CI

Set a project API key in the environment. It names its project, so reads need nothing else.

```bash theme={null}
export TRACEROOT_API_KEY="$CI_SECRET"
traceroot traces list --since 1h --json
```

To fail a step when a detector found something in the last hour:

```bash theme={null}
set -e
findings=$(traceroot findings list --since 1h --json)
echo "$findings" | jq -e '.count == 0'
```

A failed CLI call stops the script with the CLI's own [exit code](#exit-codes), so you can tell an auth failure (3) from a network failure (5). `jq -e` exits 1 when there are findings.

An API key can't [list workspaces or projects, or change resources](https://traceroot.ai/docs/cli/authentication.md#browser-login-and-api-keys). For those, store a [session token](https://traceroot.ai/docs/api-reference/authentication.md#session-token-and-cli-access-token) as a secret and set `TRACEROOT_TOKEN` to it. It acts as your account, needs a [project](https://traceroot.ai/docs/cli/authentication.md#choose-a-project) like a browser login, and stops working after 7 days without use. For `setup`, see [Run without prompts](https://traceroot.ai/docs/cli/get-started.md#run-without-prompts).

## JSON output

With `--json`, a command writes one JSON document to stdout. If it fails, stdout is empty and stderr gets `{"error":{"code":"...","message":"..."}}`.

Three commands are different. `doctor` and `setup doctor` write their report to stdout and exit 1 if a check fails. `setup` writes one JSON event per line, ending in a `result` event with `ok` and `data.trace_url`. If it fails, `result` carries an `error` in setup's own codes. Stderr gets no JSON error unless the run never started, though progress text still goes there. Without `--json`, all its messages, including the link to your first trace, go to stderr.

| Command | Document |
| :- | :- |
| `traces list`, `detectors list`, `findings list` | API response, plus `count` and `range` |
| `alerts list`, `projects list`, `workspaces list` | API response, plus `count` |
| `traces get` | API response, plus `finding` |
| `traces export` | `output_dir`, `files`, `finding_id` |
| `doctor`, `setup doctor`, `instrument`, `skills list`, `skills install` | Result under `data` |
| `login`, `logout`, `status` | Their own fields |
| Every other command | API response, unchanged |

```bash theme={null}
traceroot traces list --since 1h --json | jq -r '.data[] | select(.error_count > 0) | .trace_id'
traceroot traces get <trace-id> --json 2>&1 >/dev/null | jq -r '.error.code'
```

## Exit codes

| Exit code | JSON `code` | Meaning |
| :- | :- | :- |
| 0 | | Success |
| 1 | `internal` | Internal error, a failed `doctor` check, a file in the way without `--force`, or another HTTP error, such as 409 or a 5xx |
| 2 | `usage` | Usage error, including a missing `--project` after a browser login, or HTTP 400 or 422 |
| 3 | `auth` | Not authenticated, or not allowed (HTTP 401 or 403) |
| 4 | `not_found` | Not found (HTTP 404) |
| 5 | `network` | Network error, timeout, or rate limit (HTTP 429), retry |
| 130 | | Interrupted |

`setup` can exit 0 when it's paused, hasn't seen a trace yet, or you declined a question, so check for the link. It also uses these:

| Exit code | Meaning |
| :- | :- |
| 10 | It can't choose for you, usually because there are several projects or none. Follow the remedy it prints |
| 11 | Unsupported language, service path, or coding agent |
| 12 | The coding agent failed |
| 14 | No trace arrived in time |
| 15 | This deployment doesn't support a step |
| 16 | It would overwrite something unsafely, or couldn't verify an existing key |

## Send a JSON body

Commands that change something take their fields as one JSON document, named like the flags with underscores. `-` reads stdin. Flags override the file.

```bash theme={null}
traceroot alerts create --from-file rule.json --threshold 2000
jq '.threshold = 2000' rule.json | traceroot alerts create --from-file -
```

A body with a missing, unknown, or invalid field exits 2.

Under a browser login, your [chosen project](https://traceroot.ai/docs/cli/authentication.md#choose-a-project) fills in `project_id`, unless the file or `--project-id` sets it. An API key can't make these writes, even with `--project-id`.

## Shared flags

Every command takes these:

| Flag | Meaning |
| :- | :- |
| `--api-key <key>` | API key to use. It outranks every other [credential](https://traceroot.ai/docs/cli/authentication.md#which-credential-is-used) |
| `--env-file <path>` | `.env` file to read settings from. They outrank the environment |
| `--host <url>`, `--auth-host <url>` | [Another host](https://traceroot.ai/docs/cli/authentication.md#use-another-host) for the API and for sign-in |
| `--project <id>` | [Project](https://traceroot.ai/docs/cli/authentication.md#choose-a-project) for a browser login |
| `--json` | [JSON output](#json-output) |
| `--timeout <ms>` | Per-request timeout. Default: 30000 |

Many commands also take these:

| Flag | Taken by | Meaning |
| :- | :- | :- |
| `--from-file <path>` | Every create, update, status change, and delete | The fields as one [JSON body](#send-a-json-body) |
| `--project-id <id>` | Detector, alert, dashboard, and widget commands, except `detectors list` and `alerts list` | The project to act on. It outranks your [chosen project](https://traceroot.ai/docs/cli/authentication.md#choose-a-project), and must match an API key's project |
| `--limit <n>` | `datasets versions get`, and every list command except `workspaces`, `projects`, `dashboards`, and `skills list` | How many to return. Each command's page gives the default and maximum |
| `--since`, `--from`, `--to` | `traces list`, `detectors list`, `findings list` | A time window, as in [List traces](https://traceroot.ai/docs/cli/traces.md#list-traces) |
| `--reason <text>` | Every delete | Why, 3 to 500 characters, recorded in the audit log. Required |


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