# Synced sessions


## Get code control

`GET /v1/threads/{threadId}/control`

Read whether your Code session accepts phone control

Permission: `sessions.control` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `threadId` | path | string | yes |  |

### Responses

- `200` Current control or delivery state. (application/json, CodeControl)
  - `runnerDeviceId` (string | null, required)
  - `enabled` (boolean, required)
  - `expiresAt` (integer | null, required)
  - `serverTime` (integer): Gateway's clock when it answered, in Unix milliseconds. A device seals a command's expiresAt by this clock. An older Gateway leaves it out.
- `400` Malformed fields, ciphertext or expiry.
- `403` Invalid signature or a caller that is not a person.
- `404` The session, command or device does not belong to the caller.
- `409` A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts.
- `410` The session was deleted.
- `429` The bounded command mailbox is full.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 |  | Malformed fields, ciphertext or expiry. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | The session, command or device does not belong to the caller. |
| 409 |  | A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts. |
| 410 |  | The session was deleted. |
| 429 |  | The bounded command mailbox is full. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |

### Example

```sh
curl "$NEXUS_URL/v1/threads/<threadId>/control" \
  -H "Authorization: Bearer $NEXUS_API_KEY"
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#get%3A%2Fv1%2Fthreads%2F%7BthreadId%7D%2Fcontrol)

## Set code control

`PUT /v1/threads/{threadId}/control`

Renew a signed, locally enabled Code runner lease

Permission: `sessions.control` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `threadId` | path | string | yes |  |

### Request body (application/json, required)

- `runnerDeviceId` (string, required)
- `enabled` (boolean, required)
- `expiresAt` (integer, required)
- `signature` (string, required)

### Responses

- `200` Current control or delivery state. (application/json, CodeControl)
  - `runnerDeviceId` (string | null, required)
  - `enabled` (boolean, required)
  - `expiresAt` (integer | null, required)
  - `serverTime` (integer): Gateway's clock when it answered, in Unix milliseconds. A device seals a command's expiresAt by this clock. An older Gateway leaves it out.
- `400` Malformed fields, ciphertext or expiry.
- `403` Invalid signature or a caller that is not a person.
- `404` The session, command or device does not belong to the caller.
- `409` A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts.
- `410` The session was deleted.
- `429` The bounded command mailbox is full.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 | `invalid_request_error` | A field is missing or not valid; the message names it |
| 400 |  | Malformed fields, ciphertext or expiry. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | The session, command or device does not belong to the caller. |
| 409 |  | A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts. |
| 410 |  | The session was deleted. |
| 429 |  | The bounded command mailbox is full. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |

### Example

```sh
curl -X PUT "$NEXUS_URL/v1/threads/<threadId>/control" \
  -H "Authorization: Bearer $NEXUS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "runnerDeviceId": "<runnerDeviceId>",
    "enabled": false,
    "expiresAt": 1,
    "signature": "<signature>"
  }'
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#put%3A%2Fv1%2Fthreads%2F%7BthreadId%7D%2Fcontrol)

## List code commands

`GET /v1/threads/{threadId}/commands`

Read up to 20 queued commands for your trusted runner

Permission: `sessions.control` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `threadId` | path | string | yes |  |
| `runnerDeviceId` | query | string | yes |  |

### Responses

- `200` Current control or delivery state. (application/json, CodeCommands)
  - `commands` (array of CodeCommand, required) [at most 20 items]
    - `id` (string (uuid), required)
    - `senderDeviceId` (string, required)
    - `runnerDeviceId` (string, required)
    - `expiresAt` (integer, required)
    - `lockedKey` (string, required): 80 bytes of HPKE ciphertext, base64url without padding.
    - `sealed` (string, required): Encrypted action payload, base64url without padding. At most 65581 decoded bytes.
    - `signature` (string, required): Ed25519 signature over the domain-separated command and its routing fields, base64url without padding.
    - `waits` (boolean): The command may wait for a sleeping computer. Absent means false.
    - `threadId` (string, required)
    - `state` ("queued" | "claimed" | "completed" | "rejected" | "expired" | "cancelled", required)
- `400` Malformed fields, ciphertext or expiry.
- `403` Invalid signature or a caller that is not a person.
- `404` The session, command or device does not belong to the caller.
- `409` A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts.
- `410` The session was deleted.
- `429` The bounded command mailbox is full.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 | `invalid_request_error` | A field is missing or not valid; the message names it |
| 400 |  | Malformed fields, ciphertext or expiry. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | The session, command or device does not belong to the caller. |
| 409 |  | A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts. |
| 410 |  | The session was deleted. |
| 429 |  | The bounded command mailbox is full. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |

