# Device sessions

Device sessions are opened by the operator. While the operator keeps them closed, each session route below answers 503 `device_sessions_off` before it reads anything, nothing is charged, and a product is asked for with `POST /app/api/demand/device` and its kind, `emulator` or `android`.

## What a session is

A device session is one device that an agent drives through an MCP endpoint of its own. Its tools are `snapshot`, which reads the screen, and `act`, which taps, types, presses a key, swipes, opens a web address or waits. Nothing on the device is spoofed: it reports what it is.

- `emulator`: a virtual Android phone. It says it is an emulator, and sites and apps can tell that it is one.
- `android`: a physical Android phone on a carrier SIM. Its traffic leaves through that SIM, apps can tell that USB debugging is on, and its model and serial number are the same for every renter.

`reports` in a running session is what the device says of itself: its model, manufacturer, release, build type and security patch, as the device reports them.

## Quote, confirm, start

Every call here takes the session cookie of a signed-in account, as the [quickstart](/guides/agents.md) gets one with `POST /app/api/login`, and refuses a bearer with `session_required`.

1. Ask for a quote. It reserves nothing.

```
POST /app/api/sessions/quote
Content-Type: application/json

{"product": "emulator", "minutes": 30}
```

The answer is `{"quote": {"product", "minutes", "unit", "unitUsdMicros", "usdMicros"}, "onRequest": 0, "limits"}`. `unitUsdMicros` is the price of one minute and `usdMicros` the total, both in micro-USD as strings of digits. `limits` holds the account's bounds: `limits.minutesMost` for one session, `limits.concurrentMost` at once and `limits.startsPerDayMost` started in a day.

2. Show the total to the person in charge and wait for their agreement. Money moves only on a start that carries `"confirm": true` and the quoted `unitUsdMicros` as `expectedUnitUsdMicros`. A typed sentence, a string or a number in `confirm` is refused with `invalid`, and false with `confirmation_required`.

3. Start the session from the paid balance. Granted credit does not buy a session.

```
POST /app/api/sessions
Content-Type: application/json

{"product": "emulator", "minutes": 30, "expectedUnitUsdMicros": "<unitUsdMicros of the quote>", "requestId": "<your own id for this start>", "confirm": true}
```

The answer is `{"session": S, "token": "st_...", "onRequest": 0}`. The session is `starting` until its device is ready, then `running`. `exit` may name one of the account's own named identities (`POST /v1/identities`, `GET /v1/identities/{name}`) as `{"identity": "<name>"}` for the session's web traffic; a session without it has no network, and an `android` session takes none.

## The token, once

`token` is in the answer of the start that made the session and in no other answer. The ledger keeps only its SHA-256. Sent again with the same `requestId` and the same values, the start answers the same session without a token and debits nothing more; with other values it is `request_reused`.

A lost token is replaced. Each new value is shown once, and the one before it stops working when the gateway next reads the sessions from the coordinator. A gateway that cannot reach the coordinator serves with the list it read last, so a new token or a stop takes effect there once it reads the list again:

```
POST /app/api/sessions/{sessionId}/token
Content-Type: application/json

{"confirm": true}
```

## Drive the device

`controlUrl` of the session is its MCP endpoint: MCP Streamable HTTP, with one JSON-RPC message in each request and a JSON answer. Send `POST` requests to `controlUrl` with these headers, while the session is `running`:

```
Authorization: Bearer <token>
Content-Type: application/json
MCP-Protocol-Version: 2025-11-25
```

Start with `initialize`, then `tools/list` answers `snapshot` and `act`. A snapshot:

```json
{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "snapshot", "arguments": {"tree": true, "image": false}}}
```

```json
{"jsonrpc": "2.0", "id": 3, "result": {"content": [{"type": "text", "text": "snapshot 7216815e47b02775 screen 720x1280 rotation 0 nodes 3\ne1 EditText \"\" id=url_field [0,48][624,144] clickable focused\ne2 ImageButton \"\" label=\"Load URL\" id=btn_load_url [624,48][720,144] clickable\ne3 WebView \"\" [0,144][720,1280] scrollable\n"}], "structuredContent": {"screen": {"width": 720, "height": 1280, "rotation": 0}, "tree": {"digest": "7216815e47b02775", "nodes": 3, "truncated": false}}, "isError": false}}
```

An act, which runs its actions in order and reads the screen again:

```json
{"jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": {"name": "act", "arguments": {"actions": [{"action": "open_url", "url": "https://example.com"}, {"action": "wait", "forText": "Example Domain", "timeoutMs": 15000}], "observe": "tree"}}}
```

