Skip to content

Guides

Sandbox lifecycle

How long a sandbox lives, what the API tells you about it, and how to clean up after an agent that lost track.

Lifetime and idle timeout

timeout is the most a sandbox lives, in seconds: 3600 by default, up to 86400 or the provider's own maximum, whichever is lower. idle_timeout kills it sooner, after that many seconds without a call on it; a running command counts as a call until it ends. It is off by default on the API and the SDKs, and 600 seconds for sandboxes the MCP server starts. Pause a sandbox to stop paying for compute while keeping it: create it with require pause. Or kill it as soon as you are done: billing stops that second.

Shell
API=https://api.sandboxplease.com
AUTH="Authorization: Bearer $SANDBOXPLEASE_API_KEY"
curl -X POST "$API/v1/sandboxes" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"sandbox": "auto", "timeout": 1800, "idle_timeout": 300}'

Commands, files and ports

POST /v1/sandboxes/{id}/exec runs cmd in a shell, with an optional cwd, env and timeout in seconds; env on the create already applies to every command. It answers exit_code, stdout and stderr once the command ends, up to 10 MiB of output; with "stream": true it sends NDJSON lines as the output comes ({"type": "stdout", "data": ...}, then {"type": "exit", "exit_code": 0}). The SDKs always stream.

Shell
curl -X POST "$API/v1/sandboxes/$ID/exec" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"cmd": "npm test", "cwd": "/home/user/app", "env": {"CI": "1"}, "timeout": 600}'

PUT /v1/sandboxes/{id}/files?path=... writes the request body to a file, GET on the same path reads it back as bytes, and adding &list lists a directory. GET /v1/sandboxes/{id}/ports/{port} returns a public URL for a port the sandbox listens on, on providers with ports.

Shell
curl -X PUT "$API/v1/sandboxes/$ID/files?path=/home/user/app.py" -H "$AUTH" --data-binary @app.py
curl "$API/v1/sandboxes/$ID/files?path=/home/user/out.txt" -H "$AUTH"
curl "$API/v1/sandboxes/$ID/ports/8080" -H "$AUTH"

The sandbox object

Create, get, list, pause, resume and kill all answer with the same object: GET /v1/sandboxes lists the running and paused ones, and DELETE /v1/sandboxes/{id} kills one. The create answer also carries attempts, every provider tried in order, and skipped, the providers you named that could not run it.

id
SandboxPlease's id for the sandbox, used by every other call.
sandbox
The provider and size that serve it, e.g. "e2b/standard".
provider
The provider the router picked.
provider_sandbox_id
The sandbox's id at its provider, for the vendor's own SDK.
provider_metadata
Non-secret fields of the provider's create response: native id, URLs, region, image.
region
"eu" or "us": the requested provider.region, else the provider's default; null when unknown.
network
The network policy the provider enforces; null when the create set none.
size
The vCPUs and memory it really runs with, and the size name asked for.
status
"running", "paused" or "killed".
billing
"credits", or "byok" when it runs on your own provider key.
price_per_hour
USD per hour at the provider's list price, GPUs included.
cost
USD billed so far, devices included.
compute_cost
USD billed so far for the sandbox itself, without devices: cost minus device_cost.
fallback
Set when another provider than the first choice serves it: from, reason and message.
cold_start_ms
Milliseconds from the create request to a sandbox ready to run a command.
started_at
When it started.
expires_at
When timeout kills it.
idle_timeout
Seconds without a call before it is killed; null when off.
devices
Mobile devices attached at create, empty when none.
gpu
GPUs attached at create: type, count and memory each; null without.
device_price_per_minute
USD per started minute for all attached devices together.
device_cost
What the devices cost so far, included in cost.
device_minutes
Started minutes the devices have billed so far, rounded up (61 s bills 2); device_cost is device_minutes × device_price_per_minute.
ended_at
When it ended; null while it runs.
end_reason
Why it ended: killed, lifetime_limit, idle, out_of_credits, key_limit, provider_gone, suspended, cost_cap, key_disabled or key_expired.
from_snapshot
The snapshot it started from; null otherwise.

Retry a create safely

Send an Idempotency-Key header with any unique value. Retrying with the same key returns the sandbox the first call started, with status 200 and the header idempotent-replayed: true, instead of starting a second one. The SDKs send one on every create and retry on network errors, 429 and 5xx.

Shell
curl -X POST "$API/v1/sandboxes" -H "$AUTH" -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" -d '{"sandbox": "auto"}'

Kill everything

POST /v1/sandboxes/kill-all kills every running or paused sandbox your key reaches in one call, for an agent that crashed or lost its ids. Narrow it with provider, key (an API key id) or older_than_hours (1 to 24: only sandboxes started that long ago). An owner's or admin's key reaches the whole workspace, a member's key only that member's sandboxes. pending counts the ones the provider is still stopping: they are no longer billed.

Shell
curl -X POST "$API/v1/sandboxes/kill-all" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"older_than_hours": 6}'

The same is sp.sandboxes.killAll() / kill_all() in the SDKs, sp kill --all and the MCP tool kill_all_sandboxes.

Credits and balance

On credits, pay exactly the provider's list price. No markup, no fees. Credits are bought from $10, and your first top-up comes with $5 free. GET /v1/key returns the balance and what is left of the key's credit limit this period: check it before a long job. A sandbox created past the limit is refused with key_limit_reached, and running ones are killed when it is reached. Sandboxes on your own provider key are billed by the provider.

Shell
curl "$API/v1/key" -H "$AUTH"

No key yet? sp login gets one in the browser, and agents use POST /v1/auth/device then POST /v1/auth/device/token to have a person approve it: see the API reference.