### Example

```sh
curl "$NEXUS_URL/v1/threads/<threadId>/commands?runnerDeviceId=%3CrunnerDeviceId%3E" \
  -H "Authorization: Bearer $NEXUS_API_KEY"
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#get%3A%2Fv1%2Fthreads%2F%7BthreadId%7D%2Fcommands)

## Send code command

`POST /v1/threads/{threadId}/commands`

Queue an encrypted command for your own Code session

Permission: `sessions.control` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `threadId` | path | string | yes |  |

### Request body (application/json, required)

- `id` (string (uuid), required)
- `senderDeviceId` (string, required)
- `runnerDeviceId` (string, required)
- `expiresAt` (integer, required)
- `lockedKey` (string, required): 80 bytes of HPKE ciphertext, base64url without padding.
- `sealed` (string, required): Encrypted action payload, base64url without padding. At most 65581 decoded bytes.
- `signature` (string, required): Ed25519 signature over the domain-separated command and its routing fields, base64url without padding.
- `waits` (boolean): The person lets the command wait for a computer that is asleep or offline. Gateway then queues it without a live lease, and `expiresAt` may be up to 24 hours ahead (30 seconds more for clock skew). The sender signs `nexus-code-command-v2`, with `true` after the expiry line, so a runner that verifies only v1 never runs it. Absent means false.

### Responses

- `200` Current control or delivery state. (application/json, CodeCommand)
  - `id` (string (uuid), required)
  - `senderDeviceId` (string, required)
  - `runnerDeviceId` (string, required)
  - `expiresAt` (integer, required)
  - `lockedKey` (string, required): 80 bytes of HPKE ciphertext, base64url without padding.
  - `sealed` (string, required): Encrypted action payload, base64url without padding. At most 65581 decoded bytes.
  - `signature` (string, required): Ed25519 signature over the domain-separated command and its routing fields, base64url without padding.
  - `waits` (boolean): The command may wait for a sleeping computer. Absent means false.
  - `threadId` (string, required)
  - `state` ("queued" | "claimed" | "completed" | "rejected" | "expired" | "cancelled", required)
- `400` Malformed fields, ciphertext or expiry.
- `403` Invalid signature or a caller that is not a person.
- `404` The session, command or device does not belong to the caller.
- `409` A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts.
- `410` The session was deleted.
- `429` The bounded command mailbox is full.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 | `invalid_request_error` | A field is missing or not valid; the message names it |
| 400 |  | Malformed fields, ciphertext or expiry. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | The session, command or device does not belong to the caller. |
| 409 |  | A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts. |
| 410 |  | The session was deleted. |
| 429 |  | The bounded command mailbox is full. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |

### Example

```sh
curl -X POST "$NEXUS_URL/v1/threads/<threadId>/commands" \
  -H "Authorization: Bearer $NEXUS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "<id>",
    "senderDeviceId": "<senderDeviceId>",
    "runnerDeviceId": "<runnerDeviceId>",
    "expiresAt": 1,
    "lockedKey": "<lockedKey>",
    "sealed": "<sealed>",
    "signature": "<signature>"
  }'
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#post%3A%2Fv1%2Fthreads%2F%7BthreadId%7D%2Fcommands)

## Get code command

`GET /v1/threads/{threadId}/commands/{commandId}`

Read the delivery state of your own Code command

Permission: `sessions.control` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `threadId` | path | string | yes |  |
| `commandId` | path | string (uuid) | yes |  |

### Responses

- `200` Current control or delivery state. (application/json, CodeCommand)
  - `id` (string (uuid), required)
  - `senderDeviceId` (string, required)
  - `runnerDeviceId` (string, required)
  - `expiresAt` (integer, required)
  - `lockedKey` (string, required): 80 bytes of HPKE ciphertext, base64url without padding.
  - `sealed` (string, required): Encrypted action payload, base64url without padding. At most 65581 decoded bytes.
  - `signature` (string, required): Ed25519 signature over the domain-separated command and its routing fields, base64url without padding.
  - `waits` (boolean): The command may wait for a sleeping computer. Absent means false.
  - `threadId` (string, required)
  - `state` ("queued" | "claimed" | "completed" | "rejected" | "expired" | "cancelled", required)
- `400` Malformed fields, ciphertext or expiry.
- `403` Invalid signature or a caller that is not a person.
- `404` The session, command or device does not belong to the caller.
- `409` A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts.
- `410` The session was deleted.
- `429` The bounded command mailbox is full.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 |  | Malformed fields, ciphertext or expiry. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | The session, command or device does not belong to the caller. |
| 409 |  | A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts. |
| 410 |  | The session was deleted. |
| 429 |  | The bounded command mailbox is full. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |

