Docs/local-api.txt

2ad5d8160338d289a65d6a4d1c214a02bf916f1f
domain-dig/Docs/local-api.txt history · blame · raw

127 lines · 5539 bytes

  1# DomainDig Local API — `v1`
  2
  3The Local API exposes DomainDig's canonical report data to on-device automation
  4(Shortcuts, scripts, integrations). It is **off by default** and, when enabled,
  5binds only to loopback.
  6
  7- **Base URL:** `http://127.0.0.1:<port>` (default port `47821`, configurable in
  8  Settings → Local API)
  9- **Binding:** loopback only (`acceptLocalOnly`); never reachable off-device
 10- **Content type:** every response is `application/json`
 11- **Version:** `v1` (reported in every response envelope)
 12
 13This document is the stable contract. The response shape is pinned by
 14`DomainDigTests/LocalAPIContractTests.swift`; `LocalAPIContract` (in
 15`LocalAPIContract.swift`) is the single source of truth for the version string
 16and the JSON encoder.
 17
 18## Authentication
 19
 20Every request requires the token shown in Settings → Local API, supplied either
 21way:
 22
 23```
 24Authorization: Bearer <token>
 25```
 26```
 27X-API-Token: <token>
 28```
 29
 30A missing or wrong token returns `401 unauthorized`. Settings → Local API has a
 31**Copy cURL Command** button that emits a ready-to-run authenticated request.
 32
 33## Response envelope
 34
 35Every response — success or error — is wrapped in the same envelope:
 36
 37```json
 38{
 39  "success": true,
 40  "version": "v1",
 41  "data": { "...": "payload, present on success" }
 42}
 43```
 44```json
 45{
 46  "success": false,
 47  "version": "v1",
 48  "error": { "code": "not_found", "message": "The requested Local API route does not exist." }
 49}
 50```
 51
 52- On success, `data` holds the endpoint payload and `error` is **omitted**.
 53- On failure, `error` holds a machine `code` plus a human `message`, and `data`
 54  is **omitted**.
 55
 56### Encoding conventions
 57
 58- **Dates** are ISO-8601 UTC strings, e.g. `"2023-11-14T22:13:20Z"`.
 59- **Absent optional fields are omitted, not `null`.** Consumers must treat a
 60  missing key as "not present."
 61- Object keys are emitted in sorted order (deterministic output; not
 62  contractually meaningful — do not depend on key order).
 63
 64## Endpoints
 65
 66| Method | Path | Payload (`data`) fields |
 67|--------|------|-------------------------|
 68| GET | `/portfolio` | `summary` → `{ totalDomains, healthyCount, warningCount, criticalCount, changedLast24h, expiringSoonCount, unreachableCount }` |
 69| GET | `/domains` | `domains: [TrackedDomain]` |
 70| GET | `/domains/{domain}` | `domain`, `trackedDomain?` (`TrackedDomain`), `latestReport?` (`DomainReport`) |
 71| GET | `/domains/{domain}/history` | `domain`, `history: [HistoryEntry]` |
 72| GET | `/events` | `events: [{ timestamp, domain, summary, status, severity }]` |
 73| GET | `/monitoring` | `isEnabled`, `scope` (`"allTracked"` \| `"selectedOnly"`), `alertsEnabled`, `monitoredDomains: [{ domain, monitoringEnabled, lastMonitoredAt?, lastAlertAt?, certificateWarningLevel }]` |
 74| POST | `/inspect` | body `{ "domain": "example.com" }` → `report` (`DomainReport`) |
 75| POST | `/inspect/{domain}` | `report` (`DomainReport`) |
 76| POST | `/monitoring/{domain}/enable` | `domain`, `monitoringEnabled` |
 77| POST | `/monitoring/{domain}/disable` | `domain`, `monitoringEnabled` |
 78
 79`certificateWarningLevel` encodes as `"none"`, `"warning"`, or `"critical"`.
 80
 81Both `/inspect` routes accept an optional `profile` query parameter, `full` or
 82`quick`. It defaults to `full`, so a caller that omits it gets every section as
 83before. `quick` runs DNS, registration, ownership, TLS, HSTS, HTTP, redirects
 84and PTR, and leaves out reachability, port scanning, subdomain discovery, IP
 85geolocation and name suggestions; each omitted section carries a message saying
 86it was not run.
 87
 88When the device owner has turned off third-party enrichment in Settings, both
 89routes follow that setting and a request cannot override it: ownership,
 90subdomain discovery, IP geolocation and name suggestions are not collected, and
 91registration is decided from DNS alone, which reports `registered` or
 92`unknown` but never `available`.
 93
 94`DomainReport` is the app's canonical report model (the same shape the JSON
 95export produces); see `DomainReportBuilder.swift` for its fields. It is a large
 96object and is treated as an additive contract: new fields may appear without a
 97version bump.
 98
 99## Error codes
100
101| HTTP | `code` | When |
102|------|--------|------|
103| 400 | `bad_request` | The HTTP request line/path could not be parsed |
104| 400 | `invalid_body` | `POST /inspect` body was not `{ "domain": "…" }` |
105| 400 | `invalid_domain` | A path/body domain was empty or invalid |
106| 400 | `invalid_profile` | `profile` was present but named no known profile |
107| 401 | `unauthorized` | Missing or incorrect token |
108| 404 | `not_found` | No such route |
109| 404 | `domain_not_found` | No local data / tracked domain for the given name |
110| 500 | `encoding_failed` | The response could not be encoded |
111| 500 | `internal_error` | The request handler failed unexpectedly |
112
113## Compatibility policy
114
115The `version` field follows a semantic-version-style promise:
116
117- **Backward-compatible changes keep `version` at `v1`.** Adding a new endpoint,
118  or adding a new field to an existing payload, is non-breaking. **Consumers
119  must ignore unknown fields.**
120- **Breaking changes bump `version`.** Renaming or removing a field, changing a
121  field's type, or changing the meaning/units of an existing field requires a new
122  version, an update to this document, and an update to
123  `LocalAPIContractTests.swift`.
124
125There are currently no deprecated fields or endpoints. When a field is
126deprecated, it will be listed here with the version in which it becomes eligible
127for removal, and will remain present for at least one subsequent version.