Skip to main content

Installation


Sandbox

Class Methods

await Sandbox.create(...): Sandbox

Create a new sandbox.
cpuCount and memoryMB are not available in the Python SDK. Use the HTTP API for custom resources.

await Sandbox.connect(sandbox_id, ...): Sandbox

await Sandbox.create_from_checkpoint(checkpoint_id, ...): Sandbox

await Sandbox.create_checkpoint_patch(checkpoint_id, script, ...): dict

await Sandbox.list_checkpoint_patches(checkpoint_id, ...): list[dict]

await Sandbox.delete_checkpoint_patch(checkpoint_id, patch_id, ...): None

Context Manager

Auto-kills the sandbox on exit:

Instance Methods

await sandbox.kill(): None

Terminate the sandbox.

await sandbox.is_running(): bool

Check if the sandbox is running.

await sandbox.set_timeout(timeout): None

Update idle timeout.

await sandbox.create_checkpoint(name): dict

Create a named checkpoint. Returns checkpoint info as a dictionary.

await sandbox.list_checkpoints(): list[dict]

List all checkpoints for the sandbox.

await sandbox.restore_checkpoint(checkpoint_id): None

Revert in-place to a checkpoint.

await sandbox.delete_checkpoint(checkpoint_id): None

Delete a checkpoint.

await sandbox.create_preview_url(port, domain?, auth_config?): dict

await sandbox.list_preview_urls(): list[dict]

await sandbox.delete_preview_url(port): None

await sandbox.close(): None

Close HTTP clients. Called automatically by the context manager.

Not Available in Python

These features are TypeScript-only. Use the HTTP API directly:
  • hibernate() / wake()
  • cpuCount / memoryMB on create

Properties


Agent

Accessed via sandbox.agent.

await sandbox.agent.start(...): AgentSession

Start an agent session.
resume, on_exit, and on_scrollback_end are not available in the Python SDK.

await sandbox.agent.attach(session_id, ...): AgentSession

Reconnect to a running agent session. Accepts on_event and on_error.

await sandbox.agent.list(): list[AgentSessionInfo]

List all agent sessions.

AgentSession

await session.collect_events(): list[AgentEvent]

Collect all events until the agent process exits. Python-unique alternative to callbacks.

await session.wait(): int

Wait for the agent to finish. Returns the exit code.

AgentEvent

Dataclass with dict-like access:

AgentSessionInfo


Exec

Accessed via sandbox.exec.

await sandbox.exec.run(command, ...): ProcessResult

Run a command synchronously via sh -c.

await sandbox.exec.start(command, ...): ExecSession

Start a long-running command with streaming I/O. Returns an ExecSession — see the full reference.

await sandbox.exec.background(command, ...): ExecSession

Alias for start. Same signature and return type.

await sandbox.exec.shell(cwd=None, env=None): Shell

Open a stateful shell session whose cwd, env vars, and shell functions persist across .run() calls. Foreground-only. See the full reference.

await sandbox.exec.attach(session_id, ...): ExecSession

Reconnect to a running exec session over WebSocket, with the same streaming callbacks as start.

await sandbox.exec.list(): list[ExecSessionInfo]

List all exec sessions.

await sandbox.exec.kill(session_id, signal=9): None

Kill an exec session.

ProcessResult

ExecSessionInfo


Filesystem

Accessed via sandbox.files.

await sandbox.files.read(path): str

Read a file as a UTF-8 string.

await sandbox.files.read_bytes(path): bytes

Read a file as raw bytes.

await sandbox.files.write(path, content): None

Write content to a file. Accepts str or bytes.

await sandbox.files.list(path="/"): list[EntryInfo]

List directory contents.

await sandbox.files.make_dir(path): None

Create a directory.

await sandbox.files.remove(path): None

Delete a file or directory.

await sandbox.files.exists(path): bool

Check if a path exists. Client-side wrapper — attempts a read and returns False on error.

EntryInfo


Pty

Accessed via sandbox.pty.

await sandbox.pty.create(cols=80, rows=24, on_output=None): PtySession

Create an interactive terminal session.

PtySession

recv() is Python-unique — a pull-based alternative to the on_output callback. Returns raw bytes.

Image

Fluent, immutable builder for declarative sandbox images. Each method returns a new Image instance.

Image.base(): Image

Start from the default OpenSandbox environment (Ubuntu 22.04, Python, Node.js, build tools).

Builder Methods

image.to_dict(): dict

Returns the image manifest as a plain dict.

image.cache_key(): str

Computes a deterministic SHA-256 hash of the manifest for cache lookups.

Snapshots

Standalone class — not a property on Sandbox.

Snapshots(api_key=..., api_url=...)

await snapshots.create(name, image, on_build_logs=None): dict

Create a pre-built snapshot from a declarative image.

await snapshots.list(): list[dict]

List all snapshots for the current organization.

await snapshots.get(name): dict

Get a snapshot by name.

await snapshots.delete(name): None

Delete a snapshot. Existing sandboxes created from it are not affected.

SnapshotInfo

Snapshot methods return dicts with these fields:

Secret Stores

await SecretStore.create(**kwargs)

Create a new secret store.
name
str
required
Store name (unique per organization).
egress_allowlist
list[str] | None
default:"None"
Allowed egress hosts (e.g. ["api.anthropic.com"]).
Returns: dict — Secret store info with id, name, egressAllowlist, etc.

await SecretStore.list(**kwargs)

List all secret stores.
Returns: list[dict]

await SecretStore.get(store_id, **kwargs)

Get a secret store by ID.
store_id
str
required
UUID of the secret store.
Returns: dict

await SecretStore.update(store_id, **kwargs)

Partial updates — only the fields you pass are changed.
store_id
str
required
UUID of the store to update.
name
str
default:""
New store name (empty = no change).
egress_allowlist
list[str] | None
default:"None"
New allowed egress hosts (None = no change).
Returns: dict

await SecretStore.delete(store_id, **kwargs)

Deletes the store and all its secrets. Running sandboxes are not affected.
Returns: None

await SecretStore.set_secret(store_id, name, value, **kwargs)

Set a secret in a store. Secrets are encrypted at rest. The value is never returned by the API.
Optionally restrict which hosts can receive this secret:
store_id
str
required
UUID of the secret store.
name
str
required
Secret name (used as the env var name in sandboxes).
value
str
required
Secret value (encrypted at rest, never returned by API).
allowed_hosts
list[str] | None
default:"None"
Restrict this secret to specific hosts only.
Returns: None

await SecretStore.list_secrets(store_id, **kwargs)

Returns secret metadata only. Values are never exposed.
Returns: list[dict]

await SecretStore.delete_secret(store_id, name, **kwargs)

Returns: None

Creating a Sandbox with a Secret Store

Pass the secret_store parameter to Sandbox.create() to inject the store’s secrets: