Skip to content

Client

The entry point of the client: connect() opens a Client on the socket of the daemon, and Client.create() returns a Creation, awaited or used as an async context manager.

fastsandbox.connect async

connect(socket: str | PathLike[str] | None = None, *, timeout: float | None = None, long_operation_timeout: float | None = None, version_policy: VersionPolicy = 'strict') -> Client

Connects to the daemon.

Parameters:

Name Type Description Default
socket str | PathLike[str] | None

The socket path; DOCKER_SANDBOXES_API, then the state directory of sbx, by default.

None
timeout float | None

Deadline of ordinary requests, in seconds.

None
long_operation_timeout float | None

Deadline of long operations (creation, image pulls), in seconds.

None
version_policy VersionPolicy

What to do with an API version this release was not audited on: strict refuses it, warn logs a warning on the fastsandbox logger and connects, ignore connects.

'strict'

Raises:

Type Description
DaemonUnavailableError

When the daemon cannot be reached.

UnsupportedVersionError

When its API version is not supported and version_policy is strict.

fastsandbox.Client

Client(native: Client, *, kits: KitCache | None = None)

A connection to the daemon: the entry point of every other handle.

It can be used as an async context manager; closing it releases nothing, connections are opened per request.

native property

native: Client

The handle of the native extension.

socket_path property

socket_path: Path

The socket the client talks to.

health property

health: Health

The health the daemon reported when the client connected.

kits property

kits: KitCache

The kit cache create takes packages from: the one of the process by default.

daemon property

daemon: Daemon

The daemon: health, settings, log levels, disk usage.

sandboxes property

sandboxes: Sandboxes

The collection of sandboxes.

images property

images: Images

The images of the daemon.

events property

events: Events

The event stream of the daemon.

policy property

policy: Policy

The network policy.

mcp property

mcp: Mcp

The MCP gateway settings.

prompts property

prompts: Prompts

The approval prompts of the daemon.

sandbox

sandbox(name: str) -> Sandbox

The handle of the sandbox name; nothing is checked until it is used.

package async

package(kit: Kit | KitPackage | str) -> KitPackage

The package of kit.

A kit is forged without the sbx CLI on first use, or compiled by it for a cache given the CLI (see KitCache); a package is returned as is.

create

create(name: str | None = None, *, kit: Kit | KitPackage | str | None = 'shell', request: CreateRequest | None = None, workspace: str | PathLike[str] | None = None, clone: bool = False, workspaces: Sequence[Workspace] = (), env: Mapping[str, str] | None = None, ports: Sequence[PortBinding] = (), cpus: int | None = None, memory: str | None = None, template: str | None = None, pull_policy: PullPolicy | None = None) -> Creation

Creates a sandbox.

Await the result to get the sandbox, which stays until deleted; or use it as an async context manager, which deletes the sandbox on exit.

Example

async with client.create(kit="shell") as sandbox: ... async with sandbox.running(): ... await sandbox.exec("uname -a")

Parameters:

Name Type Description Default
name str | None

The sandbox name; a unique one by default.

None
kit Kit | KitPackage | str | None

The kit, its package made on first use by the kit cache of the client (Client.kits): forged without the sbx CLI, or compiled by it for a cache given the CLI; None to create from request alone, e.g. with a template.

'shell'
request CreateRequest | None

A full creation request; the other arguments adjust it.

None
workspace str | PathLike[str] | None

A host directory mounted as workspace, at its own path.

None
clone bool

The agent works on a private clone of the Git repository of workspace instead of the directory itself.

False
workspaces Sequence[Workspace]

Further directories or files to mount.

()
env Mapping[str, str] | None

Environment variables of the sandbox.

None
ports Sequence[PortBinding]

Ports published from the start.

()
cpus int | None

Number of virtual CPUs.

None
memory str | None

Memory limit in sbx notation, e.g. 4g.

None
template str | None

Image to start from, e.g. one saved with Sandbox.save.

None
pull_policy PullPolicy | None

When to pull kit images.

None

fastsandbox.Creation

Creation(client: Client, name: str, kit: Kit | KitPackage | str | None, request: CreateRequest | None, overrides: dict[str, Any], workspaces: list[Workspace], env: dict[str, str], ports: list[PortBinding])

A sandbox being created.

Await it, or use it as an async context manager that deletes the sandbox on exit, by its identifier: a sandbox another program created since under the same name stays.

Attributes:

Name Type Description
name

The name of the sandbox.

created Created | None

What the daemon answered once the sandbox exists (its identifier, the credentials it withholds), None before.

fastsandbox.ProxiedKey

ProxiedKey(env: str, hosts: Iterable[str], value: str | SecretStr)

An API key the proxy of a sandbox writes in place of its placeholder.

The key is written in the requests to its hosts only, whatever the network rules of the sandbox allow; the agent sends the content of its variable in a header, as an API client does with a key. Its value is never read back.

The key value, read by the agent from the variable env, for hosts.

Its placeholder is generated, under the prefix the crate gives every placeholder. value may be a pydantic.SecretStr, passed on without being exposed in between.

Raises:

Type Description
InvalidRequestError

For a variable that is not one, or no host.

native property

native: ProxiedKey

The handle of the native extension.

env property

env: str

The variable of the sandbox holding the placeholder.

hosts property

hosts: list[str]

The hosts the key is written for.

placeholder property

placeholder: str

What the sandbox holds in place of the key.

for_service classmethod

for_service(package: KitPackage, service: str, env: str, value: str | SecretStr) -> ProxiedKey

The key of the service service, for the hosts the kits of package inject it into.

For anthropic and the claude kit, api.anthropic.com.

Raises:

Type Description
InvalidRequestError

When no kit of the package declares the service, or for a variable that is not one.

fastsandbox.default_socket_path

default_socket_path() -> Path | None

fastsandbox.SOCKET_ENV module-attribute

SOCKET_ENV: str = _native.SOCKET_ENV

The environment variable that names the socket of the daemon, DOCKER_SANDBOXES_API.

connect and default_socket_path read it first, before the state directory of sbx.

fastsandbox.SUPPORTED_API module-attribute

SUPPORTED_API: tuple[str, str] = _native.SUPPORTED_API

The API versions of the daemon this release supports: the minimum, included, and the maximum, excluded.

("0.52.0", "0.53.0") is >=0.52.0, <0.53.0: connect refuses a daemon outside it, unless its version_policy says otherwise.