Serve API
Optional /execute requests may include runId for caller-owned logging
correlation. It must contain 1 to 240 safe identifier characters (letters,
digits, dot, underscore, colon or hyphen); no prefix is required. Invalid values
return HTTP 400 with INVALID_RUN_ID. It does not replace execution commandId
or taskId.
Purpose
Define the local HTTP and SSE contract exposed by androperator serve, including request bodies, success responses, status-code mapping, and how the serve layer wraps runExecution and emulator operations.
Choose the transport
Use the CLI when the agent or test harness can launch shell commands on the host
with adb and the Android SDK. Use stdio MCP when the client supports
local MCP processes. Use HTTP when the caller needs access to a separate
adb/emulator host and cannot launch CLI processes there. Run androperator serve
on that device host; device identifiers and filesystem paths refer to that host.
The agent still owns planning, app strategy and verification.
Sources
- HTTP server and handlers:
apps/node/src/cli/commands/serve.ts - Execution result contract:
apps/node/src/domain/executions/runExecution.ts - Result envelope source:
apps/node/src/contracts/result.ts; canonical docs: Result Envelope - SSE event names:
apps/node/src/domain/observe/events.ts - Emulator response types:
apps/node/src/domain/android-emulators/types.ts
Start The Server
androperator serve [--host <string>] [--port <number>]
Defaults:
| Field | Value |
|---|---|
| host | 127.0.0.1 |
| port | 3000 |
| JSON request body limit | 100kb |
When the server starts successfully, it listens until the process exits. There is no structured JSON startup response because this is a long-running command.
The CLI binds to loopback by default. The server has no built-in authentication
or authorization. To accept remote connections, deliberately select a reachable
interface with --host and provide access control outside Androperator, such as
an authenticated tunnel or gateway. Binding to 0.0.0.0 exposes the API on all
IPv4 interfaces; it does not add authentication.
Completion and concurrency
POST /execute, /snapshot and /screenshot await runExecution() and return
its outcome in the HTTP response. SSE is optional observation; the caller does
not need an SSE subscription to receive completion. An HTTP or SSE disconnect
does not cancel an execution: these handlers do not pass a disconnect-driven
abort signal. If the response is lost, inspect device state before repeating an
action whose effects are uncertain.
Execution exclusion is per device within one Node process. After preflight,
runExecution() acquires an in-memory slot and releases it when execution
finishes. A different commandId for an occupied device returns HTTP 423 with
EXECUTION_CONFLICT_IN_FLIGHT. Use a unique commandId for each execution:
the store permits re-entry with the same ID, so duplicate IDs are not a safe
retry or deduplication mechanism. The slot is neither a queue nor a lease across
an agent's multi-request workflow, and it does not coordinate separate processes
or emulator lifecycle routes.
The daemon uses the same app on a Unix socket. CLI calls routed to
one daemon share that process's execution slot and emitter. A separately started
TCP serve process, another daemon, or a direct CLI execution has separate
in-memory state. Coordinate competing agents outside this API.
Response Shapes
Most REST endpoints return one of these shapes.
Important boundary:
/execute,/snapshot, and/screenshotpass throughrunExecution()results on success and on cross-surface execution failures; readenvelopefields through the canonical Result Envelope contract- malformed request bodies and route-local validation failures are serve-layer wrappers only and are not part of the shared execution contract
Success wrapper
{
"ok": true
}
and then endpoint-specific fields such as devices, avds, output, or emulator state.
Execution result passthrough
/execute, /snapshot, and /screenshot return the runExecution() result object directly:
Successful shape:
{
"ok": true,
"deviceId": "emulator-5554",
"terminalSource": "androperator_result",
"envelope": {
"commandId": "serve-snap-1710000000000",
"taskId": "serve-snap-1710000000000",
"status": "success",
"stepResults": [
{
"id": "snap",
"actionType": "snapshot",
"success": true,
"data": {
"text": "<hierarchy rotation=\"0\">...</hierarchy>"
}
}
],
"error": null
}
}
Failure shape:
{
"ok": false,
"error": {
"code": "DEVICE_NOT_FOUND",
"message": "Device emulator-9999 not found or not in device state",
"details": {
"connected": ["emulator-5554"]
}
}
}
Success conditions for execution endpoints:
- HTTP status is
200 - response body has
"ok": true envelope.status == "success"- every
envelope.stepResults[i].success == true
Endpoint Summary
| Method | Path | Purpose |
|---|---|---|
GET |
/ping |
health probe for the local serve process |
GET |
/version |
report the CLI version and build identity |
GET |
/devices |
list adb-visible devices |
POST |
/execute |
run a caller-supplied execution payload |
POST |
/snapshot |
run a synthetic one-step snapshot execution |
POST |
/screenshot |
run a synthetic one-step take_screenshot execution |
GET |
/android/emulators |
list configured AVDs |
GET |
/android/emulators/running |
list running emulators |
GET |
/android/emulators/:name |
inspect one configured AVD |
POST |
/android/emulators/create |
create an AVD |
POST |
/android/emulators/:name/start |
start an AVD and wait for boot |
POST |
/android/emulators/:name/stop |
stop a running AVD |
DELETE |
/android/emulators/:name |
delete an AVD |
POST |
/android/provision/emulator |
create or reuse a supported emulator and boot it |
GET |
/events |
subscribe to SSE execution events |
GET /ping
Health probe for the serve process.
Response:
{
"ok": true
}
Meaning:
- the Express app is running and can answer requests
- no adb, device, Operator APK, or execution readiness check is performed
GET /version
Reports the CLI version and build identity for the running server process.
Response:
{
"version": "0.9.4",
"buildIdentity": {
"entryPath": "/path/to/androperator/dist/cli/index.js",
"mtimeMs": 1710000000000,
"size": 123456
}
}
Meaning:
versionis the same package version returned by the CLI version helperbuildIdentity.entryPathis the resolved CLI entrypoint path when Node can resolve itbuildIdentity.mtimeMsandbuildIdentity.sizecome fromstatSync()and arenullwhen the entrypoint cannot be inspected- no device or Operator APK check is performed
GET /devices
Returns the same parsed adb listing used by Devices.
Success response:
{
"ok": true,
"devices": [
{
"serial": "emulator-5554",
"state": "device"
},
{
"serial": "R58N12345AB",
"state": "unauthorized"
}
]
}
Meaning:
- this is observational output only
- it does not apply the execution-time
resolveDevice()filtering rules
Failure behavior:
- server-side listing failures return HTTP
500 - this route does not use the
errors.tsstatus mapping table
POST /execute
Request body
{
"execution": {
"commandId": "open-settings",
"taskId": "open-settings",
"source": "agent-http",
"expectedFormat": "android-ui-automator",
"timeoutMs": 30000,
"actions": [
{
"id": "a1",
"type": "open_app",
"params": {
"applicationId": "com.android.settings"
}
},
{
"id": "a2",
"type": "snapshot"
}
]
},
"deviceId": "emulator-5554",
"operatorPackage": "com.androperator.operator.dev"
}
Valid body rules enforced by the route:
- request body must be a JSON object
executionis requireddeviceId, when present, must be a stringoperatorPackage, when present, must be a non-empty string
Operator package resolution:
- if
operatorPackageis present in the request, the server uses it verbatim - otherwise it uses the server startup
--operator-packageoption when supplied - otherwise it falls back to
process.env.ANDROPERATOR_OPERATOR_PACKAGEwhen that env var is non-blank - otherwise it uses
com.androperator.operator
Then runExecution() applies full execution validation. See Actions, Selectors, and API Overview.
params.template carries live overlay templates unchanged to Android, using the
same validation and styling as literal params.text. Exactly one is required.
show_toast and cancel_toast also use /execute, without separate toast routes.
See Toast actions for parameters and submission semantics.
/execute is also the generic HTTP transport for set_on_screen_log and
clear_on_screen_log. Inside execution.actions, it accepts exact lower-case
on_screen_log_set and on_screen_log_clear input aliases and normalizes them
to the canonical action types. It does not provide separate panel endpoints,
accept case or whitespace variants, or translate on-screen log parameter
aliases. See On-screen logs for the raw payload contract
and result fields. The on-screen-log set and clear CLI commands are
conveniences over these same actions, not additional HTTP routes.
Representative serve-layer 400 wrappers for this route:
{
"ok": false,
"error": {
"code": "MISSING_EXECUTION",
"message": "Missing 'execution' in body"
}
}
Success response
{
"ok": true,
"deviceId": "emulator-5554",
"terminalSource": "androperator_result",
"envelope": {
"commandId": "open-settings",
"taskId": "open-settings",
"status": "success",
"stepResults": [
{
"id": "a1",
"actionType": "open_app",
"success": true,
"data": {}
},
{
"id": "a2",
"actionType": "snapshot",
"success": true,
"data": {
"text": "<hierarchy rotation=\"0\">...</hierarchy>"
}
}
],
"error": null
}
}
Failure response
Validation failure example:
{
"ok": false,
"error": {
"code": "EXECUTION_VALIDATION_FAILED",
"message": "press_key requires params.key",
"details": {
"path": "actions.0.params.key",
"actionId": "k1",
"actionType": "press_key"
}
}
}
Device-resolution failure example:
{
"ok": false,
"error": {
"code": "DEVICE_NOT_FOUND",
"message": "Device non-existent not found or not in device state",
"details": {
"connected": ["emulator-5554"]
}
}
}
POST /snapshot
This route builds a synthetic execution with:
source: "serve-api"expectedFormat: "android-ui-automator"timeoutMs: 30000- one action:
{ "id": "snap", "type": "snapshot" }
Request body
{
"deviceId": "emulator-5554",
"operatorPackage": "com.androperator.operator.dev"
}
Notes:
- body must still be a JSON object, but
{}is valid - omitted
operatorPackagefollows the same fallback chain as/execute
Success response
{
"ok": true,
"deviceId": "emulator-5554",
"terminalSource": "androperator_result",
"envelope": {
"commandId": "serve-snap-1710000000000",
"taskId": "serve-snap-1710000000000",
"status": "success",
"stepResults": [
{
"id": "snap",
"actionType": "snapshot",
"success": true,
"data": {
"text": "<hierarchy rotation=\"0\">...</hierarchy>"
}
}
],
"error": null
}
}
POST /screenshot
This route builds a synthetic execution with:
source: "serve-api"expectedFormat: "android-ui-automator"timeoutMs: 30000- one action:
{ "id": "shot", "type": "take_screenshot" } - optional
params.pathandparams.scalefrom the suppliedpathandscalefields
Request body
{
"deviceId": "emulator-5554",
"operatorPackage": "com.androperator.operator.dev",
"path": "/tmp/settings.png"
}
Route validation:
- request body must be a JSON object
path, when present, must be a non-empty stringscale, when present, must be the JSON number25,50, or100; strings are rejected with HTTP400andINVALID_SCALE- omitted
scaleselects 25% per dimension; send"scale": 100for full resolution - omitted
operatorPackagefollows the same fallback chain as/execute
Artifact locality
Node captures PNG bytes through adb and writes the file on the server host.
An omitted path creates a file in that process's operating-system temporary
directory. A relative path resolves from the server process's working directory;
an absolute path addresses its filesystem. The successful step returns data.path
and capture metadata, not image bytes or a download URL. There is no screenshot
file-download route. Remote callers must arrange file transfer separately.
Success response
Synthetic, abridged resize-fallback example. Step data values are strings; see the screenshot contract for helper-specific correlation fields and detail-recovery guidance.
{
"ok": true,
"deviceId": "emulator-5554",
"terminalSource": "androperator_result",
"envelope": {
"commandId": "serve-shot-1710000000000",
"taskId": "serve-shot-1710000000000",
"status": "success",
"stepResults": [
{
"id": "shot",
"actionType": "take_screenshot",
"success": true,
"data": {
"path": "/tmp/settings.png",
"captureSource": "host",
"persistedAt": "2026-10-11T00:00:00.000Z",
"captureWidthPx": "270",
"captureHeightPx": "600",
"nativeWidthPx": "1080",
"nativeHeightPx": "2400",
"coordinateSpace": "screenshot_pixels",
"origin": "top_left",
"captureMethod": "adb_screencap_resize",
"requestedScale": "25",
"appliedScale": "25",
"protectedContent": "unknown",
"fallbackAttempted": "true",
"fallbackReason": "unavailable"
}
}
],
"error": null
}
}
Error Layers
Serve has three distinct error layers. Keep them separate when recovering from a failure.
| Layer | Where it comes from | Shape | Recovery |
|---|---|---|---|
| Route-local wrapper error | Express JSON parsing or per-route request checks in serve.ts |
{ "ok": false, "error": { "code": "INVALID_BODY", ... } } or similar route-local codes |
Fix the HTTP request shape and retry. These codes are not the shared execution contract. |
| Shared execution error | runExecution() returns ok: false for /execute, /snapshot, or /screenshot |
{ "ok": false, "error": { "code": "<errors.ts code>", ... } } |
Branch on error.code and use Errors for recovery. |
| Failed result envelope | Android returned an execution envelope with envelope.status == "failed" |
{ "ok": true, "envelope": { "status": "failed", ... } } in success-wrapper passthrough cases |
Read Result Envelope, then branch on envelope.errorCode or failed stepResults[].data.error. |
Machine-checkable rule:
- first check HTTP status and top-level
ok - for execution endpoints, also inspect
envelope.statuswhen an envelope is present - do not treat route-local codes such as
INVALID_BODY,INVALID_DEVICE_ID, orMISSING_EXECUTIONas publicerrors.tscodes
Global Serve-Layer Failures
These wrappers come from Express middleware rather than a specific endpoint handler:
| HTTP status | Code | When it appears |
|---|---|---|
400 |
INVALID_JSON |
request body is malformed JSON |
413 |
PAYLOAD_TOO_LARGE |
request body exceeds the 100kb Express limit |
500 |
INTERNAL_SERVER_ERROR |
unhandled server-side exception reached the catch-all middleware |
Many individual route handlers also return INTERNAL_ERROR from local catch blocks. Treat both INTERNAL_ERROR and INTERNAL_SERVER_ERROR as host-side 500 failures rather than as stable execution-contract codes.
Emulator Endpoints
GET /android/emulators
Lists configured AVDs, merged with running-state information:
{
"ok": true,
"avds": [
{
"name": "androperator-pixel",
"exists": true,
"running": false,
"apiLevel": 35,
"abi": "arm64-v8a",
"playStore": true,
"deviceProfile": "pixel_8",
"systemImage": "system-images;android-35;google_apis_playstore;arm64-v8a",
"supported": true,
"unsupportedReasons": []
}
]
}
GET /android/emulators/running
{
"ok": true,
"devices": [
{
"type": "emulator",
"avdName": "androperator-pixel",
"serial": "emulator-5554",
"booted": true,
"supported": true,
"unsupportedReasons": []
}
]
}
GET /android/emulators/:name
Returns one ConfiguredAvd object merged into the success wrapper:
{
"ok": true,
"name": "androperator-pixel",
"exists": true,
"running": false,
"apiLevel": 35,
"abi": "arm64-v8a",
"playStore": true,
"deviceProfile": "pixel_8",
"systemImage": "system-images;android-35;google_apis_playstore;arm64-v8a",
"supported": true,
"unsupportedReasons": []
}
POST /android/emulators/create
Request body:
{
"apiLevel": 35,
"abi": "arm64-v8a",
"deviceProfile": "pixel_8",
"playStore": true,
"storageSize": "12G"
}
Defaults when omitted:
| Field | Default |
|---|---|
name |
derived from storage size, for example androperator-pixel-12gb |
apiLevel |
SUPPORTED_EMULATOR_API_LEVEL (35) |
abi |
arm64-v8a |
deviceProfile |
DEFAULT_EMULATOR_DEVICE_PROFILE (pixel_7) |
playStore |
true unless explicitly false |
storageSize |
12G |
storageSize accepts positive integer gigabyte values such as 12G, 12GB,
or 16G. The aliases size, diskSize, and dataPartitionSize are also
accepted. Only one storage size field may be provided.
When name is omitted, the server derives the AVD name from the normalized
storage size. For example, 12G and 12GB both default to
androperator-pixel-12gb.
Success response:
{
"ok": true,
"name": "androperator-pixel-12gb",
"exists": true,
"running": false,
"apiLevel": 35,
"abi": "arm64-v8a",
"playStore": true,
"deviceProfile": "pixel_8",
"systemImage": "system-images;android-35;google_apis_playstore;arm64-v8a",
"supported": true,
"unsupportedReasons": []
}
POST /android/emulators/:name/start
Success response:
{
"ok": true,
"type": "emulator",
"avdName": "androperator-pixel",
"serial": "emulator-5554",
"booted": true
}
Behavior:
- verifies the AVD exists
- rejects already-running AVDs
- starts the emulator, waits for adb registration, waits for boot completion, then enables developer settings
POST /android/emulators/:name/stop
{
"ok": true,
"avdName": "androperator-pixel",
"stopped": true
}
DELETE /android/emulators/:name
{
"ok": true,
"avdName": "androperator-pixel",
"deleted": true
}
POST /android/provision/emulator
This route calls provisionEmulator() and may reuse a supported running emulator, start an existing supported AVD, or create and start a new one.
Optional request body:
{
"storageSize": "16GB"
}
When a new AVD is created, storageSize controls the internal app storage data
partition. It accepts the same gigabyte-only values and aliases as
POST /android/emulators/create. Omit it to use 12G.
When a new AVD is created and no name is provided by the caller, provisioning
uses the same storage-derived default name, such as androperator-pixel-12gb
for the default 12G size.
Success response:
{
"ok": true,
"type": "emulator",
"avdName": "androperator-pixel",
"serial": "emulator-5554",
"booted": true,
"created": false,
"started": true,
"reused": true
}
Meaning of flags:
created: a new AVD had to be createdstarted: the emulator process was started during this requestreused: an existing supported emulator or AVD was reused
GET /events SSE Stream
The server responds with:
Content-Type: text/event-streamCache-Control: no-cacheConnection: keep-alive
One initial heartbeat event (there is no periodic heartbeat):
event: heartbeat
data: {"code":"CONNECTED","message":"Androperator SSE stream active"}
Execution-related events:
| Event name | Data shape |
|---|---|
androperator:result |
{ "deviceId": "<serial>", "envelope": <ResultEnvelope> } |
androperator:execution |
{ "deviceId": "<serial>", "input": <unknown>, "result": <RunExecutionResult> } |
Host transport failures appear in androperator:execution with result.ok: false
and structured error diagnostics. They do not emit a synthetic
androperator:result envelope. Execution events cover outcomes from runExecution() in this process when a
device ID is available, including failures before a terminal Android result.
Failures without a resolved or supplied device ID, route-local validation errors
and emulator lifecycle operations do not emit these execution events.
The emitter is process-local, not an adb-wide feed. It does not observe unrelated
CLI processes or a separate daemon. All subscribers receive the same events;
there is no device/task subscription filter. Match deviceId, execution
commandId and taskId in the payload yourself. runId is logging correlation,
not an SSE subscription key.
There are no event IDs, replay storage or Last-Event-ID recovery. Reconnecting
can miss events emitted while disconnected. If you need live observation,
connect and receive the CONNECTED heartbeat before dispatching; use the HTTP
response as the request's completion result rather than waiting only for an
androperator:result event, which is absent for some failures.
Use /events when:
- you want push-style result observation instead of polling
- you need both raw execution outcomes and envelope-only results
HTTP Status Mapping
When a handler returns an enum-backed error from errors.ts, serve.ts maps it to HTTP status like this:
| Error code | HTTP status |
|---|---|
EXECUTION_CONFLICT_IN_FLIGHT |
423 |
DEVICE_NOT_FOUND |
404 |
NO_DEVICES |
404 |
MULTIPLE_DEVICES_DEVICE_ID_REQUIRED |
400 |
EXECUTION_VALIDATION_FAILED |
400 |
PAYLOAD_TOO_LARGE |
413 |
RESULT_ENVELOPE_TIMEOUT |
504 |
DEVICE_NOT_INTERACTIVE |
409 |
DEVICE_ACCESSIBILITY_NOT_RUNNING |
409 |
DEVICE_SHELL_UNAVAILABLE |
503 |
EMULATOR_NOT_FOUND |
404 |
EMULATOR_NOT_RUNNING |
404 |
EMULATOR_UNSUPPORTED |
409 |
EMULATOR_ALREADY_RUNNING |
409 |
| anything else | 500 |
Machine-checkable rule:
- for execution endpoints, use both HTTP status and
error.code - branch primarily on
error.code, not only on the HTTP status
Error Handling Notes
Two classes of failures exist:
- Stable enum-backed failures from
apps/node/src/contracts/errors.ts - Route-local HTTP validation failures for malformed or missing request bodies
For long-term agent logic, prefer branching on the enum-backed errors above. Route-local validation failures should be treated as “fix the request body and retry” rather than as durable cross-surface contract codes.
Examples of route-local validation failures:
- malformed JSON body -> HTTP
400 - body is missing or not a JSON object on POST routes -> HTTP
400 /executewithoutexecution-> HTTP400/screenshotwith blankpath-> HTTP400