# API reference

> The Sandboxer public HTTP API — base URL, authentication, envelopes, errors, pagination and every endpoint.

## Base URL

```text
https://sandboxer.com/api/v1
```

`https://api.sandboxer.com/v1` is an alias. The machine-readable description is at [/openapi.yaml](/openapi.yaml) (also `GET /api/v1/openapi.yaml`).

## Authentication

Send an API key as a bearer token on every request:

```bash
curl -sS https://sandboxer.com/api/v1/me \
  -H "Authorization: Bearer $SANDBOXER_API_KEY"
```

Keys look like `sbx_live_` followed by 40 characters. Create them on the dashboard or with `sbx api-key create`; scope them to actions, sandboxes and environments. See [API keys](/docs/api-keys). Inside a sandbox, `SANDBOXER_TOKEN` holds a key for the calls the sandbox makes about itself.

## Envelopes

Every JSON response is an object with `ok` and a `type` discriminator, and every response carries an `X-Request-Id` header.

```json title="Success"
{
  "ok": true,
  "type": "sandbox.info",
  "sandbox": { "id": "sb_23456789", "state": "ready", "type": "default" }
}
```

```json title="Error"
{
  "ok": false,
  "type": "sandbox.error",
  "status": 409,
  "code": "machine_not_running",
  "message": "The sandbox is stopped. Resume it first.",
  "error": { "code": "machine_not_running", "message": "The sandbox is stopped. Resume it first.", "status": 409 },
  "requestId": "req_01HX7Q2M8ZK4"
}
```

Branch on `code`, show `message` to people, and include `requestId` when you contact support. The top-level `code`/`message`/`status` and the nested `error` object always agree.

## Status codes

| Status | Meaning | Typical `code` |
| - | - | - |
| `400` | The request is malformed | `invalid_json`, `invalid_env`, `invalid_timeout`, `invalid_path` |
| `401` | Missing or invalid key | `unauthorized` |
| `402` | The wallet can't start machines: no card or plan yet, the last payment failed, an existing trial ended, or balance empty | `payment_method_required`, `payment_past_due`, `billing_required`, `trial_expired` |
| `403` | The key's scope or your plan doesn't allow it | `api_key_action_forbidden`, `machine_class_plan_required` |
| `404` | Not found, or not yours | `not_found`, `snapshot_not_found` |
| `409` | Conflicts with the current state | `machine_not_running`, `sandbox_stopping`, `idempotency_in_progress`, `no_running_turn` |
| `429` | Start or concurrency limit | `rate_limited`, `limit_reached` |
| `503` | No capacity right now | `no_ready_machine`, `capacity_exceeded` |

See [Limits & errors](/docs/limits) for retry advice.

## Idempotent creates

Send `Idempotency-Key: <uuid>` on `POST /sandboxes` to make it safe to retry after a timeout: the same key and body return the same sandbox for 24 hours instead of creating a second, billable one. Reusing a key with a different body returns `409 idempotency_key_reused`.

```bash
curl -sS -X POST https://sandboxer.com/api/v1/sandboxes \
  -H "Authorization: Bearer $SANDBOXER_API_KEY" \
  -H "Idempotency-Key: 6f2c0a3e-8d7b-4b1e-9a55-0c2f1e7d9b10" \
  -H "Content-Type: application/json" \
  -d '{"type":"small"}'
```

## Pagination

List endpoints accept `limit` (1–200, default 100), `cursor` and `sort` (`asc`/`desc`) and return `pageInfo`:

```json
{ "pageInfo": { "nextCursor": "eyJ0IjoiMjAyNi0xMC0xMVQxMjowMDowMFoifQ", "hasMore": true, "limit": 100 } }
```

Pass `nextCursor` back as `cursor` until `hasMore` is `false`. Cursors are opaque.

## Labels