### Example

```sh
curl "$NEXUS_URL/v1/threads/<threadId>/commands/<commandId>" \
  -H "Authorization: Bearer $NEXUS_API_KEY"
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#get%3A%2Fv1%2Fthreads%2F%7BthreadId%7D%2Fcommands%2F%7BcommandId%7D)

## Claim code command

`POST /v1/threads/{threadId}/commands/{commandId}/claim`

Claim a Code command once before executing it

Permission: `sessions.control` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `threadId` | path | string | yes |  |
| `commandId` | path | string (uuid) | yes |  |

### Request body (application/json, required)

- `runnerDeviceId` (string, required)
- `signature` (string, required)

### Responses

- `200` Current control or delivery state. (application/json, CodeCommandClaimed)
  - `claimed` (boolean, required)
  - `command` (CodeCommand, required)
    - `id` (string (uuid), required)
    - `senderDeviceId` (string, required)
    - `runnerDeviceId` (string, required)
    - `expiresAt` (integer, required)
    - `lockedKey` (string, required): 80 bytes of HPKE ciphertext, base64url without padding.
    - `sealed` (string, required): Encrypted action payload, base64url without padding. At most 65581 decoded bytes.
    - `signature` (string, required): Ed25519 signature over the domain-separated command and its routing fields, base64url without padding.
    - `waits` (boolean): The command may wait for a sleeping computer. Absent means false.
    - `threadId` (string, required)
    - `state` ("queued" | "claimed" | "completed" | "rejected" | "expired" | "cancelled", required)
- `400` Malformed fields, ciphertext or expiry.
- `403` Invalid signature or a caller that is not a person.
- `404` The session, command or device does not belong to the caller.
- `409` A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts.
- `410` The session was deleted.
- `429` The bounded command mailbox is full.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 | `invalid_request_error` | A field is missing or not valid; the message names it |
| 400 |  | Malformed fields, ciphertext or expiry. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | The session, command or device does not belong to the caller. |
| 409 |  | A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts. |
| 410 |  | The session was deleted. |
| 429 |  | The bounded command mailbox is full. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |

### Example

```sh
curl -X POST "$NEXUS_URL/v1/threads/<threadId>/commands/<commandId>/claim" \
  -H "Authorization: Bearer $NEXUS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "runnerDeviceId": "<runnerDeviceId>",
    "signature": "<signature>"
  }'
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#post%3A%2Fv1%2Fthreads%2F%7BthreadId%7D%2Fcommands%2F%7BcommandId%7D%2Fclaim)

## Finish code command

`POST /v1/threads/{threadId}/commands/{commandId}/result`

Report a claimed Code command completed or rejected

Permission: `sessions.control` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `threadId` | path | string | yes |  |
| `commandId` | path | string (uuid) | yes |  |

### Request body (application/json, required)

- `runnerDeviceId` (string, required)
- `state` ("completed" | "rejected", required)
- `signature` (string, required)

### Responses

- `200` Current control or delivery state. (application/json, CodeCommand)
  - `id` (string (uuid), required)
  - `senderDeviceId` (string, required)
  - `runnerDeviceId` (string, required)
  - `expiresAt` (integer, required)
  - `lockedKey` (string, required): 80 bytes of HPKE ciphertext, base64url without padding.
  - `sealed` (string, required): Encrypted action payload, base64url without padding. At most 65581 decoded bytes.
  - `signature` (string, required): Ed25519 signature over the domain-separated command and its routing fields, base64url without padding.
  - `waits` (boolean): The command may wait for a sleeping computer. Absent means false.
  - `threadId` (string, required)
  - `state` ("queued" | "claimed" | "completed" | "rejected" | "expired" | "cancelled", required)
- `400` Malformed fields, ciphertext or expiry.
- `403` Invalid signature or a caller that is not a person.
- `404` The session, command or device does not belong to the caller.
- `409` A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts.
- `410` The session was deleted.
- `429` The bounded command mailbox is full.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 | `invalid_request_error` | A field is missing or not valid; the message names it |
| 400 |  | Malformed fields, ciphertext or expiry. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | The session, command or device does not belong to the caller. |
| 409 |  | A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts. |
| 410 |  | The session was deleted. |
| 429 |  | The bounded command mailbox is full. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |

### Example

