Sandbox class is the main entry point to OpenSandbox. It supports both manual lifecycle management and async context managers.
Creating a Sandbox
Sandbox.create(**kwargs)
Creates a new sandbox and returns a connected Sandbox instance.
Container template to use.
Sandbox time-to-live in seconds.
API key. Falls back to
OPENSANDBOX_API_KEY env var.API base URL. Falls back to
OPENSANDBOX_API_URL env var, then https://app.opensandbox.ai.Environment variables injected into the sandbox.
Arbitrary key-value metadata attached to the sandbox.
Sandbox
Async Context Manager
The recommended way to use sandboxes in Python — the sandbox is automatically killed when the block exits:Connecting to an Existing Sandbox
Sandbox.connect(sandbox_id, **kwargs)
Connects to a running sandbox by ID.
The ID of the sandbox to connect to.
API key override.
API URL override.
Sandbox
Properties
| Property | Type | Description |
|---|---|---|
sandbox_id | str | Unique identifier for the sandbox |
status | str | Current status: "running", "stopped", or "error" |
template | str | Template used to create the sandbox |
files | Filesystem | Filesystem operations |
commands | Commands | Command execution |
pty | Pty | PTY terminal access |
Instance Methods
await sandbox.kill()
Stops and destroys the sandbox.
None
await sandbox.is_running()
Checks if the sandbox is still alive.
bool
await sandbox.set_timeout(timeout)
Updates the sandbox timeout. Resets the TTL countdown.
New timeout in seconds.
None
sandbox.close()
Closes the underlying HTTP client connections without killing the sandbox.
None