DocsReferenceAPI reference

Apps and macros

Lua apps on the device, one running at a time: list, start, stop, start on boot, delete, message, read events and run code in the app. The macro player is the app macro, controlled through POST /v1/apps/msg

GET /v1/apps#

Apps, the running app, start on boot, memory

Signature required Kernel only

Lists the apps under /apps in storage (ones with a broken manifest too, with error), plus the runtime state. Changes nothing

Reply#

Apps and runtime state

FieldTypeRequiredValuesRangeDescription
okbooleanYestrueAlways true
appsobject[]YesApps in storage
idstringYesApp ID (folder name)
namestring≤ 64 bytesName (manifest name; defaults to the id)
versionstring≤ 32 bytesVersion (manifest version; may be empty)
permsstring[]hidPermissions: only hid so far (keys through the app source)
autobooleanYesStarts on boot
sizeintegerYes≥ 0Total bytes of the folder's files
errorstringbad_manifestBroken manifest: missing, not JSON or breaking the rules
runningstring | nullYesID of the running app (also in the error state); null if none
statestringYesstopped running errorstopped: none running; running; error: halted on an error (timers stopped, UI kept)
errorstring | nullYesText of the latest error (Lua error and location); null if none
memintegerYes≥ 0Lua memory used by the app (bytes)
mem_maxintegerYesLua memory limit per app (bytes)
started_msintegerYes≥ 0Uptime in ms when the app started; 0 if none running
autostring | nullYesApp started on boot; null if none

Error codes#

StatusCodeDescription
503no_storageStorage is not mounted. See GET /v1/logs

For errors any endpoint can return, see Common errors.

POST /v1/apps/start#

Start an app (the macro player is macro)

Signature required Kernel only

Stops the running app, reads the manifest and runs the entry file's top level (limit 1000 ms), replying when it finishes. A top-level error still returns HTTP 200, with ok false and error app_error; the app stays in the error state. A manifest with the hid permission opens the app source

Request body#

application/json

Names one app

FieldTypeRequiredValuesRangeDescription
idstringYespattern ^[a-z0-9_-]{1,24}$App ID: 1–24 of a-z 0-9 _ -, the folder name under /apps. IDs starting with _ are for temporary host apps (such as _try)

Examples#

Start the clock#

JSON
{"id": "clock"}

Start the macro player#

Once started, a click of BOOT runs the current macro

JSON
{"id": "macro"}

Reply#

Started; ok is false on a top-level error

ok false means a top-level error (HTTP 200); the app stays in the error state

FieldTypeRequiredValuesRangeDescription
okbooleanYesWhether the app started
runningstringID of the started app (on success)
errorstringapp_errorTop-level error (on failure)
messagestringLua error text (on failure)

Error codes#

StatusCodeDescription
503no_storageStorage is not mounted. See GET /v1/logs
400bad_jsonThe body is not a JSON object
400bad_idid is not 1–24 of a-z 0-9 _ -. See the example “Start the clock” for the correct form
404no_appNo such app in storage (message is the id). Run stick push first
400bad_manifestmanifest.json is missing, not JSON, or breaks the manifest rules (docs/lua.md section 2)
500start_failedInternal error such as the runtime not ready (message is the ESP-IDF error name)

For errors any endpoint can return, see Common errors.

POST /v1/apps/stop#

Stop the current app and return home

Signature required Kernel only

Stops the running app (calls app.on_stop first, limit 200 ms), closes the app source and shows the home screen. Succeeds when no app is running too. No body needed

Reply#

Stopped

Successful reply; endpoints add their own fields

FieldTypeRequiredValuesRangeDescription
okbooleanYestrueAlways true

Error codes#

StatusCodeDescription
400bad_jsonA body was sent but is not a JSON object. Send no body

For errors any endpoint can return, see Common errors.

POST /v1/apps/auto#

Set or clear start on boot (id null clears)

Signature required Kernel only

Starts this app 3 s after boot; id null clears it. Cleared automatically after 3 errors in a row within 10 s of starting

Request body#

application/json

Sets or clears start on boot

FieldTypeRequiredValuesRangeDescription
idstring | nullYespattern ^[a-z0-9-][a-z0-9_-]{0,23}$App ID to start on boot, not starting with _; null clears

Examples#

Start the clock on boot#

JSON
{"id": "clock"}

Clear start on boot#

JSON
{"id": null}

Reply#

Saved

Saved start-on-boot setting

FieldTypeRequiredValuesRangeDescription
okbooleanYestrueAlways true
autostring | nullYesApp now started on boot; null if none

Error codes#

StatusCodeDescription
503no_storageStorage is not mounted. See GET /v1/logs
400bad_jsonThe body is not a JSON object
400bad_idid is not a string or null, not 1–24 of a-z 0-9 _ -, or starts with _ (temporary apps cannot start on boot). See the example “Start the clock on boot” for the correct form
404no_appNo such app in storage (message is the id). Run stick push first
400bad_manifestmanifest.json is missing, not JSON, or breaks the manifest rules (docs/lua.md section 2)
500nvsSaving to NVS failed. Try again

For errors any endpoint can return, see Common errors.

POST /v1/apps/delete#

Delete an app folder (keeps its data folder)

Signature required Kernel only Changes data

Deletes the whole /apps/<id> folder: stops the app first if it is running and clears start on boot if set. The data folder /data/<id> is kept

Request body#

application/json

Names one app

FieldTypeRequiredValuesRangeDescription
idstringYespattern ^[a-z0-9_-]{1,24}$App ID: 1–24 of a-z 0-9 _ -, the folder name under /apps. IDs starting with _ are for temporary host apps (such as _try)

Examples#

Delete the app hello#

JSON
{"id": "hello"}

Reply#

Deleted

Successful reply; endpoints add their own fields

FieldTypeRequiredValuesRangeDescription
okbooleanYestrueAlways true

Error codes#

StatusCodeDescription
503no_storageStorage is not mounted. See GET /v1/logs
400bad_jsonThe body is not a JSON object
400bad_idid is not 1–24 of a-z 0-9 _ -
404no_appNo such app in storage (message is the id). Run stick push first
500io_errorDeleting the folder failed (storage error). See GET /v1/logs

For errors any endpoint can return, see Common errors.

POST /v1/apps/msg#

Message the running app; macro player: select / run / stop / reload / plan

Signature required Kernel only Sends input

Converts data to a Lua value for the running app's app.on_message and replies once it is queued, without waiting; read the app's response in GET /v1/apps/events. The macro player (app macro) takes AppsMacroCommand; for a bad command it emits an error event and the endpoint still succeeds. Apps in the error state receive nothing

Request body#

application/json

Message for the running app

FieldTypeRequiredValuesRangeDescription
dataobjectYesValue for app.on_message. The macro player takes AppsMacroCommand
Macro player commandobjectdata for the macro player (app macro). Results arrive as selected, started, stopped, plan, error and other events in GET /v1/apps/events (docs/web.md section 7); the macro file format is web/spec/macro.schema.json
cmdstringYesselect run stop reload planselect: set the current macro (stops a running one; needs name); run: run the current macro, or with name a trial run of that macro (once, current macro unchanged); stop: stop and reset; reload: re-read the current macro (after the file changed); plan: dry run — load and check, then walk the first steps with a fake hid and emit a plan event (step counts, estimated time, the hid.* calls each step would make); sends no HID, keeps the current macro and anything running
docobject | stringplan only: the macro itself (a macro object or its JSON string) instead of reading a file by name / where
traceintegerdefault 200–200plan only: how many steps to walk and list
jsonbooleandefault falseplan only: include the normalized macro JSON in the event (dropped first when the event would exceed 4 KB)
namestring | nulllength 1–48; ≤ 48 bytesMacro name: the file name without .json
wherestring | nullfs sd nullWhere the macro is: fs = device storage /data/macro/ (default), sd = microSD card /stick/data/macro/
#2Other apps: any JSON

