Skip to content

The client

The client is a typed async API over the sandboxd socket of Docker Sandboxes, which must be installed and running on the machine (see Installation): every operation of docker-sandbox, the Rust library it is built on, with Pydantic models for the payloads and one exception hierarchy for the errors. This page covers the connection: where the socket is, the deadlines, the API version, and the handles a client gives. The pages that follow take one domain each.

Connecting

connect() reaches the socket of the current user, asks the daemon for its health and checks its API version. It returns a Client, the entry point of every other handle.

docs_src/client/connect.py
import fastsandbox


async def main() -> None:
    client = await fastsandbox.connect(timeout=10, long_operation_timeout=600)
    health = client.health
    print(f"sbx {health.version}, API {health.api_version}, {health.status}")

    low, high = fastsandbox.SUPPORTED_API
    print(f"supported API: from {low} up to {high}, excluded")

    sandboxes = await client.sandboxes.list()
    print(f"{len(sandboxes)} sandboxes")


if __name__ == "__main__":
    fastsandbox.run(main())

A client holds no connection of its own: one is opened per request, so a program connects once and passes the client, or the handles it gives, where they are needed. It can be used as an async context manager, which releases nothing. Client.health is the Health the daemon reported when the client connected; client.daemon.health() asks again.

Where the socket is

Without a socket argument, the socket is the one the sbx CLI uses, so that a program and the CLI always talk to the same daemon:

  1. the path in the environment variable DOCKER_SANDBOXES_API (fastsandbox.SOCKET_ENV);
  2. $XDG_STATE_HOME/sandboxes/sandboxes/sandboxd/sandboxd.sock;
  3. the same path under $HOME/.local/state.

default_socket_path() gives that path, or None when none of these variables is set. When the daemon cannot be reached, connect() raises DaemonUnavailableError, a ConnectionError. fastsandbox info prints the socket, the version of the daemon and the API range of the library:

fastsandbox info

Deadlines

Every request has a deadline; past it, the call raises SandboxTimeoutError, a TimeoutError whose after attribute says how long it waited.

Argument Default Applies to
timeout 30 s ordinary requests
long_operation_timeout 15 min creations, image pulls, commands without a deadline of their own

A command can have its own deadline, shorter or longer: await sandbox.exec("make test", timeout=600) (see Commands).

The API version

The sandboxd API is private and changes with sbx releases. Each release of fastsandbox is checked against a range of API versions, SUPPORTED_API, a (minimum, maximum) pair whose maximum is excluded. version_policy says what connect() does with a daemon outside it:

version_policy Outside the supported range
"strict" (default) raises UnsupportedVersionError, with found and supported
"warn" logs a warning on the fastsandbox logger and connects
"ignore" connects

The warning names the version and the range: sandboxd speaks API 0.99.0, which this release of fastsandbox was not audited on (supported >=0.52.0, <0.53.0). Python prints it on standard error when the program configures no logging.

fastsandbox info connects with "warn", so that it reports a daemon the library does not support instead of failing: it prints that warning, then its report, and exits with status 0. Its api line says whether the version is in the range; outside it, connect() with its default policy, and so every task, raises UnsupportedVersionError.

The handles

A client gives one handle per domain of the daemon. Handles are cheap: each access builds a new one on the same client, and none checks anything until it is used.

Handle Domain Page
client.create(...) a new sandbox Sandboxes
client.sandboxes list, look up, create from a request Sandboxes
client.sandbox(name) one sandbox, by name Sandboxes, Commands, Files
client.policy the network policy Network and policy
client.images the images of the daemon Images
client.events, client.prompts the event and prompt streams Events and prompts
client.mcp the MCP gateway settings MCP gateway
client.daemon health, settings, log levels, disk usage The daemon
client.kits, client.package(kit) kit packages Kits

A whole sandbox, its rules, keys and MCP servers included, is also launched in one call from a SandboxSpec, spec.launch(client, name, keys) (see Sandbox specs).

From synchronous code

The client is async. fastsandbox.run() runs a coroutine from synchronous code, a script or a notebook cell, as asyncio.run does; it also runs the tasks of a FastSandbox application (see Running tasks).

fastsandbox.run(main())