# Accounts and keys in the CLI

> Use several AnyRouter accounts, give each coding agent its own key, and manage API keys from the terminal.


# Accounts and keys

The CLI supports any number of accounts (work, personal, a self-hosted gateway), each with its own key, base URL and defaults, and can list, create, update and revoke API keys.

## Multiple accounts

```bash
anyr account list              # * marks the active account
anyr account add work          # sign in to a second account
anyr account use work          # switch
anyr account rename work team
anyr account remove team       # not the active account; also deletes its stored keys
```

Accounts live in `~/.anyrouter/config.yaml`. To pick one, strongest first: `--profile <name>` on a command, `ANYROUTER_PROFILE=<name>` in the environment, then the persistent default from `anyr account use`.

## One key per coding agent

Separate keys keep each agent's spend, limits and revocation apart. These commands arrive in the first `anyr` release after 0.1.37 (`anyr upgrade`).

```bash
anyr keys create --agent claude --limit 20   # mint a key for Claude Code and save it for it
pbpaste | anyr keys set --agent codex -      # store a key copied from the dashboard
anyr account use work --agent opencode       # OpenCode launches with the work account
anyr auth status                             # the key each agent launches with
```

`--agent` takes any launched agent: `claude`, `codex`, `opencode`, `grok`, `pi`, `qwen`, `goose`, `aider`, `crush`, `kilo`, `pool`, `fx`.

A launch picks its key in this order:

1. `--key`, then `ANYROUTER_API_KEY`.
2. The agent's own key (`keys create --agent` or `keys set --agent`).
3. `--profile` or `ANYROUTER_PROFILE`.
4. The agent's account (`account use <name> --agent`).
5. The active account.

- `keys create --agent` mints the key in that agent's account, prints it once and saves it. The scope flags (`--limit`, `--reset`, `--endpoints`, `--expires`) work as usual; `--quiet` still saves it and prints only the secret. It refuses while `--key` or `ANYROUTER_API_KEY` is set.
- `keys set --agent <agent> -` reads the key from stdin (an argument is refused, so it stays out of shell history). Only a whole `sk-ar-v1-` key is accepted.
- `anyr keys unset --agent codex` removes the agent's own key. The key keeps working until you revoke it; the CLI prints the `anyr keys revoke` command.
- `anyr account use --agent opencode --clear` removes an account binding. Renaming an account keeps its bindings; removing it unbinds them.

## Who am I, and signing out

```bash
anyr whoami          # or: anyr status; --json for scripts
anyr logout                    # active account
anyr logout --profile work     # a specific account
```

`whoami` shows name, email, active account, a masked key hint, default model and config path, plus the masked key and source for each agent with its own key or account (`agents` in `--json`). Full secrets are never printed. `logout` removes the account's key from `config.yaml`; `anyr auth login` restores it.

## Managing API keys

```bash
anyr keys list           # * marks the key this machine uses
anyr keys create laptop  # named key (default: cli-<hostname>)
anyr keys show <hash>    # limit, spent, endpoints, expiry
anyr keys update <hash> --limit 50 --disable   # --enable turns it back on
anyr keys revoke <last4|id|hash>               # asks first; --yes in scripts
```

- CLI login mints (or reuses) a key with Key Management permission, so `anyr keys` works right after `anyr auth login`. If a dashboard key has Key Management off, enable it or log in again.
- `keys create` prints the key once. In a terminal the CLI offers to store it for the current account.
- An existing key's secret cannot be fetched again. To switch keys, run `anyr login`, or `anyr login --key -` to paste one from the dashboard.
- `keys revoke` warns when you revoke the key the account is using.

## Scoped keys for CI

```bash
anyr keys create ci-bot --limit 25 --reset monthly --endpoints chat,embeddings --expires 30d
KEY=$(anyr keys create ci-bot --limit 5 --quiet)   # prints only the secret; not saved
```

- `--limit`: spend cap in USD. `--reset`: window (`daily`, `weekly`, `monthly`).
- `--expires`: `30d`, `12h`, `45m` or `2w`.
- `--endpoints`: `chat`, `embeddings`, `moderations`, `rerank`, `documents`, `speech`, `decisions`, `mcp`, `me`, `relay`, `models`, `keys`, `credits`, `logs`, `dashboard`, `trace`. Chat, messages and responses share one permission, so `chat` covers all three.
- `--quiet` prints only the secret on stdout, safe to capture.

## Check and script

```bash
anyr claude --dry-run  # command and environment a launch would use (secrets redacted; works on every launcher)
ANYROUTER_PROFILE=ci anyr whoami --json | jq -r .base_url
anyr keys list --json | jq -r '.[] | select(.current) | .name'
```

`account list` is line-per-account text; `whoami --json` and `keys list --json` emit masked values and presence flags only, never secrets.


## Related

- [Login with AnyRouter](/docs/cli/login.md)
- [Configuration](/docs/cli/configuration.md)
- [Use anyr from scripts and AI agents](/docs/cli/agents.md)
