Local HTTP API (#285)¶
Mesh Client can expose a small localhost-only HTTP API so scripts and companion tools (similar in spirit to the TAK server) can read mesh state and send chat on the active protocol tab.
Constants and route paths live in src/shared/hobbyistGapsContracts.ts. Enable the server under App → Local API.
Security defaults¶
| Setting | Default |
|---|---|
| Server enabled | Off |
| Authentication | Off (operator can require Basic) |
| Bind address | 127.0.0.1 only (not configurable) |
Loopback TCP does not identify the desktop OS user. Authentication is optional: the operator can enable the listener with or without credentials. When Authentication required is on, a User ID and Password must be saved before the listener starts, and all routes (including health and WebSocket upgrades) require HTTP Basic (Authorization: Basic base64(userid:password)). Passwords must be at least 12 characters (LOCAL_API_PASSWORD_MIN_LENGTH). When authentication is off, Basic is not checked (Origin / Sec-Fetch-Site browser refusal and rate limits still apply).
Browsers are refused¶
The API is for scripts, not web pages. Any request that carries an Origin header (every cross-origin POST and every WebSocket handshake from a page, including Origin: null) or a Sec-Fetch-Site header (every request a browser makes to 127.0.0.1, including no-cors GETs such as an image) gets 403 { "ok": false, "error": "browser_request_refused" }. The refusal happens before authentication and before either rate budget, so a page cannot send as the user with Basic credentials the browser has cached, and cannot use up the budgets below. curl and Node fetch send neither header by default; if your client adds one, remove it. Opening the health URL in a browser is refused too.
POST /api/v1/messages/send also requires Content-Type: application/json (parameters such as charset are allowed); anything else gets 415 unsupported_media_type before the body is read.
Rate limits¶
Failed Basic-auth attempts (HTTP and WebSocket upgrades) share a per-minute budget (LOCAL_API_AUTH_FAILURE_LIMIT_PER_MIN, default 30). Exhausted budgets return 429 instead of distinguishable unthrottled 401s. Overall request rate limiting (LOCAL_API_HTTP_RATE_LIMIT_PER_MIN, default 240) still applies separately.
Both budgets are per server, not per client, and the auth-failure budget is checked before the password. A local process that sends 30 wrong passwords in a minute therefore locks out every client, the real one included, until the minute passes; a correct password during the lockout also gets 429, so the lockout does not tell a guesser which attempt was right. Loopback gives no per-client identity to split the budgets by. If your script gets 429, wait a minute and look for another program on the machine probing the port.
Credentials are stored in secure_secrets (localApi:basicAuth), not in plaintext app_settings. IPC status never returns the password. Forgetting credentials while the server is enabled and authentication is required stops the listener; with authentication off, the listener can keep running without Basic. The user ID may not contain a colon, because HTTP Basic splits userid:password at the first one; Settings and the IPC handler refuse it on save, and a legacy stored user ID that contains a colon is ignored (and cleared) on load.
The enabled flag is saved only once the listener is up. If the port is in use (by another program or a second copy of Mesh Client), enabling fails, the API stays off and Settings says which port is taken; the same happens when applying a new port or saving new credentials restarts the listener on a taken port. If the listener cannot start at launch, the flag stays on so the next launch tries again, and Settings shows that the API is on but not listening. While the API is enabled, the status bar shows a Computer indicator (listening = ok, enabled but not listening = warn); clicking it opens App → Local API.
Routes¶
| Method | Path | Description |
|---|---|---|
GET |
/api/v1/health |
App version and whether a radio link is up |
GET |
/api/v1/nodes |
Nodes or contacts of the focused tab's identity |
GET |
/api/v1/messages?limit=200 |
Its recent messages (limit capped at 2000) |
POST |
/api/v1/messages/send |
JSON body, sent on the focused tab's identity |
WS |
/api/v1/events |
Sends one hello message; no events are pushed |
What nodes and messages return¶
Both routes read SQLite for the identity the focused protocol tab shows; the renderer reports it to main whenever the tab or identity changes. Each answer names it: { "ok": true, "identityId": "...", "protocol": "meshcore", "nodes": [...] } (or "messages"). Before the renderer has reported one (for a moment after launch), both return an empty list with identityId and protocol set to null. Rows are table rows, newest first, with a fixed column list (LOCAL_API_COLUMNS in local-api-reads.ts): every column except channel_key on the Meshtastic and MeshCore message tables. channel_key is a 64-bit digest of the channel name and secret, so a guessable PSK could be confirmed against it; it is never returned. None of these tables stores a raw PSK or a private key. Reticulum message rows include attachment_path and attachments_json, which are local file paths. A column added to a table later is not returned until it is added to the list.
| Focused protocol | /nodes reads |
/messages reads |
|---|---|---|
| Meshtastic | nodes, every row (the table has no identity column) |
messages for the identity, by receipt time |
| MeshCore | meshcore_contacts for the identity, newest last_advert first |
meshcore_messages for the identity, by receipt time |
| MeshCom | meshcom_contacts under the stable MeshCom slot |
meshcom_messages under the stable MeshCom slot |
| Reticulum | reticulum_destinations, every row (no identity column) |
reticulum_messages for the identity, by receipt time |
Meshtastic and MeshCore also return rows saved before identities existed (an empty identity_id) for the offline identity, as their chat views do. MeshCom always stores under one stable slot, whatever the session identity. MeshCore room posts are rows of meshcore_messages and are included. Other tables (RRC rooms, telemetry, position history) are not exposed.
connected in the health answer is true while a Meshtastic or MeshCore radio link is up or the Reticulum sidecar runs. It does not count MQTT, so it can read false while a send over MQTT succeeds.
POST /api/v1/messages/send body:
- The renderer decides whether the focused identity can send: an RF link or MQTT for Meshtastic, MeshCore and MeshCom, the running sidecar for Reticulum. Without one it returns 503 with
{ "ok": false, "error": "disconnected" }. An MQTT-only connection can send. - 502
send_failedwhen the send could not be queued or the renderer did not answer within 30 s; 502no_rendererwhen the main window is closed. - 400
invalid_bodyfor malformed JSON, emptytext, a negative or fractionalchannel, or a fractionaltoNode. - Bodies over
LOCAL_API_MAX_BODY_BYTES(64 KiB) get 413body_too_largeonce the client has finished sending; a body that keeps coming past a further 8 MiB is cut off.
curl examples¶
Replace PORT with your App → Local API port (default 18765).
With authentication required (omit -u when authentication is off):
curl -s -u 'myuser:mypass' "http://127.0.0.1:PORT/api/v1/health"
curl -s -u 'myuser:mypass' "http://127.0.0.1:PORT/api/v1/nodes"
curl -s -u 'myuser:mypass' "http://127.0.0.1:PORT/api/v1/messages?limit=50"
curl -s -u 'myuser:mypass' -X POST "http://127.0.0.1:PORT/api/v1/messages/send" \
-H 'Content-Type: application/json' \
-d '{"text":"Hello from curl","channel":0}'
Wrong or missing credentials (when authentication is on) receive 401 with WWW-Authenticate: Basic.
WebSocket events (optional)¶
When authentication is on, the socket authenticates and answers {"type":"hello","ok":true}, then stays open. Mesh Client does not push any other events on it yet.
# --basic-auth takes the raw user:password value; websocat base64-encodes it.
websocat --basic-auth 'myuser:mypass' "ws://127.0.0.1:PORT/api/v1/events"
Omit --basic-auth when authentication is off.
Implementation map¶
| Area | File |
|---|---|
| HTTP server | src/main/local-api-server.ts |
| IPC + lifecycle | src/main/ipc/local-api-handlers.ts (sibling local-api-handlers.js in production) |
| Status bar hook | src/renderer/hooks/useLocalApiStatus.ts (localApi:status push) |
| Renderer send bridge | src/renderer/hooks/useLocalApiBridge.ts (also reports the focused identity) |
| Scoped reads | src/main/local-api-reads.ts |