Direct controlUpdated 11 min read
How to control an iPhone or Android phone from your own code
Some phone jobs need no agent. Here is how your own code reads a phone's screen as an element tree, taps, types and swipes through Lusor's direct-control API, gets the new screen back in the same response, and runs up to 20 actions in one request.
By Josiah Richards
TL;DR
- Lusor's direct-control API lets your code operate a phone one action at a time with no Lusor agent involved. Each action runs once, as sent, and returns when the phone has carried it out.
- GET /state returns the screen as an element tree in which each interactive element has a 1-based index. A tap by index is resolved against a fresh read of the screen when the tap runs.
- Add capture to any action and the response carries the screen as it is right after the action, so each step of your loop is one request.
- POST /chain runs 1 to 20 actions in order. A step that completes with ok: false lets the chain continue. A step that errors stops it, and the steps before it have already run.
- Your own iPhone and Android never use Lusor Phone minutes. On a Lusor Phone, direct control with an API key uses them, reads included, and a refused action is never sent to the phone.
Some phone jobs need no agent to decide what to tap. A test walks the same sign-up flow on every build. A script reads one value from an app each morning. An agent loop you wrote runs on your own model and picks its own steps. Each of these needs the screen as data and a way to act on it.
Lusor's direct-control API gives your code those two things and leaves every decision to it. This post goes through it with curl, from the first screen read to a chain of 20 actions, and ends with what changes between your own iPhone, your own Android and a Lusor Phone.
When do you drive a phone yourself instead of running an agent?
When your code already knows the next step, or wants to choose it. Direct control runs each action once, as sent, and returns as soon as the phone has carried it out. No Lusor agent is involved, so no model sits between your request and the phone.
| Direct control | Agent run | |
|---|---|---|
| Who picks each step | Your code | Lusor's agent |
| One request carries | One action, or a chain of up to 20 | A task in plain language |
| What comes back | ok, exit_code, status, and the screen if you ask | A session you follow through its events |
An agent run fits when the steps depend on what the app shows, and writing every branch would take longer than the job. When that run has to return data your code can store, send a JSON Schema with it, as the post on getting checked JSON from a phone screen shows. For app data that should land in a spreadsheet, read how an agent copies app data into a Sheet. An agent also ends a task with a claim that it finished, which checking an agent that says it is done covers. A direct action has no such claim. Its own ok field says whether the phone carried it out.
AI apps get both paths. Lusor's MCP server for real phones gives them phone_action for one exact action and run_task for a whole job, and each tool calls the same Lusor API with the connection's own credential.
How do you read the screen as an element tree?
With GET …/state on the phone. Every direct-control path sits under the phone, at https://api.lusor.ai/v1/workspaces/{workspaceId}/devices/{deviceId}/. The workspace id is the workspaceId field of any agent from GET /v1/agents. Phone ids come from GET /v1/devices, which lists every phone in the API key's workspace, offline ones included.
curl https://api.lusor.ai/v1/devices \
-H "X-Api-Key: $LUSOR_API_KEY"Each phone in the list has an id, a name and connected, which says whether it is connected to Lusor right now. Actions on a disconnected phone fail. The phones overview in the Lusor docs lists the other fields. The samples below keep the two ids in WS and DEVICE and read the screen like this.
curl https://api.lusor.ai/v1/workspaces/$WS/devices/$DEVICE/state \
-H "X-Api-Key: $LUSOR_API_KEY"a11yTreeis the element tree as text. Each interactive element carries a 1-based index such as[12], which you can tap directly.deviceContextholdsscreenWidth,screenHeight,interactiveCountandelementCount.phoneStateholds extra details the phone reports about its current state.
For an image instead, GET …/screenshot takes a new screenshot and returns it as PNG bytes.
curl https://api.lusor.ai/v1/workspaces/$WS/devices/$DEVICE/screenshot \
-H "X-Api-Key: $LUSOR_API_KEY" -o screen.pngA model that reads the tree picks an element by its number instead of working out coordinates from pixels. Lusor's own agent reads the screen the same way, as the post on what makes a phone agent fast explains.
An index is a position on the current screen, not a lasting id. When a tap names an index, Lusor reads the screen again at that moment and taps the element that holds that number now. If the screen changed after your read, for example because a list scrolled or a dialog opened, the same number can belong to another element. An index the screen does not have returns 400 with a message to read /state again.
How do you tap by index or coordinate, type and swipe?
Each action is a POST under the same phone path, with a small JSON body. The direct control docs have the same table with Python and Node.js samples.
| Endpoint | Body | |
|---|---|---|
| Tap | POST …/tap | { "index": 12 } or { "x": 540, "y": 1200 } |
| Swipe | POST …/swipe | { "startX": 540, "startY": 1800, "endX": 540, "endY": 600, "durationMs": 300 } |
| Type into the focused field | POST …/type | { "text": "hello", "clear": true } |
| Press a key | POST …/key | { "keyCode": 66 } |
| Clear the focused field | POST …/clear | none |
| Back, Home, Recent apps | POST …/back, …/home, …/recent | none |
| Launch an app | POST …/apps/start | { "package": "com.android.settings" } |
| Force-stop an app | POST …/apps/stop | { "package": "com.android.settings" } |
| Take a screenshot | POST …/screenshot | none |
curl https://api.lusor.ai/v1/workspaces/$WS/devices/$DEVICE/tap \
-H "X-Api-Key: $LUSOR_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "index": 12 }'- Tap. Send
indexfrom the element tree, orxandyin pixels. If you send both,indexwins. Sending neither is a400, and so isxwithouty. The response'sresolvedfield holds the point that was tapped. The tap endpoint reference has the full schema. - Swipe.
durationMsis 1 to 60000. - Type.
textis 1 to 8192 characters, typed into the focused field.clear: trueempties the field first. - Key.
keyCodeis an Android key code. Common ones are 66 Enter, 67 Delete, 61 Tab, 111 Escape and 19 to 22 the arrow keys. - Apps.
packageand the optionalactivitymust be dotted identifiers such ascom.android.settings. Anything else is a400.
Every action returns ok, exit_code (0 on success) and status. ok: false means the phone could not carry the action out. A body that fails validation returns 400 with error: "validation_error" and per-field details.
tap, swipe and type also take stealth: true, which holds the press, curves the swipe and spaces out the keys the way a person would. A phone that cannot do that answers 412 unsupported_stealth_input. Lusor never sends the action as plain input instead.
An action is not undone or deduplicated on its own, so resending a tap that timed out may tap twice. Send an Idempotency-Key header with each action and resend with the same key. A repeat returns the first result with Idempotent-Replayed: true and does not act again. A new key acts again.
KEY=$(uuidgen)
curl https://api.lusor.ai/v1/workspaces/$WS/devices/$DEVICE/type \
-H "X-Api-Key: $LUSOR_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $KEY" \
-d '{ "text": "hello", "clear": true }'Without a key, read the screen before you retry.
How do you get the screen back with the action?
Add capture to the action. The response then carries the screen as it is right after the action, so the next decision needs no separate /state call. Each step of your loop becomes one request. Send an action and get back the screen that decides the next one.
curl https://api.lusor.ai/v1/workspaces/$WS/devices/$DEVICE/tap \
-H "X-Api-Key: $LUSOR_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "index": 12, "capture": true }'capture: true returns the element tree with default options. An object changes them, as in this tap.
curl https://api.lusor.ai/v1/workspaces/$WS/devices/$DEVICE/tap \
-H "X-Api-Key: $LUSOR_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "index": 12, "capture": { "verbosity": "compact", "only": "clickable" } }'uiincludes the element tree. It defaults totrue.screenshotincludesscreenshot_url, a link toGET …/screenshot.verbosityiscompact, which drops indentation and non-interactive elements for far fewer tokens,default, the tree Lusor's own agent reads, orfull, with every flag.onlykeeps onlyclickable,focusedoreditableelements.allkeeps everything.min_size_pxdrops elements smaller than this many pixels,include_keyboardadds the on-screen keyboard's elements, andmax_depthlimits the tree to 1 to 50 levels.
The response's capture object holds elements, the tree with its [N] indices, plus element_count and screen_dimensions. With screenshot on, it also holds screenshot_url.
Read the numbers you plan to tap from the right tree. The only filter runs before the numbering, and a tap resolves its index against the full tree. compact and only: "clickable" keep every interactive element in order, so their numbers match. only: "editable" and only: "focused" drop interactive elements first, so a number from those trees can belong to a different element when you tap it.
screenshot_url takes a new screenshot each time you fetch it. It shows the screen at fetch time, not the moment after the action, so fetch it before the next action. It needs the same API key.
The action has already run when the capture is taken. If the action succeeds and the capture fails, the response is still 200, with capture.failed set to true and an error. Don't resend the action, because it ran. Read the screen with GET …/state instead. The chains and capture docs list every option and field.
How does a chain of up to 20 actions run, and what happens when a step fails?
POST …/chain takes 1 to 20 steps and runs them in order in one request. Each step is a verb with the same params as the body of the matching action endpoint, and an optional capture of its own. This chain opens Settings, waits, taps the fourth interactive element, types a search and presses Enter.
curl https://api.lusor.ai/v1/workspaces/$WS/devices/$DEVICE/chain \
-H "X-Api-Key: $LUSOR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"capture": "final",
"steps": [
{ "verb": "start_app", "params": { "package": "com.android.settings" } },
{ "verb": "wait", "params": { "duration_ms": 1500 } },
{ "verb": "tap", "params": { "index": 4 } },
{ "verb": "type", "params": { "text": "battery" } },
{ "verb": "key", "params": { "keyCode": 66 } }
]
}'tap,swipe,type,key,clear,back,home,recentandscreenshotare the actions of the same name.start_appandstop_appareapps/startandapps/stop.waitpauses forduration_ms, 0 to 60000. Steps otherwise run back to back with no pause, so put awaitafter a step that opens a new screen. All waits in one chain add up to at most 120 seconds.captureat the top level isnone(the default),eachorfinal. A step's owncaptureoverrides it.
A tap by index inside a chain is resolved when its step runs, against the screen the earlier steps left. In the example, index 4 is counted on the Settings screen after the wait, not on whatever screen you read before you sent the chain. That is why the wait comes first.
The response has a top-level ok and one entry in steps per step, in order. Each entry has the step's verb, ok, exit_code and status, plus resolved for a tap, and cleared and capture when they apply. A step can fail in two ways, and they behave differently.
- A step completes with ok: false. The chain keeps going. The top-level
okistrueonly if every step succeeded, so checksteps[n].okto find the one that failed. - A step errors. The chain stops. That happens when the phone cannot be reached, when a step's capture fails, or when billing refuses a later step on a Lusor Phone. A tap whose index is not on the screen when its step runs stops the chain too, with the same
400a single tap returns. The response is that error alone, with no per-step results, and the steps before it have already run on the phone.
After an error, the response does not say how far the chain got, so read the screen before you retry. The Idempotency-Key header works on a chain too. Once the first step has been sent, Lusor stores the outcome under the key, error included, so resending the same chain with the same key returns that outcome and runs no step again.
What differs between your own iPhone, your own Android and a Lusor Phone?
The paths and request bodies are the same on all three. What differs is the tree, which actions apply, and whether time on the phone is metered.
| Your own iPhone | Your own Android | Lusor Phone | |
|---|---|---|---|
| Joins the workspace through | The Lusor Mac app | The Lusor Launcher app | Lusor runs it in the cloud |
| Element tree | iOS, frames in points | Android | Android |
| Capture verbosity and only | Not applied | Applied | Applied |
| Lusor Phone minutes | Never | Never | With an API key |
Your own iPhone. GET …/state returns the iOS accessibility tree. Each frame is written {{x, y}, {width, height}} in points, and [N] taps the centre of that frame. A capture returns the same tree as /state, whatever verbosity and only say. Key codes and app package names are Android ones, so key and apps/start are for Android phones. The guide to connecting your iPhone covers setup with the Lusor Mac app.
Your own Android. The phone joins through the Lusor Launcher app, as the guide to connecting your Android phone shows. It returns the Android tree, takes every action in the table above, and never uses Lusor Phone minutes.
A Lusor Phone. This is a cloud phone that Lusor runs for you, with its own Internet connection. The table shows an Android Lusor Phone; an iPhone Lusor Phone is available through Contact sales. Direct control of it with an API key uses Lusor Phone minutes, the same pool as agent runs. Reads count too. GET …/state and GET …/screenshot are metered like actions, and a chain is metered before each step. A repeat with the same Idempotency-Key uses no minutes, because it does not act again. The credits and minutes page explains how minutes and overages work. When the phone cannot be used, Lusor refuses the action and does not send it.
402 credits_exhausted: the workspace is out of credits for Lusor Phone minutes, or reached its plan's allowance with overages off.402 billing_required: the phone is not covered by an active paid plan.409 internet_route_inactive: the phone's Internet connection is not active yet. Retry once it is.503 meter_unavailable: metering could not start. Nothing was sent, so retry shortly.
The same API serves an agent when you want one later, on any of the three phones. What that agent can see and do on a phone you own is the subject of whether it is safe to let an agent use your phone.
Frequently asked questions
How do you control a phone from code without an agent?
Use Lusor's direct-control endpoints, which sit under each phone's own path in the API. GET /state reads the screen as an element tree, and POST /tap, /swipe, /type, /key, /back, /home, /recent and /apps/start act on it. Each action runs once, as sent, with no Lusor agent involved.
What does tapping by index mean in Lusor's API?
Every interactive element in the a11yTree from GET /state carries a 1-based index such as [12], and POST /tap with { "index": 12 } taps it. Lusor reads the screen again when the tap runs, so the index refers to the screen at that moment. An index the screen does not have returns 400 with a message to read /state again.
How do you get the screen back after a tap without a second request?
Add capture to the action, for example { "index": 12, "capture": true }. The response then has a capture object with the element tree, its element count and the screen dimensions, taken right after the action. If only the capture fails, the response is still 200 with capture.failed, and the action should not be resent.
How many actions can one Lusor chain run?
POST /chain runs 1 to 20 steps in order. Steps run back to back with no pause, a wait step pauses for 0 to 60000 ms, and all waits in one chain add up to at most 120 seconds. Capture can be none, each or final, and any step can set its own.
What happens when a step in a chain fails?
A step that completes with ok: false does not stop the chain, and the top-level ok is true only if every step succeeded. A step that errors stops the chain. That happens when the phone cannot be reached, a step's capture fails, a tap index is not on screen, or billing refuses a later step on a Lusor Phone. The response is then that error alone, and the earlier steps have already run, so read the screen before you retry.
Does direct control use Lusor Phone minutes?
Only on a Lusor Phone, when you drive it with an API key. Reads count as well as actions, and a chain is metered before each step. Your own iPhone and your own Android never use Lusor Phone minutes. When a Lusor Phone cannot be used, the action is refused with 402, 409 or 503 and is not sent to the phone.