AsyncSandbox is the async counterpart of Sandbox. Every method is a coroutine (async def) and must be awaited. It is the preferred choice when:
- You need to create or manage multiple sandboxes concurrently with
asyncio.gather. - Your application is built on an async framework (FastAPI, LangGraph, LiteLLM, etc.).
- You want to stream command output using
async fororasyncio.Queue.
When to use async
Class methods
AsyncSandbox.create()
Create a new sandbox and return a connected AsyncSandbox instance.
Sandbox.create().
Returns AsyncSandbox
AsyncSandbox.connect()
Connect to an existing sandbox by ID.
Sandbox.connect().
Returns AsyncSandbox
Instance methods
await sbx.kill()
Kill and destroy the sandbox.
bool
await sbx.is_running()
Check whether the sandbox is in the running state.
bool
await sbx.set_timeout()
Update the sandbox timeout.
int
required
New timeout in seconds.
None
await sbx.get_info()
Fetch the current metadata and state.
SandboxInfo
await sbx.get_metrics()
Retrieve resource usage metrics for a time range.
list[SandboxMetrics]
await sbx.pause()
Pause the sandbox.
None
await sbx.create_snapshot()
Create a sandbox snapshot.
SnapshotInfo
await sbx.snapshot()
Create a manual snapshot of this sandbox. Manual snapshots accumulate — every
call creates a new persistent checkpoint that survives sbx.kill(). Use
AsyncSandbox.restore() or
sbx.list_snapshots() to retrieve and fork from
them.
float | None
default:"None"
Per-request HTTP timeout in seconds.
Snapshot
await sbx.list_snapshots()
List all snapshots (periodic, pause, and manual) for this sandbox, newest first.
float | None
default:"None"
Per-request HTTP timeout in seconds.
list[Snapshot]
await AsyncSandbox.restore()
Restore a sandbox from a snapshot. The restored sandbox may run on a different
worker than the original. Returns a usable AsyncSandbox instance already
connected to the restored sandbox.
str
required
The sandbox to restore.
str | None
default:"None"
Specific snapshot to restore from. If omitted, the most recent snapshot is
used (preference order: pause > periodic > manual).
str | None
default:"$DECLAW_API_KEY"
API key override.
str | None
default:"$DECLAW_DOMAIN"
Domain override.
float | None
default:"None"
Per-request HTTP timeout in seconds.
AsyncSandbox
Command methods (inline on AsyncSandbox)
Unlike the synchronousSandbox, AsyncSandbox exposes command operations directly as methods rather than through a sub-module:
await sbx.run_command()
Run a command and return its result (or a handle if background=True).
str
required
Shell command to execute.
bool
default:"False"
When
True, returns an AsyncCommandHandle immediately without waiting for
the command to finish.dict[str, str] | None
default:"None"
Environment variables for the command.
str
default:"'user'"
Unix user to run the command as.
str | None
default:"None"
Working directory for the command.
Callable[[str], None] | None
default:"None"
Callback invoked for each stdout line after completion (foreground only).
Callable[[str], None] | None
default:"None"
Callback invoked for each stderr line after completion (foreground only).
float | None
default:"60"
Command execution timeout in seconds.
CommandResult | AsyncCommandHandle
await sbx.list_commands()
List all running processes in the sandbox.
list[ProcessInfo]
await sbx.kill_command()
Kill a running command by PID.
int
required
Process ID to kill.
bool
Filesystem methods (inline on AsyncSandbox)
await sbx.read_file()
Read a file’s content.
str
required
Absolute path inside the sandbox.
str
default:"'text'"
"text" returns a str; "bytes" returns a bytearray.str
default:"'user'"
Unix user context.
str | bytearray
await sbx.write_file()
Write content to a file.
str
required
Absolute path inside the sandbox.
str | bytes
required
Content to write.
WriteInfo
await sbx.list_files()
List directory entries.
str
required
Directory path.
int
default:"1"
Recursion depth.
1 lists only the immediate directory.list[EntryInfo]
Async context manager
__aexit__ calls await sbx.close() which releases the HTTP client. It does not kill the sandbox — call sbx.kill() explicitly.
AsyncCommandHandle
Returned bysbx.run_command(background=True). Allows you to wait for or kill the background process.