Examples#

Macro player: set the current macro#

Makes the macro 每日签到 (daily check-in) on the device current (stops a running one first); emits selected

JSON
{"data": {"cmd": "select", "name": "每日签到", "where": "fs"}}

Macro player: run the current macro#

Presses keys on the computer. If already running, emits started with already

JSON
{"data": {"cmd": "run"}}

Macro player: trial run#

Runs it once without changing the current macro; events carry trial

JSON
{"data": {"cmd": "run", "name": "每日签到"}}

Message another app#

Any JSON, interpreted by the app's app.on_message

JSON
{"data": {"cmd": "reset"}}

Reply#

Handed to the app

Successful reply; endpoints add their own fields

FieldTypeRequiredValuesRangeDescription
okbooleanYestrueAlways true

Error codes#

StatusCodeDescription
400bad_jsonThe body is not a JSON object, or data is missing
409no_app_runningNo app is running. POST /v1/apps/start first (the macro player is macro). See the example “Macro player: run the current macro” for the correct form
500msg_failedThe app's message queue is full (not queued within 100 ms). Try again later

For errors any endpoint can return, see Common errors.

GET /v1/apps/events#

Events emitted by apps (app.emit)

Signature required Kernel only

Ring of app.emit events keeping the latest 64, cleared on reboot. To read incrementally, pass the last reply's next as since. since defaults to 0, i.e. the oldest event in the ring

Parameters#

ParameterTypeRequiredValuesRangeDescription
sinceinteger≥ 0Start at this sequence number (inclusive); use the last reply's next. Defaults to 0
maxinteger0–64Max events, 1–64; 0 or omitted means 64

Reply#

Events

Events from app.emit

FieldTypeRequiredValuesRangeDescription
okbooleanYestrueAlways true
nextintegerYes≥ 0since for the next call
eventsobject[]YesIn sequence order
seqintegerYes≥ 0Sequence number from 0, reset on reboot
msintegerYes≥ 0Uptime in ms when emitted
appstringYesID of the emitting app
dataYesValue given to app.emit (any JSON, ≤ 4096 bytes). Macro player events are told apart by ev (docs/web.md section 7)

Error codes#

For errors any endpoint can return, see Common errors.

POST /v1/eval#

Run code in the app’s Lua state (can call hid; take care)

Signature required Kernel only Sends input

Tries code as an expression first (prefixed with return), then as statements. Runs in the running app's Lua state, so it can read and change the app's variables and call hid; with no app running, in a temporary state (only sys, log, json and the standard library). Errors and timeouts still return HTTP 200 with ok false. The body is at most 64 KB

Request body#

application/json

A piece of Lua code

FieldTypeRequiredValuesRangeDescription
codestringYesLua code
timeout_msintegerdefault 20000–10000Time limit in ms; 0 or omitted means 2000, at most 10000

Examples#

Check memory (expression)#

JSON
{"code": "sys.heap()"}

Change an app variable (statement)#

No return value, so result is null

JSON
{"code": "count = 0", "timeout_ms": 500}

Reply#

Result; ok is false on error

ok false means the code failed (still HTTP 200)

FieldTypeRequiredValuesRangeDescription
okbooleanYesWhether the code ran
resultReturn value as JSON (on success): several values become an array, none is null; functions and the like become description strings
errorstringlua_error timeout no_memFailure: lua_error = syntax or runtime error, timeout, no_mem = out of memory
messagestringError text (on failure)
printsstring[]Yes≤ 64 itemsOutput of print and log.* during the run, up to 64 lines

Error codes#

StatusCodeDescription
400bad_jsonThe body is not a JSON object, or code (a string) is missing. See the example “Check memory (expression)” for the correct form
500eval_failedThe Lua runtime is not ready, or memory ran out before a result

For errors any endpoint can return, see Common errors.

View the source of this page on GitHub