# Authentication and scopes API keys with one scope each, limited to the accounts they need; the dashboard's own session; and how the two differ. Every API call carries a key in the `Authorization` header. A request without a valid key answers `401 unauthorized`; a key that may not do what was asked answers `403 forbidden`. ```sh curl -s "$TERMLESS_URL/api/v1/me" -H "Authorization: Bearer $TERMLESS_KEY" ``` ## Keys A key looks like `tl_` followed by 48 hex characters. Termless stores only its SHA-256 hash, so the full key appears exactly once: in the response that creates it. Lose it and you create a new one; the `prefix` (the first 12 characters) is what you see in lists afterwards, to tell keys apart. Create keys in the dashboard (**API keys**) or with `POST /api/v1/keys` using an admin key or the dashboard session: ```sh curl -s -X POST "$TERMLESS_URL/api/v1/keys" -b cookies.txt \ -H "Content-Type: application/json" \ -d '{"name": "EA bridge (gold)", "scopes": ["trade"], "accounts": ["pepperstone-12345678"]}' ``` ```json {"id": "key_3f9a1c07", "name": "EA bridge (gold)", "prefix": "tl_4be1a09", "scopes": ["trade"], "accounts": ["pepperstone-12345678"], "created_at": 1790981179, "last_used_at": null, "key": "tl_4be1a09c7d2e5f8b1a3c6e9d0f2b4a7c8e1d3f5a7b9c0e2d"} ``` Revoke with `DELETE /api/v1/keys/{id}`; the key stops working at once. ## Scopes A key has exactly one scope. Each includes the one before it. | Scope | May | Give it to | |---|---|---| | `read` | List accounts and their status, balance and margin, symbols and specs, quotes, candles, streams, positions, orders and deals. | Dashboards, journals, backtests, analysis agents, anything that only looks. | | `trade` | Everything `read` may, plus place, modify, cancel and close. On an account the broker reports as real money, only once an admin has turned trading on for it (`trade_real`); until then those routes answer `403 real_account`. | Bots and agents that trade. Limit it to the accounts they trade. | | `admin` | Everything, including adding and changing accounts, creating keys, the server directory and diagnostics. | Your own tooling. Never an agent that does not need it. | Scopes apply per route; the reference shows the scope next to every route, and `GET /api/v1/me` shows what the key you hold may do. ## Limiting a key to some accounts A `read` or `trade` key can be created with `accounts: ["pepperstone-12345678"]`. It then sees only those accounts: others are left out of `GET /api/v1/accounts`, `GET /api/v1/health` lists only those, and any account URL outside the list answers `403`. An `admin` key cannot be limited (the request is `400`), because admin routes are not about one account. This is the main safety tool. A trading bot gets a `trade` key for its one demo account; it cannot touch anything else even if its code goes wrong. ## Streams take the key in the query Browsers cannot set headers on `EventSource` or `WebSocket`, so the two stream routes, `GET /api/v1/accounts/{id}/stream` and `GET /api/v1/accounts/{id}/ws`, also accept the key as `?key=tl_...`. No other route reads it (any route accepts `key=` in its query string and ignores it). Prefer the header wherever you can: query strings end up in proxy and browser logs. ## The dashboard session The dashboard does not use keys. It signs in with the admin password at `POST /api/v1/session` and receives `termless_session`, an HttpOnly, `SameSite=Strict` cookie that lasts 30 days and acts like an admin key. Three rules come with it: - A write or a WebSocket authenticated by the cookie whose `Origin` header is another site is refused with `403`. Keys are not affected. - `PUT /api/v1/admin/password` takes the cookie only; a key gets `403`. Changing the password signs out every other dashboard session. - Scripts should use keys, not the cookie. If you do script against the cookie (for example to create keys), sign in once with `curl -c cookies.txt` and send `-b cookies.txt` afterwards: ```sh curl -s -c cookies.txt -X POST "$TERMLESS_URL/api/v1/session" \ -H "Content-Type: application/json" -d '{"password": "your admin password"}' ``` ## First run and the setup token A new install has no admin password. The server prints a one-time **setup token** to its log and writes it to `/setup-token`; `POST /api/v1/setup` with that token and a new password (15 to 200 characters with a lowercase letter, a capital, a digit and a symbol) sets the password and signs you in. This stops anyone who reaches the port first from claiming the install. `GET /api/v1/setup` says whether this step is still pending, and `GET /api/v1/health` reports `status: "setup"` until it is done. (`/health` answers anyone, but gives its `version` and the accounts only to a caller with a valid key or the dashboard session.) Setting `TERMLESS_ADMIN_PASSWORD` in the environment on the first start skips the step. `termlessd passwd` resets the password later from the command line. ## Lockouts Wrong credentials are counted per client IP: - 5 wrong passwords or setup tokens in 15 minutes lock that IP out of sign-in for 15 minutes. - 30 unknown API keys in 15 minutes lock that IP out of key authentication for 15 minutes. A valid key from that IP still works (0.6.3): the lockout refuses unknown keys only, so one stale process polling with an old key cannot lock the other programs on its box out. A locked-out request answers `429 too_many_requests` with a `Retry-After` header. Every wrong password, every lockout and every sign-in is also a row in the [audit trail](/docs/trails) with the client IP. ### Behind a reverse proxy Without `--trust-proxy` every request appears to come from the proxy's address, so one person's wrong passwords lock everyone out, and the lockouts protect nothing. With `--trust-proxy` (`TERMLESS_TRUST_PROXY=1`) the server reads the client IP from **one** header, the one named by `--proxy-header` / `TERMLESS_PROXY_HEADER` (default `X-Forwarded-For`, whose last hop is the one your proxy appended; `X-Real-IP` is never a fallback), and https from `X-Forwarded-Proto`. Two conditions make that safe, and both are yours to meet: - The proxy must **set or overwrite** that header on every request it forwards. Caddy's `reverse_proxy` does for `X-Forwarded-For`; to use `X-Real-IP` instead, add `header_up X-Real-IP {remote_host}` and start with `--proxy-header X-Real-IP`. - Clients must not be able to reach termlessd's port directly (keep `--listen 127.0.0.1:8780`), or they choose their own address and the lockouts mean nothing. The headers are believed only on connections that come from `--trusted-proxies` / `TERMLESS_TRUSTED_PROXIES` (CIDRs; default `127.0.0.0/8,::1`, the machine itself). A program that `--allow-from` lets in directly, such as an app on a private network, is taken at its own address and cannot name another caller. When the proxy runs elsewhere (another machine, or a container: the Docker files set the compose network), list its address there. See [running the server](/docs/running#behind-a-reverse-proxy) for the Caddy snippet. ## Checking what a key may do ```sh curl -s "$TERMLESS_URL/api/v1/me" -H "Authorization: Bearer $TERMLESS_KEY" ``` ```json {"kind": "key", "name": "EA bridge (gold)", "scopes": ["trade"], "accounts": ["pepperstone-12345678"]} ``` `accounts` is `null` when the key may use every account. For the dashboard session `kind` is `"admin"`.