```sh
curl -X POST "$NEXUS_URL/v1/threads/<threadId>/commands/<commandId>/result" \
  -H "Authorization: Bearer $NEXUS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "runnerDeviceId": "<runnerDeviceId>",
    "state": "completed",
    "signature": "<signature>"
  }'
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#post%3A%2Fv1%2Fthreads%2F%7BthreadId%7D%2Fcommands%2F%7BcommandId%7D%2Fresult)

## Cancel code command

`POST /v1/threads/{threadId}/commands/{commandId}/cancel`

Cancel your own Code command before a computer receives it

Permission: `sessions.control` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `threadId` | path | string | yes |  |
| `commandId` | path | string (uuid) | yes |  |

### Request body (application/json, required)

- `senderDeviceId` (string, required)
- `signature` (string, required)

### Responses

- `200` The cancelled command, also when it was already cancelled. (application/json, CodeCommand)
  - `id` (string (uuid), required)
  - `senderDeviceId` (string, required)
  - `runnerDeviceId` (string, required)
  - `expiresAt` (integer, required)
  - `lockedKey` (string, required): 80 bytes of HPKE ciphertext, base64url without padding.
  - `sealed` (string, required): Encrypted action payload, base64url without padding. At most 65581 decoded bytes.
  - `signature` (string, required): Ed25519 signature over the domain-separated command and its routing fields, base64url without padding.
  - `waits` (boolean): The command may wait for a sleeping computer. Absent means false.
  - `threadId` (string, required)
  - `state` ("queued" | "claimed" | "completed" | "rejected" | "expired" | "cancelled", required)
- `400` A malformed body.
- `403` An invalid signature, a caller that is not a person, or a device that did not send the command (`code_command_not_sender`).
- `404` The session or command does not belong to the caller, or Gateway has no such command (`code_command_not_found`).
- `409` A computer already claimed the command (`code_command_not_queued`), or the device is untrusted.
- `410` The session was deleted.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 | `invalid_request_error` | A field is missing or not valid; the message names it |
| 400 |  | A malformed body. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | The session or command does not belong to the caller, or Gateway has no such command (`code_command_not_found`). |
| 409 |  | A computer already claimed the command (`code_command_not_queued`), or the device is untrusted. |
| 410 |  | The session was deleted. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |

### Example

```sh
curl -X POST "$NEXUS_URL/v1/threads/<threadId>/commands/<commandId>/cancel" \
  -H "Authorization: Bearer $NEXUS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "senderDeviceId": "<senderDeviceId>",
    "signature": "<signature>"
  }'
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#post%3A%2Fv1%2Fthreads%2F%7BthreadId%7D%2Fcommands%2F%7BcommandId%7D%2Fcancel)

## List runner code commands

`GET /v1/devices/{runnerDeviceId}/code-commands`

Read queued encrypted commands across your runner's sessions

Permission: `sessions.control` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `runnerDeviceId` | path | string | yes |  |

### Responses

- `200` Current control or delivery state. (application/json, CodeCommands)
  - `commands` (array of CodeCommand, required) [at most 20 items]
    - `id` (string (uuid), required)
    - `senderDeviceId` (string, required)
    - `runnerDeviceId` (string, required)
    - `expiresAt` (integer, required)
    - `lockedKey` (string, required): 80 bytes of HPKE ciphertext, base64url without padding.
    - `sealed` (string, required): Encrypted action payload, base64url without padding. At most 65581 decoded bytes.
    - `signature` (string, required): Ed25519 signature over the domain-separated command and its routing fields, base64url without padding.
    - `waits` (boolean): The command may wait for a sleeping computer. Absent means false.
    - `threadId` (string, required)
    - `state` ("queued" | "claimed" | "completed" | "rejected" | "expired" | "cancelled", required)
- `400` Malformed fields, ciphertext or expiry.
- `403` Invalid signature or a caller that is not a person.
- `404` The session, command or device does not belong to the caller.
- `409` A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts.
- `410` The session was deleted.
- `429` The bounded command mailbox is full.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 |  | Malformed fields, ciphertext or expiry. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | The session, command or device does not belong to the caller. |
| 409 |  | A device is untrusted, control is disabled or offline, the organization forbids synced content, or a command state conflicts. |
| 410 |  | The session was deleted. |
| 429 |  | The bounded command mailbox is full. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |

### Example

```sh
curl "$NEXUS_URL/v1/devices/<runnerDeviceId>/code-commands" \
  -H "Authorization: Bearer $NEXUS_API_KEY"
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#get%3A%2Fv1%2Fdevices%2F%7BrunnerDeviceId%7D%2Fcode-commands)
