# Execute a command in a sandbox

`POST https://sandboxer.com/api/v1/sandboxes/{sandboxId}/commands`

Runs the command synchronously by default (timeout configurable via `timeoutSeconds`, 600s cap). With `detached: true` the command starts in the background and a process id is returned immediately; poll `/sandboxes/{sandboxId}/commands/{processId}` for status and logs. With `stream: true` the output arrives as the command writes it, as newline-delimited JSON (`application/x-ndjson`): `{"type":"started"}`, then `{"type":"stdout"|"stderr","data":"..."}` per chunk, and last `{"type":"exit",...}` (the synchronous result without stdout/stderr) or `{"type":"error","error":"...","message":"..."}`. Returns 400 invalid_timeout when `timeoutSeconds` is not an integer in 1-600. A command sent while the sandbox is still starting (right after a resume) waits up to 60s for it to be ready instead of failing; 409 sandbox_starting (retryable) is returned only if it is still not ready by then. A sandbox that is not starting (`error`, `stopped`) returns 409 sandbox_not_ready with `retryable: false` and its `state` at once: resume or recover it first. Command execution is never retried automatically: a 502 sandbox_direct_failed means the command may already be running on the sandbox. Every command and its output are kept 90 days in the command history (`/sandboxes/{sandboxId}/command-history`), readable while the sandbox is stopped.

## Parameters

| Name | In | Required | Description |
| - | - | - | - |
| `sandboxId` | path | yes | Public Sandbox id returned by create/list/get sandbox calls. |

## Request body

JSON, schema `CommandRequest` (see /openapi.yaml).

## Responses

- `200` Command result (synchronous), process start confirmation (detached), or the output stream (`stream: true`).
- `400` Invalid request body or parameters. (`ErrorEnvelope`)
- `401` Missing or invalid bearer token. (`ErrorEnvelope`)
- `404` Resource not found. (`ErrorEnvelope`)
- `409` Request conflicts with current account or sandbox state. (`ErrorEnvelope`)