Sandboxes and named snapshots carry `labels`, string keys and values you choose, such as `{"pr":"412","repo":"web"}`. Use them to find the sandboxes and templates that belong to a branch, a customer or a job. Labels are set through the API and the [MCP server](/docs/mcp). The CLI and SDKs don't have a labels command yet.

```bash title="curl"
# Set labels when you create a sandbox
curl -sS -X POST https://sandboxer.com/api/v1/sandboxes \
  -H "Authorization: Bearer $SANDBOXER_API_KEY" -H "Content-Type: application/json" \
  -d '{"labels":{"pr":"412","repo":"web"}}'

# Replace a sandbox's labels (send {} to clear them)
curl -sS -X PATCH https://sandboxer.com/api/v1/sandboxes/sb_k3v9x2mq \
  -H "Authorization: Bearer $SANDBOXER_API_KEY" -H "Content-Type: application/json" \
  -d '{"labels":{"pr":"412","repo":"web","stage":"review"}}'

# List the sandboxes that carry all of these labels
curl -sS -G https://sandboxer.com/api/v1/sandboxes \
  -H "Authorization: Bearer $SANDBOXER_API_KEY" \
  --data-urlencode "labels=pr=412,repo=web"
```

- Every sandbox returns its `labels`. A `PATCH` replaces the whole set; it doesn't merge.
- At most 64 labels. Keys are 1–128 letters, digits, `.`, `_`, `-` or `/`. Values are up to 1024 bytes.
- The `labels` filter takes `k=v` pairs separated by commas, or a JSON object, and matches sandboxes that carry all of them. A malformed filter or label returns `400 invalid_labels`.
- Named snapshots take the same `labels` on save and the same filter on `GET /named-snapshots`. `POST /sandboxes` with `fromLabels` starts from the newest ready snapshot carrying all of them ([snapshots](/docs/snapshots#named-snapshots-templates)).
- Labels set through the [Daytona-compatible API](/docs/daytona) are the same labels.

## Choosing a wallet

Requests bill your **active wallet** (personal unless you switched). To bill an organization for one request, add `X-Sandboxer-Org: <team id or name>` or `?org=`. `personal` selects your own account. See [Organizations](/docs/organizations).

## Asynchronous operations

Create, stop, resume and fork return `202` immediately with the sandbox in a transitional state (`provisioning`, `archiving`). Then either:

- poll `GET /sandboxes/{id}` until `state` is `ready` (or `archived`), backing off to about once a second;
- or subscribe to [webhooks](/docs/webhooks) (`sandbox.ready`, `sandbox.archived`, `sandbox.error`).

## Interactive exec over WebSocket

`GET /sandboxes/{id}/exec/ws` upgrades to a WebSocket for PTY sessions (this is what the dashboard terminal uses):

1. Send a text frame `{"command": null, "pty": true, "cols": 120, "rows": 32}` (`command: null` with a PTY gives a login shell; or pass a string or argv array).
2. Binary frames you send are stdin. Text frames are control messages: `{"type":"resize","cols":…,"rows":…}`, `{"type":"eof"}`, `{"type":"signal","signal":"SIGINT"}`.
3. Binary frames you receive start with one byte for the stream (`1` stdout, `2` stderr) followed by data. The last message is a text frame `{"type":"exit","exitCode":0}`.

## SDKs

Typed clients generated from the same OpenAPI document: [TypeScript](/docs/sdk-typescript) and [Python](/docs/sdk-python), both `sandboxer-sdk`. Method names match each endpoint's `operationId`.

## Client versions

`sbx` and the SDKs send their version in `User-Agent` (`sbx/<version>`, `sandboxer-ts/<version>`, `sandboxer-python/<version>`). When that version is out of date, every response carries two headers:

| Header | What it is |
| - | - |
| `Sandboxer-Client-Warning` | One line saying it's out of date and how to update |
| `Sandboxer-Client-Latest` | The newest version |

The call itself works as usual. A release that's known to be broken is refused with `426 client_outdated`, whose message says how to upgrade.
