DocsIntegrate

HTTP API

The concepts of the device HTTP API (pairing, request signing, endpoint groups, actions and timelines) and where to try it online. Endpoint details are in the API reference.

Overview#

The device serves an HTTP API on port 80, with every path under /v1. The stick command line, the MCP server, and the device web app all use this same API.

  • Requests and responses are JSON, in UTF-8.
  • Success: {"ok": true, ...}.
  • Failure: an HTTP status code plus {"ok": false, "error": "<code>", "message": "<description>"}.
  • The device's HTTP server is single-tasked: long polling isn't supported, and no single request should take long. Connections can be reused.

Parameters, request and reply fields, examples and error codes of every endpoint are in the API reference, generated from the API definition openapi.json, which you can also download. This page covers only the concepts and entry points; behavior rules such as boot, Wi-Fi setup and leases are on GitHub in docs/interfaces.md.

Try it online#

The API page: pick an endpoint on the left, send a request and see the response on the right
The API page of the device web app

The API page of the device web app lists every endpoint, at http://<device-address>/#/api. Select an endpoint to read its description, fill in parameters, and send it live; the browser handles signing. Endpoints that press keys, reboot the device, or change data require you to tick a confirmation box before sending.

Pairing#

Pairing is how you obtain the device's key. Both endpoints below need no authentication and are available only in the kernel.

  1. POST /v1/pair with body {"name": "My Script"}. The name is 1–32 bytes and appears on the device screen. Response:

    JSON
    {"ok": true, "id": "…", "code": "4821", "expires_s": 30}
  2. Tell the user that 4821 will appear on the device screen and to click BOOT to allow.

  3. Poll GET /v1/pair?id=<id> once a second until state becomes approved, denied, or expired.

  4. The approved response includes key, 64 hex digits. It is returned only once, so save it immediately.

Only one request can be waiting at a time; otherwise the response is 409 busy. For fields and error codes, see API reference: Pairing.

Authentication#

Every /v1 endpoint requires a signature, except the following:

  • GET /v1/info
  • GET /v1/nonce
  • /v1/pair
  • Static files of the setup page and the device web app

To sign a request:

  1. Get a one-time nonce with GET /v1/nonce: {"nonce": "…", "ttl_s": 60}. Each nonce can be used only once and expires after 60 seconds.

  2. Compute the SHA-256 of the request body, as lowercase hex. Do this for an empty body too.

  3. Compute mac = HMAC-SHA256(key, "stick-v1\n" + method + "\n" + URI + "\n" + nonce + "\n" + body hash), as lowercase hex. The method is uppercase; the URI is the path plus query string exactly as it appears in the request line.

  4. Send two headers with the request:

    X-Stick-Sha256: <body hash>
    Authorization: Stick <nonce>:<mac>

Responses that pass through the authentication check carry an X-Stick-Nonce header containing the next nonce. You can use it directly for the next request and skip a GET /v1/nonce.

Python example:

Python
import hashlib, hmac, json, urllib.request

def call(base, key_hex, method, uri, obj=None):
    body = json.dumps(obj).encode() if obj is not None else b""
    nonce = json.load(urllib.request.urlopen(base + "/v1/nonce"))["nonce"]
    sha = hashlib.sha256(body).hexdigest()
    msg = f"stick-v1\n{method}\n{uri}\n{nonce}\n{sha}".encode()
    mac = hmac.new(bytes.fromhex(key_hex), msg, "sha256").hexdigest()
    req = urllib.request.Request(base + uri, data=body if body else None, method=method, headers={
        "X-Stick-Sha256": sha,
        "Authorization": f"Stick {nonce}:{mac}",
        "Content-Type": "application/json"})
    return json.load(urllib.request.urlopen(req))

call("http://192.168.1.50", KEY, "POST", "/v1/hid/arm", {"seconds": 60})

Authentication errors (auth_required, bad_nonce, bad_mac, unpaired, body_mismatch) and other errors any endpoint can return are listed under Common errors in the API reference. A 401 bad_nonce reply carries a new nonce in its headers, so you can resend right away.

