# Sandboxes

Full Alpine Linux microVMs and Browser Sandboxes (with CDP) that AI agents create, run commands in, transfer files with, and destroy.

A **Sandbox** is a dedicated Alpine Linux Nitro Sandbox with a persistent **8 GiB** writable root disk that survives idle stops until you destroy it. Sandboxes run in a separate host pool from Workers.

The default base image includes a shell, **Python 3 + pip**, **Node + npm**, **git**, **curl**, a **C/C++ toolchain**, and the bindings forwarder (`SKOPIK_BINDINGS_URL`).

## Creating & Running Commands

Choose `1` or `2` vCPUs and `256`–`1024` MiB of memory (defaults: `1` vCPU, `512` MiB). Size, `bindings`, and `mounts` are fixed at creation time.

:::tabs
::tab skopik
```bash
skopik create_sandbox --vcpus 1 --memory-mib 512
skopik exec_in_sandbox --sandbox-id sbx_... --command "python3 --version && git --version"
skopik destroy_sandbox --sandbox-id sbx_...
```
::tab curl
```bash
# Create a Sandbox
curl -X POST https://api.platform.skopik.com/v1/sandboxes \
  -H "Authorization: Bearer $SKOPIK_API_KEY" \
  -H "content-type: application/json" \
  -d '{"vcpus": 1, "memory_mib": 512}'

# Run a command (add "stream": true for NDJSON streaming)
curl -X POST https://api.platform.skopik.com/v1/sandboxes/$SANDBOX/exec \
  -H "Authorization: Bearer $SKOPIK_API_KEY" \
  -H "content-type: application/json" \
  -d '{"command": "python3 -c \"print(6*7)\"", "timeout_secs": 60}'
```
::tab MCP
```json
{
  "tool": "exec_in_sandbox",
  "arguments": {
    "sandbox_id": "sbx_...",
    "command": "python3 -c 'print(6*7)'",
    "timeout_secs": 60
  }
}
```
:::

- **Timeouts:** Commands default to a 60-second timeout (maximum 1 hour). Timed-out commands and their child processes are terminated.
- **Streaming:** Pass `"stream": true` to `POST /v1/sandboxes/{id}/exec` to receive newline-delimited JSON frames (`stdout`, `stderr`, `exit`).

## Files & Directory Listing

- **HTTP routes:** `PUT /v1/sandboxes/{id}/files?path=/work/app.py&mode=644` and `GET /v1/sandboxes/{id}/files?path=/work/app.py` stream raw files up to **1 GiB**.
- **MCP tools:** `write_sandbox_file` and `read_sandbox_file` transfer UTF-8 or base64 files up to **4 MiB**.
- **Directory listing:** `GET /v1/sandboxes/{id}/dir?path=/work` (MCP `list_sandbox_files`) returns paginated entries with `name`, `type`, `size`, `mode`, `modified_at`, and symlink `target`.

## Idle Stop & Automatic Backups

- A Sandbox with no active command, file transfer, or CDP session stops after **5 minutes** of idle time, freeing host memory while keeping its disk intact.
- On every stop, changed 1 MiB disk chunks are compressed with zstd and backed up to S3. If a host is replaced, the next call automatically restores the Sandbox on another host.
- Destroying a Sandbox deletes both its disk and its S3 backup immediately.

## Browser Sandboxes (CDP)

Create a Sandbox with `"image": "browser"` to launch an isolated Chromium environment accessible over the Chrome DevTools Protocol (CDP):

```bash
curl -X POST https://api.platform.skopik.com/v1/sandboxes \
  -H "Authorization: Bearer $SKOPIK_API_KEY" \
  -H "content-type: application/json" \
  -d '{"image": "browser", "vcpus": 2, "memory_mib": 1024}'
```

Connect Playwright or Puppeteer via authenticated WebSocket to:

```text
wss://api.platform.skopik.com/v1/sandboxes/{id}/cdp
```

Include `Authorization: Bearer <SKOPIK_API_KEY>` in the WebSocket upgrade headers. Nothing else on the internet can reach the browser instance.
