# maskyr admin API (local, token-protected)

Base: `http://127.0.0.1:8787/admin/api`. Every request needs header `x-maskyr-admin: <token>`.
The token is regenerated at each start, written to `$MASKYR_HOME/admin.token` (0600), and embedded in
`index.html` as `<meta name="maskyr-admin" content="...">` for same-origin navigations only.
No CORS headers are ever sent for these routes. JSON in, JSON out, errors as `{ "error": { "message": "..." } }`.
`App` and `AppContext` are defined in `src/gateway/server.ts` and `src/gateway/pipeline.ts`.

| Method & path | Body / query | Response |
|---|---|---|
| GET `/state` | | `{ version, listen:{host,port}, vault:{status:'absent'\|'locked'\|'unlocked', keyMode:'passphrase'\|'keyfile'\|null, scopes:number, entries:number}, activePolicyId, policies: Policy[], providers: {id,type,baseUrl,models,hasKey:boolean,keySource:'env'\|'vault'\|'passthrough'\|'none',default:boolean}[], local:{type,baseUrl?,model?,isFixture:boolean,health:{ok,detail}\|null}, kinds: EntityKind[] }` |
| GET `/status` | | Operator status: `{ version, pid, startedAt, uptimeSec, listen, vault:{status,keyMode}, activePolicyId, providers:{id,type,default,hasKey}[], local:{type,configured}, requests:{total,blocked,lastRequestAt} }`. Counts come from the in-memory audit ring. No keys, paths or content. |
| POST `/preview[?reveal=1]` | `{ text: string, manualTerms?: {text,kind?}[], scopeId?: string }` | Dry run (nothing persisted, no network). `{ masked, spans: {start,end,kind,source,confidence,action,pseudonym?}[] (plus `text`, the original characters, only with `?reveal=1`), blocked?: {reason,message}, residual: number, route: 'external'\|'local'\|'blocked' }`. Uses the real engine, policy and vault (so pseudonyms equal what a real send would use). Vault locked => 503. |
| POST `/chat` | same as OpenAI chat body + `{ maskyr?: { manualTerms?, scopeId? } }` | Same pipeline as `/v1/chat/completions` (via `handleChat` with `keepInspection:true`). Non-stream: JSON as OpenAI plus header `x-maskyr-request-id`. Stream: SSE exactly as `/v1/chat/completions`. |
| GET `/transmissions/:id[?reveal=1]` | | `{ id, ts, route, sent, inspection?: {masked,spans}[] }`; with `?reveal=1` each item also has `original` and spans have `text`. Without it no original text is returned (spans carry offsets into the text you sent). From the in-memory ring (last 20, cleared on lock). 404 if gone. |
| GET `/audit?limit=100` | | `{ records: AuditRecord[] }` newest first |
| DELETE `/audit` | | `{ ok: true }` clears the audit log |
| PUT `/policy/active` | `{ id }` | `{ ok:true, activePolicyId }`; saves config |
| PUT `/policy/:id` | a Policy object | Validated with `normalisePolicy`; creates/updates, saves config; returns the stored Policy |
| DELETE `/policy/:id` | | Only non-builtin ids (not balanced/strict/permissive); falls back to balanced if it was active |
| PUT `/providers/:id` | `{ type, baseUrl?, apiKeyEnv?, passthroughAuth?, models?: string[] }` | Upsert provider config (no secrets in config), saves config, calls `app.reload()` |
| DELETE `/providers/:id` | | Removes provider (and its vault secret) |
| PUT `/providers/default` | `{ id }` | Sets `defaultProvider` |
| POST `/providers/:id/key` | `{ apiKey }` | Stores key in the vault secrets as `provider:<id>`; 409 if vault locked. Never returned by any route. |
| DELETE `/providers/:id/key` | | Deletes that vault secret |
| GET `/providers/:id/models` | | `{ models: string[] }` via adapter.listModels with resolved auth (502 + generic message on failure) |
| PUT `/local` | `{ type:'none'\|'openai-compatible'\|'fixture', baseUrl?, model?, detectionModel? }` | Saves config, `app.reload()`; baseUrl validated by `assertLocalUrl` (400 if not loopback/private) |
| POST `/vault/init` | `{ mode:'keyfile'\|'passphrase', passphrase? }` | Creates + unlocks the vault. 409 if exists |
| POST `/vault/unlock` | `{ passphrase? }` | `{ ok:true }`; 403 on wrong passphrase |
| POST `/vault/lock` | | Locks, clears `ctx.transmissions` |
| GET `/vault/scopes` | | `{ scopes: {id,entries,createdAt,lastUsedAt}[] }` |
| GET `/vault/scopes/:id` | `?reveal=1` | `{ id, entries: {index,kind,pseudonym,original,aliases,createdAt}[] }`; `original` is `null` unless `reveal=1` |
| DELETE `/vault/scopes/:id` | | delete scope, save |
| DELETE `/vault/scopes/:id/entries/:index` | | delete one entry, save |
| POST `/vault/purge` | `{ days?: number }` | purge scopes unused for N days (default: active policy retentionDays), returns `{ removed }` |
| GET `/vault/export` | | encrypted vault file bytes as `application/octet-stream` download (`maskyr-vault-backup.json`) |
| POST `/integrations/test` | | Runs a real self-test against this gateway: GET /v1/models and a dry-run masking of a sample sentence; returns `{ checks: {name, ok, detail}[] }` |

Rules for every handler: never log request bodies; never put original values in error messages; validate input types; limit body to 1 MiB (except `/chat`: config.maxBodyBytes); respond 405 for wrong methods.
