Skip to content
throndir edited this page Nov 14, 2026 · 1 revision

Martlet's API for other apps and scripts

Status: Partial Last Updated: 2026-11-13 Audience: Developers and people who connect apps to a Martlet host Last verified: 2026-11-13 against 5f40af2d9; token format, key-list limits, bearer validation, scope enforcement and GET /version identity checked. UI flow, sync timing and other endpoint contracts remain unchecked. Code: src/Martlet.Core/Access/ApiKeys.cs, src/Martlet.Gateway/, src/Martlet.Gateway.Host.Linux/, src/Martlet.Gateway.Persistence/, src/Martlet.HostClient/HostApiKeys.cs, src/Martlet.Desktop/MainWindow.ApiKeys.cs, src/Martlet.Desktop/ApiKeyDialogs.cs, src/Martlet.Mcp.Protocol/, src/Martlet.NodeLinkCheck/ApiRehearsal.cs

The desktop creates and synchronizes API keys. A separate Gateway host executable serves the host API; the desktop itself does not open this API. The host executable is a candidate, not an installed service. See the host executable guide.

Your own computers use paired, signed connections (Martlet network). An external app uses an API key when it connects to a configured Gateway host.

Making and revoking keys

Share › Apps and API keys › Create API key: name the key after the app that uses it, choose what it may do and when it expires (never, 30 days, 90 days or a year). The key is shown once, with your hosts' addresses, their key pins and an example request; copy it into the app's secret settings. Martlet keeps no copy and can't show it again. Revoke sends a revocation to paired hosts; an offline host receives it when it syncs again. Each key row shows its scopes, the computer that made it, its last use on each host, and the first characters of its ID.

A key looks like martlet_<22-character ID>.<43-character secret>. The fixed martlet_ prefix lets secret scanners spot a leaked key.

Calling a host

What Value
Address The HTTPS origin shown on Devices; :9443 is the host default
Key Authorization: Bearer martlet_... on every request; never in a URL
Bodies Content-Type: application/json (exactly, or with ; charset=utf-8)
Role (optional) X-Martlet-Role: voice, perception or memory filters role-scoped capabilities and status, and selects the role for cancel requests; the default is voice
Errors JSON {code, summary, remedy, trace_id} with a 4xx/5xx status

Pin the SHA-256 fingerprint of the host certificate's public key (SPKI). Gateway renewal keeps the same key; do not assume a fixed renewal interval. The create dialog shows each host's pin in curl's form:

curl -k --pinnedpubkey "sha256//<pin>" \
  -H "Authorization: Bearer $MARTLET_API_KEY" \
  https://192.168.1.20:9443/martlet/v1/version

-k disables normal certificate validation. --pinnedpubkey still rejects a different public key, but does not check the certificate name or validity. Prefer a client that supports public-key pinning without disabling those checks.

GET /martlet/v1/version answers any valid key with the host, its Martlet version and the key's own api_key (id, name, scopes, expires_at), so an app can check its key.

What a key may do

