Create a sandbox
Save this assandbox_demo.py and run it with uv run python sandbox_demo.py:
0 and the sandbox’s Python version. A sandbox bills until
it stops, so the finally block terminates it even when a command fails. A
plain number for memory means MiB.
Reconnect to a running sandbox
Another process can reconnect by ID before the sandbox expires:sandbox.list() returns the workspace’s sandboxes, and sandbox.stats()
counts them.
Use a running sandbox
Commands and processes
run waits for the command to exit, and process controls long-running ones:
pid, status(), kill(), wait(), and stdout, stderr, and
logs streams with read() and lines().
Files
instance.fs reads and writes the sandbox filesystem, rooted at /workspace:
Ports and network
ports=[8000] on the sandbox
exposes ports at start. Outbound access is either blocked or limited to CIDR
ranges.
Snapshots
Image.from_id(...).
Async
Each API has an async form under.aio, such as await instance.aio.run(...).
Sandbox options
Sandboxes acceptenv, secrets, volumes, gpu and gpu_count,
sync_local_dir=True to copy your working directory in at creation,
command for the initial process, ports, docker_enabled, and machine.
keep_warm_seconds sets how long an idle sandbox stays up, 600 by default.
Docker inside a sandbox
image=Image().with_docker() with docker_enabled=True runs a Docker daemon in
the sandbox, and instance.docker wraps run, build, ps, logs,
compose_up, and the rest. Nested containers share the sandbox’s network, so
each service binds its own port and instance.expose_port(port) gives its URL.
Stop and clean up
instance.terminate() stops a sandbox at once. Otherwise it stops after
keep_warm_seconds idle. The dashboard shows running
sandboxes with a terminal, file browser, and process list.
