---
title: "How to control an iPhone or Android phone from your own code"
description: "Control an iPhone or Android phone from your own code with Lusor's direct-control API: read the element tree, tap and type, and chain up to 20 actions."
author: "Josiah Richards"
canonical: "https://lusor.ai/blog/drive-a-phone-from-your-own-code"
updated: "2026-11-26"
---

[Blog](https://lusor.ai/blog) / [Integrations](https://lusor.ai/blog/integrations)

# 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.

Direct control · November 24, 2026 · Updated November 26, 2026 · 11 min read

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](https://json-schema.org/specification) with it, as [the post on getting checked JSON from a phone screen](https://lusor.ai/blog/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](https://lusor.ai/blog/stop-copying-app-data-by-hand). An agent also ends a task with a claim that it finished, which [checking an agent that says it is done](https://lusor.ai/blog/your-agent-says-done-how-to-check) 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](https://lusor.ai/blog/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.

```bash
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](https://docs.lusor.ai/phones/overview) lists the other fields. The samples below keep the two ids in `WS` and `DEVICE` and read the screen like this.

```bash
curl https://api.lusor.ai/v1/workspaces/$WS/devices/$DEVICE/state \
  -H "X-Api-Key: $LUSOR_API_KEY"
```

- `a11yTree` is the element tree as text. Each interactive element carries a 1-based index such as `[12]`, which you can tap directly.
- `deviceContext` holds `screenWidth`, `screenHeight`, `interactiveCount` and `elementCount`.
- `phoneState` holds 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.

```bash
curl https://api.lusor.ai/v1/workspaces/$WS/devices/$DEVICE/screenshot \
  -H "X-Api-Key: $LUSOR_API_KEY" -o screen.png
```

A 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](https://lusor.ai/blog/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](https://docs.lusor.ai/phones/direct-control) 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 |

```bash
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 `index` from the element tree, or `x` and `y` in pixels. If you send both, `index` wins. Sending neither is a `400`, and so is `x` without `y`. The response's `resolved` field holds the point that was tapped. The [tap endpoint reference](https://docs.lusor.ai/api-reference/deviceverbs/tap-on-device) has the full schema.
- **Swipe.** `durationMs` is 1 to 60000.
- **Type.** `text` is 1 to 8192 characters, typed into the focused field. `clear: true` empties the field first.
- **Key.** `keyCode` is an Android key code. Common ones are 66 Enter, 67 Delete, 61 Tab, 111 Escape and 19 to 22 the arrow keys.
- **Apps.** `package` and the optional `activity` must be dotted identifiers such as `com.android.settings`. Anything else is a `400`.

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`](https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/) 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.

```bash
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.

```bash
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.

```bash
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" } }'
```

- `ui` includes the element tree. It defaults to `true`.
- `screenshot` includes `screenshot_url`, a link to `GET …/screenshot`.
- `verbosity` is `compact`, which drops indentation and non-interactive elements for far fewer tokens, `default`, the tree Lusor's own agent reads, or `full`, with every flag.
- `only` keeps only `clickable`, `focused` or `editable` elements. `all` keeps everything.
- `min_size_px` drops elements smaller than this many pixels, `include_keyboard` adds the on-screen keyboard's elements, and `max_depth` limits 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](https://docs.lusor.ai/phones/chains-and-capture) 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.

```bash
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`, `recent` and `screenshot` are the actions of the same name. `start_app` and `stop_app` are `apps/start` and `apps/stop`.
- `wait` pauses for `duration_ms`, 0 to 60000. Steps otherwise run back to back with no pause, so put a `wait` after a step that opens a new screen. All waits in one chain add up to at most 120 seconds.
- `capture` at the top level is `none` (the default), `each` or `final`. A step's own `capture` overrides 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 `ok` is `true` only if every step succeeded, so check `steps[n].ok` to 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 `400` a 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](https://docs.lusor.ai/help/connect-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](https://docs.lusor.ai/help/connect-your-android) 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](mailto:hello@lusor.ai?subject=Lusor%20Phone%20iPhone). 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](https://docs.lusor.ai/billing/credits-and-minutes) 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](https://lusor.ai/blog/is-it-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.

## About the author

**Josiah Richards**

- [LinkedIn](https://www.linkedin.com/in/illjosiah/)

## Keep reading

- [The best MCP servers for Android phones](https://lusor.ai/blog/best-mcp-servers-for-android-phones): A shortlist of nine MCP servers for Android phones. What each one shows the agent, its setup, where it runs and its license, from each project's own docs.
- [How to give your AI agent an Android phone: every option](https://lusor.ai/blog/give-your-ai-agent-an-android-phone): Every way to give an AI agent an Android phone: adb, Appium, mobile-mcp, phone-harness and Lusor. Setup, what the agent sees, cost, and when to pick each.
- [Introducing interact: continue a phone app task in plain words](https://lusor.ai/blog/introducing-interact-for-phone-apps): Lusor's POST /v1/interact/{id} continues a scrape or agent task on the screen it ended on: send a turn, read the output, stop, and handle a busy phone.