Scope (in the dialog) Endpoints
any key GET /martlet/v1/version, GET /martlet/v1/capabilities
read (See status and logs) GET /martlet/v1/status, GET /martlet/v1/machine (hardware), GET /martlet/v1/cluster (who does what), GET /martlet/v1/logs?after=N[&limit=L], GET /martlet/v1/commands, GET /martlet/v1/commands/{id}
voice (Use thinking, listening, speaking and lip-sync) POST to each route returned by capabilities whose required_role is voice, and POST /martlet/v1/inference/cancel for its own requests
perception (Use screen understanding) POST to each perception route returned by capabilities, and POST /martlet/v1/inference/cancel for its own requests
openai (Use your hosts' language models) GET /v1/models, GET /v1/models/{id}, POST /v1/chat/completions, /v1/completions and /v1/embeddings: the OpenAI-compatible endpoint, on the models the hosts run themselves. With equal-priority too, its requests rank with your own background work instead of below all of your work
manage (Update hosts and change their roles) POST /martlet/v1/commands (martlet.update, host.status, host.describe-role, host.add-role, host.remove-role, host.exposure), POST /martlet/v1/commands/{id}/cancel, and reading commands

A key can never pair devices, read or change your Martlet network, read the voice list (it holds voiceprints), change who does what, upload logs, take a host's commands as its agent, or read or change API keys; those answer key.scope (403). The gateway contract lists each request body and bound. The key scope controls access; the role header does not add permission.

Code Status Meaning
auth.missing 401 No key (or signed request)
key.invalid 401 Unknown key or wrong secret, or the host hasn't heard of the key yet
key.revoked / key.expired 401 Revoked or past its expiry
key.scope 403 The key doesn't allow this request

Using a host's engines

Engine routes take the host's exact route identity, so a reply always comes from the model the host advertises:

  1. GET /martlet/v1/capabilities and pick the route (for example route_id martlet.gateway.ollama-chat.v1).
  2. POST to its path with its route_id, contract_id, contract_version, destination_id, worker_id, adapter_version, model_id, model_revision, model_sha256 and artifact_identity_sha256 copied in, plus protocol_version {"major":2,"minor":0}, fresh session_id, turn_id and request_id (UUIDs), epoch 0, a deadline_utc within the route's maximum_duration_milliseconds, and the route's payload. For a chat: {"input": "...", "temperature": 0.7, "maximum_output_tokens": 256, "maximum_context_tokens": 4096} (optional system and up to 16 history messages {role, text}, think false to answer without thinking first, and context_tokens, the context window the host's Ollama loads: larger than maximum_output_tokens and not larger than maximum_context_tokens, else request.invalid; a chat takes up to 15 minutes (a Deep thinking think, which has no time limit of its own, asks for all of it), 32,768 output tokens and 32,768 context tokens, so long thinking fits). For transcription: {"sample_rate": 16000, "pcm_base64": "..."} (mono 16-bit, at most 30 s).
  3. Read the application/x-ndjson reply: started, text_delta events with text (or audio/face frames), then completed, failed (with code) or canceled.

The route's maximum_concurrency is in capabilities. Most routes allow one request at a time. A Deep thinking route can allow up to four. When all slots are in use, another request gets job.busy. API-key and paired-device requests share these slots.

Commands

curl -k --pinnedpubkey "sha256//<pin>" -H "Authorization: Bearer $MARTLET_API_KEY" \
  -H "Content-Type: application/json" -d '{"kind":"martlet.update","arguments":{"version":"<release-version>"}}' \
  https://192.168.1.20:9443/martlet/v1/commands

Replace <release-version> with a release available to that host. The answer (202) holds the command's id; follow it with GET /martlet/v1/commands/{id}. It runs once Martlet runs on that host, as commands between your computers describe; requested_by shows api-key-<key ID>.

How keys reach your hosts

Keys belong to your Martlet network, like the shared who does what:

  • Each desktop keeps the list in api-keys.json in Martlet's data folder; each host keeps it in api-keys.json beside host.json (0600, service owner). It holds names, scopes, expiry, who made each key and the SHA-256 of each secret; never a usable key.
  • Every 30 seconds while Martlet runs (and right after you make or revoke a key, or pair a host) each desktop merges its copy with every paired host and gives each host whose copy differs the merged list. A key works on a host a moment after it reaches it; a host that was off gets it on its next sync.
  • One entry per key, newest stamp wins, except that a revoked key always wins: an older copy can never bring it back. Revoking also stops a reply the key is streaming on a host that already heard about it.
  • Only paired computers read or change the list (GET/POST /martlet/v1/api-keys, signed). Each host reports when each key was last used there since it started.
  • At most 32 non-revoked keys and 64 entries total. Expired keys still count toward the 32-key limit. Update hosts to a version with API-key support.

Security notes

  • Keys are bearer secrets: whoever has one can use it until you revoke it. Give each app its own key with only the scopes it needs, and an expiry if it is temporary.
  • The Gateway accepts loopback or private-IP origins, not public addresses. To reach a private host from outside your home, use a VPN. Do not forward the port to the internet.
  • The engines a voice key uses run on your hosts, but what the app sends them (text, audio) is processed there and shows in the host's logs only as activity, never content.
  • Your own computers keep using signed requests (HMAC with nonce and clock window over a pinned connection); keys add no way into pairing, the network or other computers.

Why API keys

Option Verdict
Scoped API keys (bearer, over TLS with a pinned host key) Chosen: HTTP clients and scripts can send a header; keys are revocable per app; hosts store verifiers, not usable secrets
Martlet's signed requests (HMAC, what your computers use) Kept for your computers; too much code for most integrations (canonical request, nonce, clock)
OAuth 2.0 Needs an authorization server and token refresh in every app; no benefit for keys you hand out yourself
Client certificates (mutual TLS) Strong, but few integrations can install one, and rotating or revoking them is harder for owners

Where it lives

Piece Location
Key list, format and merge rules src/Martlet.Core/Access/ApiKeys.cs
Host routes and bearer checks src/Martlet.Gateway/GatewayApiKeys.cs and src/Martlet.Gateway/
Linux host storage src/Martlet.Gateway.Host.Linux/ and src/Martlet.Gateway.Persistence/
Desktop client src/Martlet.HostClient/HostApiKeys.cs
Desktop UI and sync src/Martlet.Desktop/MainWindow.ApiKeys.cs and src/Martlet.Desktop/ApiKeyDialogs.cs
MCP api_keys_status and api_selftest in src/Martlet.Mcp.Protocol/; the local rehearsal is in src/Martlet.NodeLinkCheck/ApiRehearsal.cs. See MCP for tool behavior.

Previous qualification (2026-10-02)

This evidence predates later Gateway changes. Do not treat it as current qualification. At that time, api_selftest checked (two real gateways on 127.0.0.1 with the real Ollama relay route over a fixture Ollama, NOT AI: keys made and synced through the desktop client, hosts keeping verifiers only, refusals without a key, with a wrong key and on every endpoint keys may never use, read, voice and manage scopes, the documented curl -k --pinnedpubkey request with Windows' own curl (and a wrong pin refused), a chat through the native route with a bearer key, the same key on a second host and after its restart, last-used reports, revoking mid-reply, a stale copy and expiry), and the desktop's Devices card on a disposable data folder (create with name, scopes and expiry; the key shown once and never returned to MCP; revoke with confirmation; api_keys_status). NOT RUN: a desktop syncing keys with real paired hosts (it needs a real pairing secret in Windows Credential Manager), api-keys.json on a native or Docker Linux host, a real model, Home Assistant or another real integration, and two physical PCs on a real LAN.

The OpenAI-compatible endpoint over the same keys has its own page: Share your language models (MCP openai_selftest).

Planned: Desktop API

A direct API on the desktop (for example, asking the companion to speak) is not implemented.

Martlet documentation

Clone this wiki locally