# Use anyr from scripts and AI agents

> Drive the AnyRouter CLI from CI, shell scripts and coding agents with JSON output on every command, JSON errors, stable exit codes and a machine-readable command list.


# Use `anyr` from scripts and AI agents

Every `anyr` command can be run without a terminal. Add `--json` to get output a program can parse. Errors come back as JSON with a fixed exit code, so a script or a coding agent can branch on the result without reading prose.

```bash
anyr help agents        # this guide, in the terminal
```

## Discover the commands

```bash
anyr --help             # the everyday commands
anyr help --all         # every command, grouped
anyr commands --json    # the full command tree as JSON
```

`anyr commands --json` lists each command with its subcommands, flags, aliases, its group, and whether it has JSON output:

```json
{
  "schema": "anyr.commands/v1",
  "commands": [
    {
      "name": "chat",
      "summary": "Ask a model: prompt, stdin or a REPL; --json for scripts",
      "group": "chat",
      "aliases": [],
      "subcommands": [],
      "flags": ["base-url", "config", "key", "model", "no-stream", "profile", "system"],
      "json": true
    }
  ],
  "exit_codes": [{ "code": 0, "name": "ok", "meaning": "success" }]
}
```

New fields may be added; existing fields keep their meaning while `schema` stays `anyr.commands/v1`.

## JSON output

| Setting | Effect |
|---|---|
| `--json` | JSON on stdout for any command that has a JSON form |
| `ANYR_OUTPUT=json` | The same as `--json` on every command |
| `--quiet` or `ANYR_QUIET=1` | No update notices and no background update check |
| `NO_COLOR=1` | No colour. Colour is also off whenever stdout is not a terminal |

Without a terminal, `anyr` shows no prompts, spinners or update notices. A command that would ask before acting, such as revoking a key, fails with a usage error until you pass `--yes`.

## Errors and exit codes

With `--json`, an error prints as one JSON object on stderr, and stdout stays empty:

```json
{"error":{"code":"auth","exit":4,"message":"HTTP 401: invalid key","hint":"sign in again: anyr login"}}
```

| Exit | `code` | Meaning |
|---|---|---|
| 0 | `ok` | Success |
| 1 | `error` | The request or command failed |
| 2 | `usage` | Bad flag, argument or subcommand. Nothing was sent |
| 3 | `not_found` | The model, key, id or resource does not exist |
| 4 | `auth` | No key, a rejected key, or a key without the needed scope |
| 5 | `rate_limited` | Too many requests. Retry later |

## Ask a model

```bash
anyr chat "Reply with ok" --json
```

```json
{"id":"req_…","model":"z-ai/glm-4.7-flash","content":"ok","finish_reason":"stop","usage":{"prompt_tokens":12,"completion_tokens":1}}
```

- `--json` waits for the whole answer and prints one object. `id` is the id `anyr generation <id>` takes, so you can look up cost and latency afterwards.
- `--no-stream` waits for the whole answer and prints only the text.
- Piped input becomes the prompt. A lone `-` places it after your own words:

```bash
echo "Summarise the AnyRouter docs in one line" | anyr chat --no-stream
git diff | anyr chat "Review this diff" - --json
```

## Authenticate without a login

Set a key in the environment; no saved login is needed:

```bash
export ANYROUTER_API_KEY=sk-ar-v1-…        # inference key
export ANYROUTER_MANAGEMENT_KEY=ak_…       # presets, provider keys, device removal
anyr whoami --json
```

## Commands with JSON output

```bash
anyr models --json --free
anyr usage --json
anyr logs --json --status error
anyr keys list --json
anyr embed "text" --json
anyr artifact publish page.html --json
anyr api credits            # any dashboard resource; prints JSON when piped
```

Launchers such as `anyr claude` start an interactive agent and have no JSON form, but their errors still follow the JSON and exit-code rules above. Use `--dry-run` to see the command and environment a launch would use.

## Related

- [Usage, logs & transactions](/cli/usage)
- [Configuration](/cli/configuration)
- [Delegate tasks between coding agents](/cli/delegate)
