11 KiB
Ianitor control API
ianitor-control serves a REST + JSON API on loopback only (127.0.0.1:7878 by default),
under /api/v1. The OpenAPI document is generated from the code: GET /api/v1/openapi.json,
or ianitor-control openapi without a running server. The web UI's contract is
docs/ui-api.md; its last section lists where this API differs.
Everything below was run end to end on loopback against --dev with a real edge
(2026-11-07, WSL2 Debian); the outputs are trimmed with jq, never edited.
Ground rules
-
Token. Every call needs
Authorization: Bearer <API token>. Nothing else identifies a caller (noRemote-User). Without a valid token:401withtoken.missing,token.unknown,token.expiredortoken.revoked. 120 calls a minute per token. -
Host header. Only
127.0.0.1:<port>,localhost:<port>,[::1]:<port>and the configuredallowed_hostsare answered (DNS rebinding). A browser write (OriginorSec-Fetch-Sitepresent) also needsX-Ianitor-UI: 1(CSRF). -
Bodies are JSON, at most 64 KiB, with no unknown fields. Error messages never repeat a value you sent.
-
Errors have one shape;
codeis stable,messageis plain English,fieldpoints at the form field,paramsholds the values in the message,detailis the technical text:{ "error": { "code": "host.hostname.uppercase", "message": "Use lowercase letters only: wiki.dev.test.", "field": "hostname", "params": { "hostname": "Wiki.dev.test", "suggestion": "wiki.dev.test" }, "retry_after_s": null, "detail": "hosts/Wiki.dev.test.toml: `Wiki.dev.test` is not a valid hostname: it must be lowercase" }, "errors": [ ... one per field problem ... ], "request_id": "9c1f…", "server_time": "2026-11-07T17:19:40Z" } -
server_timeis in every JSON object answer. Times are RFC 3339 UTC. -
Changes are applies. Adding, changing, removing a host, Undo and Restore answer
202 {"apply": {...}}at once; pollGET /applies/{id}(every second) untilstateiskept,reverted,failedorneeds_attention. Applies run one at a time, in order; the others arequeued. -
Idempotency-Keyon a write: a retry with the same key gets the same apply back. -
base_rev(fromGET /hosts/{h}or the overview'srev): the change is refused with409 change.conflictonly if the same host file changed since.
Start
Development, everything temporary on 127.0.0.1 (removed on Ctrl-C):
cargo build -p ianitor-edge -p ianitor-control # so --dev finds ianitor-edge next to it
cargo run -p ianitor-control -- --dev
Ianitor control (dev) on http://127.0.0.1:7878/api/v1
Routes repository: /tmp/ianitor-dev-1523/routes.git
Dev proxy: …/ianitor-edge on https://127.0.0.1:18443 (HTTP 18080). tls = "domi" works for app.dev.test, wiki.dev.test, photos.dev.test; CA: /tmp/ianitor-dev-1523/certs/dev-ca.crt
API token (shown once): ian_06484a2c0a6ec73b_vceenNC…
Press Ctrl-C to stop and remove /tmp/ianitor-dev-1523
Without ianitor-edge (or with --edge-bin pointing nowhere) applies are only validated,
compiled and written. In dev mode the health watch lasts 10 s (--watch-secs); in
production 60 s.
Production: the first token comes from the CLI on the server, never over the network:
ianitor-control token create --name mark-laptop --config /etc/ianitor/control.toml
The examples use:
TOKEN=ian_… # from the line above
API=http://127.0.0.1:7878/api/v1
H="Authorization: Bearer $TOKEN"
CA=/tmp/ianitor-dev-1523/certs/dev-ca.crt
visit() { curl -s -o /dev/null -w '%{http_code}\n' --cacert "$CA" --resolve "$1:18443:127.0.0.1" "${@:2}"; }
python3 -m http.server 3000 --bind 127.0.0.1 & # a test upstream
Add a host
curl -s -H "$H" -H 'Content-Type: application/json' -H 'Idempotency-Key: add-app-1' \
-X POST "$API/hosts" -d '{"host": {"hostname": "app.dev.test",
"upstreams": ["http://127.0.0.1:3000"], "tls": "domi", "auth": "none",
"health_check": {"path": "/", "interval_secs": 5}}}' | jq '{state: .apply.state, id: .apply.id}'
{ "state": "queued", "id": "ch_b720484831a9" }
curl -s -H "$H" "$API/applies/ch_b720484831a9" | jq '{state, summary, reason: .reason.message, steps: [.steps[] | "\(.key)=\(.state)"]}'
{ "state": "kept", "summary": "Added app.dev.test",
"reason": "app.dev.test is live. Health checks passed for 10 s.",
"steps": ["validate=done", "compile=done", "write=done", "reload=done", "watch=done", "push=skipped"] }
visit app.dev.test https://app.dev.test:18443/ # 200
The same request with the same Idempotency-Key returns ch_b720484831a9 again and changes
nothing. A host that fails its health check during the watch is reverted: state: "reverted", and reason says why, e.g. "a.test was not changed. 127.0.0.1:41999 answered with an error page on 3 of 3 checks. Ianitor undid the change." The routes repository then
holds the change and its revert commit; edge and the files never left the last good config.
A form error comes back at once, field by field:
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/hosts" \
-d '{"host": {"hostname": "Wiki.dev.test", "upstreams": ["http://10.0.0.23"]}}' \
| jq '{code: .error.code, errors: [.errors[] | {code, field, message}]}'
{ "code": "apply.failed.validation",
"errors": [{ "code": "host.hostname.uppercase", "field": "hostname", "message": "Use lowercase letters only: wiki.dev.test." }] }
POST /hosts/validate?mode=create checks a body without applying it; PUT /hosts/{h} with
{"host": …, "base_rev": …} changes a host; PATCH /hosts/{h} with {"share_links": true}
turns share links on; DELETE /hosts/{h} removes one.
Undo
Undo always names what it will undo; the POST must name the same change, or it is refused
with 409 undo.target_moved. Undo right after an Undo is a redo, and effect says so.
curl -s -H "$H" "$API/undo" | jq '{available, target: .target.summary, effect}'
{ "available": true, "target": "Added app.dev.test", "effect": "Removes app.dev.test" }
TARGET=$(curl -s -H "$H" "$API/undo" | jq -r .target.change_id)
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/undo" \
-d "{\"target_change_id\": \"$TARGET\"}" | jq -r .apply.id
# then poll /applies/{id}:
{ "state": "kept", "summary": "Undid: Added app.dev.test", "reason": "app.dev.test is removed.",
"steps": ["validate=done", "compile=done", "write=done", "reload=done", "watch=skipped", "push=skipped"] }
visit app.dev.test https://app.dev.test:18443/ # 000: no certificate is served for a removed host
History and Restore
One entry per action (a reverted attempt is one reverted entry):
curl -s -H "$H" "$API/history?limit=5" | jq '[.entries[] | {change_id, summary, state}]'
[ { "change_id": "ch_cf0b434ecb4c", "summary": "Undid: Added app.dev.test", "state": "kept" },
{ "change_id": "ch_b720484831a9", "summary": "Added app.dev.test", "state": "kept" } ]
curl -s -H "$H" "$API/history/ch_b720484831a9/restore-preview" | jq '{restorable, changes}'
{ "restorable": true, "changes": [{ "effect": "added", "fields": [], "hostname": "app.dev.test" }] }
REV=$(curl -s -H "$H" "$API/overview" | jq -r .rev)
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/restore" \
-d "{\"change_id\": \"ch_b720484831a9\", \"base_rev\": \"$REV\"}" | jq -r .apply.id
# then poll /applies/{id}:
{ "state": "kept", "summary": "Restored the version of 7 November 2026",
"reason": "app.dev.test is live. Health checks passed for 10 s." }
visit app.dev.test https://app.dev.test:18443/ # 200
Share link
A host with auth = "forward-auth" and share_links = true (dev mode points sign-in at an
Authelia on 127.0.0.1:9091 that is not running, so without a link the request fails closed):
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/hosts" \
-d '{"host": {"hostname": "wiki.dev.test", "upstreams": ["http://127.0.0.1:3000"],
"tls": "domi", "auth": "forward-auth", "share_links": true}}' # poll until kept
visit wiki.dev.test https://wiki.dev.test:18443/ # 502: sign-in unreachable, fails closed
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/hosts/wiki.dev.test/share-links" \
-d '{"path_prefix": "/", "expires_in_s": 86400}'
{ "id": "ec724bb1420da0d8d81ee897e1e5344b", "expires_at": "2026-11-08T17:20:05Z", "path_prefix": "/",
"has_password": false, "url": "https://wiki.dev.test:18443/?share=ish1.…", "warning": null }
The URL is shown once (control keeps no token). The first visit trades the token for a
path-scoped HttpOnly; Secure; SameSite=Lax cookie and redirects to the same URL without it:
curl -s -o /dev/null -w '%{http_code} -> %{redirect_url}\n' --cacert "$CA" \
--resolve wiki.dev.test:18443:127.0.0.1 -c jar "$URL" # 303 -> https://wiki.dev.test:18443/
visit wiki.dev.test -b jar https://wiki.dev.test:18443/ # 200, no sign-in
Stop it; edge refuses it at once (and within 5 s even if control's nudge is lost):
curl -s -H "$H" -X DELETE -o /dev/null -w '%{http_code}\n' "$API/share-links/ec724bb1420da0d8d81ee897e1e5344b" # 204
visit wiki.dev.test -b jar https://wiki.dev.test:18443/ # 502: back to sign-in
visit wiki.dev.test "$URL" # 403: "This link no longer works"
Options: "password": "at least 8 characters" (the visitor gets a password page; 5 wrong
tries a minute per link and network), "replaces": "<id>" stops an earlier link,
POST /share-links/stop-all stops every link made so far, GET /share-links lists them
(without tokens).
Tokens
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/tokens" -d '{"name": "backup script"}'
# 201 {"id": "…", "name": "backup script", "token": "ian_…", "expires_at": "2027-11-07T…"} (shown once)
curl -s -H "$H" "$API/tokens" # ids, names, dates; never a value or hash
curl -s -H "$H" -X DELETE "$API/tokens/<id>" # 204; anything using it stops now
curl -s "$API/overview" | jq .error.code # "token.missing"
All calls
| Call | Does |
|---|---|
GET /overview |
the main screen in one call (hosts with health, engine, last apply, undo, active apply, repo, site); ETag/304 |
GET /hosts, GET /hosts/{h} |
hosts; one host with rev, its TOML, health and certificate |
POST /hosts/validate?mode=create|edit |
check a host body |
POST /hosts, PUT /hosts/{h}, PATCH /hosts/{h}, DELETE /hosts/{h}?base_rev= |
applies |
POST /hosts/{h}/certificate/retry |
try Let's Encrypt again now |
GET /applies, GET /applies/{id}, DELETE /applies/{id} |
apply records; cancel a queued one |
GET /history, GET /history/{id}, GET /history/{id}/restore-preview |
history, one entry, restore preview |
GET /undo, POST /undo, POST /restore |
undo and restore |
POST /repo/push, GET /settings |
push now; settings |
GET/POST /tokens, PATCH/DELETE /tokens/{id} |
API tokens |
POST /hosts/{h}/share-links, GET /share-links, DELETE /share-links/{id}, POST /share-links/stop-all |
share links |
GET /health |
edge reachability, config hash, trial, paused state, hosts |
GET /openapi.json |
the OpenAPI document |