```json
{"jsonrpc": "2.0", "id": 4, "result": {"content": [{"type": "text", "text": "1 open_url ok\n2 wait ok\n\nsnapshot 7285b9e8c2da3527 screen 720x1280 rotation 0 nodes 6\ne1 EditText \"https://example.com/\" id=url_field [0,48][624,144] clickable\ne2 ImageButton \"\" label=\"Load URL\" id=btn_load_url [624,48][720,144] clickable\ne3 WebView \"Example Domain\" [0,144][720,1280] scrollable\n  e4 TextView \"Example Domain\" [32,188][688,236]\n  e5 TextView \"This domain is for use in documentation examples without needing permission. Avoid use in operations.\" [32,268][688,356]\n  e6 View \"\" label=\"Learn more\" [32,388][176,424] clickable\n"}], "structuredContent": {"results": [{"action": "open_url", "code": "ok"}, {"action": "wait", "code": "ok"}], "screen": {"width": 720, "height": 1280, "rotation": 0}, "tree": {"digest": "7285b9e8c2da3527", "nodes": 6, "truncated": false}}, "isError": false}}
```

A ref such as `e2` is valid only for the snapshot it came from: send that snapshot's digest in `snapshot` with the ref.

## Extend and stop

An extension names the new length of the whole block, so the same call sent twice extends once. The added minutes cost the price the session started at.

```
POST /app/api/sessions/{sessionId}/extend
Content-Type: application/json

{"minutesTotal": 60, "expectedUnitUsdMicros": "<unitUsdMicros of the session>", "confirm": true}
```

A stop ends the session and answers it with `minutesBilled` and `refundUsdMicros`:

```
POST /app/api/sessions/{sessionId}/stop
Content-Type: application/json

{"confirm": true}
```

`GET /app/api/sessions/{sessionId}` reads one session and `GET /app/api/sessions` the account's sessions, newest first, with `nextCursor` for the next page. The month's sessions are in `GET /app/api/billing/statement` under `sessions`.

## What is billed

- The block of minutes is debited from the paid balance at the start, and each extension when it is made.
- Billing runs from the moment the device is ready (`runningAt`), by the started minute, and never past the paid block.
- At the end the rest of the block goes back to the paid balance, once: on a stop, when the paid time runs out, and when the session fails.
- A session whose device never became ready pays nothing, and a failed session pays only the whole minutes it ran. A session whose device failed, or whose host fell silent, is failed whoever ends it: a stop answers it `failed` with the cause.

## On request

With no price in force or no device free, a quote answers `{"quote": null, "onRequest": 1, "limits"}` and a start `{"session": null, "onRequest": 1}`. The request is recorded as demand for the product, and nothing is charged.

## Refusals

Every refusal carries `code`, `cause` and `fix`, and some carry the detail named here.

| Code | Status | Next call |
|---|---|---|
| `device_sessions_off` | 503 | Ask for the product with `POST /app/api/demand/device` |
| `confirmation_required` | 400 | Ask the person in charge, then send `"confirm": true` |
| `invalid` | 400 | Correct the field that `field` names, or the body, and send it again |
| `unknown_product` | 400 | Send `product` as `emulator` or `android` |
| `price_changed` | 409 | Quote again, confirm the new `unitUsdMicros` with the person in charge, and start again |
| `insufficient_balance` | 402 | Add at least `shortfallUsdMicros` to the paid balance, then send the same request again |
| `session_limit` | 429 | `limit` names the bound and `most` its value: ask for fewer minutes, stop a session, or after `retryAt` start again |
| `exit_not_found` | 404 | Name an identity of the account with `GET /v1/identities/{name}`, or leave `exit` out |
| `request_reused` | 409 | Send the original start again to read it, or use a new `requestId` |
| `purchases_closed` | 409 | Start again once `GET /app/api/buy` answers `charging` true |
| `account_suspended` | 403 | Ask support to review the account |
| `session_not_found` | 404 | Use a `sessionId` from `GET /app/api/sessions` |
| `session_starting` | 409 | Read the session until `state` is `running`, then extend it |
| `session_ended` | 410 | Start a new session with `POST /app/api/sessions` |
| `rate_limited` | 429 | Wait, then send the same request again |
| `session_required` | 403 | Send the session cookie and no `Authorization` header |
| `sign_in_required` | 401 | Sign in with `POST /app/api/login` |

The [OpenAPI document](/openapi.json) lists every call with its body and its answer.