Endpoint groups#

  • Device: info, status, logs, restart, locating the device, screen language, time zone, BOOT button events
  • Wi-Fi: saved networks, adding and forgetting, and the /prov/* endpoints the phone setup page uses during Wi-Fi setup
  • Pairing: request pairing, collect the key
  • HID keyboard and mouse: arming, sending actions, timelines, timelines on the microSD card, emergency stop, monitor screen, USB mode
  • Apps and macros: app management, messages and events, running Lua; the macro player is controlled through messages
  • Storage files: files in device storage
  • microSD card: files on the microSD card, read and written in chunks
  • Screen: screenshots, UI object tree, full-screen mockup preview
  • Updates and boot: firmware updates, confirm, rollback, switching firmware, crash dumps
  • Debug and drills: fault injection, for development

The rescue firmware implements only a subset: it has no HID, apps, files, microSD card, pairing, or device web app, and those paths return 404.

Sending keyboard and mouse actions#

The basic flow:

  1. POST /v1/hid/arm with body {"seconds": 60}: arm output and get a lease.
  2. POST /v1/hid with body {"actions": [...]}: send actions. Response: {"ok": true, "seq": 8, "queued": 5}.
  3. POST /v1/hid/disarm: emergency stop and disarm.
JSON
{"actions": [
  {"tap": "S", "mods": ["ctrl"]},
  {"text": "hello"},
  {"move": [100, 0]},
  {"click": "left"},
  {"wait": 200}
]}
  • The call returns as soon as the actions are queued; it doesn't wait for them to finish. To find out whether they have been sent, check done_seq in GET /v1/hid.
  • Not armed: 403 hid_locked. The computer hasn't recognized the device: 503 not_mounted. The queue can't hold the whole batch: 429 queue_full. Malformed actions: 400 bad_action, and nothing in the batch is queued.
  • At most 512 actions per request; the queue holds 4096 entries in total.
  • Every action form and field is in API reference: POST /v1/hid; key, modifier, mouse button and media names are in the name tables.
  • If the lease expires without renewal, the device clears its queue, releases everything, and disarms on its own. For long tasks, call arm again periodically to renew.

The JSON action format is frozen and will not get new actions. New features are added only to timelines. Macro steps use the same format, plus a few macro-only extensions; see Macro JSON.

Timelines#

A timeline is the device's only execution format: the layer above decides when each frame is sent and what it contains, and the device executes frame by frame on time, without any modeling or randomness of its own.

  • A sequence of 6-byte little-endian records: dt (u16), op (u8), a (u8), b (i8), c (i8).
  • dt is how many milliseconds after the previous record finishes to run this one, 0–5000.
  • op is the operation: key down, key up, relative move, mouse button, wheel, release all, media key, absolute pointer, and so on.
  • Frames are at least 4 milliseconds apart. The device schedules against ideal timestamps, so rounding errors don't accumulate; per-frame accuracy is about 1 millisecond.
  • A batch holds 1–4096 records. The whole batch is validated first and queued only if every record is valid.
POST /v1/hid/timeline?tag=run_x
Content-Type: application/octet-stream

<6-byte records, back to back>

Keys pressed in a timeline must be released by the timeline itself, or end it with a "release all" record. For longer sequences, the host sends batches and renews the lease; or put the file on the microSD card and use POST /v1/hid/play_file to have the device read and play it as it goes, up to 16 MB per file.

Absolute pointer coordinates range from 0 to 32767, spread evenly across the computer's primary display, and are unaffected by mouse acceleration. Secondary displays can't be reached.

For the op code table and examples, see API reference: POST /v1/hid/timeline; key usage codes, modifier bits, mouse button bits and media usage codes are in the name tables. Host-side encoding and checks live in host/stick/hidtl.py.

Monitor screen#

POST /v1/monitor sets what appears on the right side of the device's monitor screen: a name, mode, status, two lines of text, and progress. It takes a ttl_s and clears itself when that expires. It only changes the screen and sends no input. Scripts operating the computer should use it to tell the user what they are doing. For its fields, see API reference: POST /v1/monitor.

Macro player#

A macro is simply the file /data/macro/<macro-name>.json in device storage, read and written with /v1/fs. The player is controlled with app messages:

JSON
{"data": {"cmd": "select", "name": "示例", "where": "fs"}}

Send it with POST /v1/apps/msg (here 示例 is the macro name, meaning "Example"); cmd can be select, run, stop, or reload. Read the player's replies from GET /v1/apps/events. For the macro format, see Macros.

Python library#

The repository's Python package stick wraps connecting, signing, batched sending, and lease renewal, and can be imported directly: stick.client.Client and stick.ops. See host/README.md on GitHub.

View the source of this page on GitHub