Skip to content

Sandbox specs

A whole sandbox described as data, as docker-sandbox-blueprint describes it: SandboxSpec, the keys it gives (KeySpec) and their values (Keys), the Launch that applies it and the Lease that owns the sandbox. What a launch did is a Report.

fastsandbox.SandboxSpec

SandboxSpec(native: SandboxSpec | None = None)

A whole sandbox described as data: all it receives but its name and the values of its keys.

Write it with chained methods, from SandboxSpec.agent(), SandboxSpec.base() or SandboxSpec.package(); each returns a new spec. One spec serves every sandbox of a job. SandboxSpec() names no base kit, for a launch on a package the program holds (launch_package).

Example

spec = ( ... SandboxSpec.agent(Agent.CLAUDE) ... .with_workspace("/srv/project") ... .with_network_deny("*.pastebin.com") ... .with_key(KeySpec.for_service("ANTHROPIC_API_KEY", "anthropic")) ... ) async with spec.launch(client, "job-42", Keys.from_env(spec)) as lease: ... await lease.sandbox.exec("git status")

native property

native: SandboxSpec

The handle of the native extension.

agent classmethod

agent(agent: Agent | str) -> SandboxSpec

A sandbox of the kit of agent, resolved as sbx create <agent> resolves it.

Raises:

Type Description
InvalidKitError

For an agent sbx does not know.

base classmethod

base(reference: str) -> SandboxSpec

A sandbox of the published kit reference, e.g. docker.io/sbx/shell:latest.

package classmethod

package(dir: str | PathLike[str]) -> SandboxSpec

A sandbox of the kit package of the directory dir, compiled by the CLI or saved.

Without mixins, network rules or MCP servers in the spec, the package is launched as it is; with them, they are composed after every kit of the package, its own mixins and their images kept. It keeps the arguments it was compiled with, its agent is the one its base kit declares, and it must suit the daemon (KitPackage.suits).

from_json classmethod

from_json(form: dict[str, Any]) -> SandboxSpec

The spec of its JSON form, the one of the crate; an unknown field is refused.

Raises:

Type Description
InvalidSpecError

For a form that is not one of a spec.

to_json

to_json() -> dict[str, Any]

The JSON form of the spec, as a program in another language writes it.

with_arg

with_arg(name: str, value: str) -> SandboxSpec

Gives the value value to the argument name of the base kit.

with_mixin

with_mixin(mixin: Mixin) -> SandboxSpec

Composes mixin, a mixin written in Python.

with_mixin_dir

with_mixin_dir(dir: str | PathLike[str]) -> SandboxSpec

Composes the mixin of the directory dir, read when the spec is checked or launched.

with_workspace

with_workspace(dir: str) -> SandboxSpec

Bind-mounts the host directory dir, by its absolute path, as the workspace.

with_cloned_workspace

with_cloned_workspace(repository: str) -> SandboxSpec

Runs the agent on a private clone of the Git repository at repository.

with_mount

with_mount(mount: Workspace) -> SandboxSpec

Bind-mounts a further host directory or file.

The daemon mounts further directories only beside a workspace: a spec with mounts and no workspace is refused when it is checked or launched.

with_network_allow

with_network_allow(*hosts: str) -> SandboxSpec

Allows hosts, host patterns, from the creation of the sandbox.

A refusal wins over an allowance, and an allowance adds to the global policy of the daemon: where that policy allows every host, under the Open preset of sbx, it restricts nothing, which the report says.

with_network_deny

with_network_deny(*hosts: str) -> SandboxSpec

Refuses hosts, host patterns, whatever else allows them, from the creation.

with_http_allow

with_http_allow(hosts: Iterable[str], methods: Iterable[str], path: str = EVERY_PATH) -> SandboxSpec

Allows the HTTP requests of methods (GET, …) to path (/v1/**) on hosts.

Applied before the start of the sandbox, the rule covers its startup commands, not its install commands.

with_http_deny

with_http_deny(hosts: Iterable[str], methods: Iterable[str], path: str = EVERY_PATH) -> SandboxSpec

Refuses the HTTP requests of methods to path on hosts, whatever else allows them.

Applied before the start of the sandbox, the rule covers its startup commands, not its install commands.

with_key

with_key(key: KeySpec) -> SandboxSpec

Gives the sandbox the API key key, whose value comes at launch.

with_agent_keys

with_agent_keys() -> SandboxSpec

Gives the sandbox every API key its agent takes, as its profile says (Agent.keys).

An optional key of its service for each variable the agent reads it from, so that whichever the program has goes to the proxy and the others are left out; Keys.from_env() then reads their values. Nothing for a published base kit or a package, whose agent is known once its kit is forged: give it to with_keys_of.

with_keys_of

with_keys_of(agent: Agent | str) -> SandboxSpec

Gives the sandbox every API key agent takes, as its profile says (Agent.keys).

As with_agent_keys, whatever the base kit of the spec: a published kit or a package the program knows to be of that agent. A variable the spec already gives a key keeps it. The variables the agent reads in clear (Agent.forwarded_env) stay out.

Raises:

Type Description
InvalidKitError

For an agent sbx does not know.

with_mcp

with_mcp(server: McpServer) -> SandboxSpec

Registers the MCP server server in the agent, for claude, copilot or opencode.

The token of a server of the host is drawn at each launch (Lease.mcp_token).

with_env

with_env(key: str, value: str) -> SandboxSpec

Sets the variable key of the sandbox to value, in clear: a secret is a key.

with_cpus

with_cpus(cpus: int) -> SandboxSpec

Sets the number of virtual CPUs.

with_memory

with_memory(memory: str) -> SandboxSpec

Sets the memory limit, in the notation of sbx (4g).

keep_on_failure

keep_on_failure() -> SandboxSpec

Keeps the sandbox when a launch fails after its creation, for a look inside.

check async

check(client: Client) -> None

Checks the spec against the rules of a launch and the kit policy of the daemon.

Nothing is pulled or written; the mixins of a directory are read.

Raises:

Type Description
InvalidSpecError

For a rule the spec breaks.

InvalidKitError

When a mixin or an MCP server is not one the forge takes.

KitForgeError

When the forge refuses the kit.

check_keys

check_keys(keys: Keys) -> None

Checks that keys gives a value to every key that is not optional, and to no other.

Raises:

Type Description
InvalidSpecError

For a key without a value that is not optional, or a value for a variable no key reads, most likely a misspelt one.

launch

launch(client: Client, name: str | None = None, keys: Keys | None = None, *, cache: KitCache | None = None) -> Launch

Launches the sandbox name from the spec, its keys given keys.

The steps, those of the crate: check the spec and the values of its keys; forge its kit, taken from cache when given (the sandboxes of a job then forge it once); draw a token for each MCP server of the host; prepare a ProxiedKey for each key; create the sandbox; start it, apply its HTTP rules and push its keys; return the lease. A failure once the sandbox exists removes it, unless the spec keeps it.

Parameters:

Name Type Description Default
client Client

The client of the daemon.

required
name str | None

The name of the sandbox; a unique one by default.

None
keys Keys | None

The values of the keys of the spec; none by default.

None
cache KitCache | None

The kit cache to take the kit from; the kit is forged for this launch alone by default.

None

Raises:

Type Description
InvalidSpecError

For a rule the spec or the keys break.

LaunchError

When the launch fails once the sandbox exists.

launch_package

launch_package(client: Client, name: str | None = None, keys: Keys | None = None, *, package: KitPackage) -> Launch

Launches the sandbox name as launch does, its base kit the package package.

A package the program holds, forged once in memory (KitForge.build, a KitCache without a directory) or compiled, with nothing written to a directory of its own. The spec names no base kit (SandboxSpec()), and the rules of SandboxSpec.package apply: the package suits the daemon, takes no arguments, and is launched as it is, or under the mixins, network rules and MCP servers of the spec, its own kits kept. The JSON form of a spec has no such base kit: a package in memory is a value of the program.

Raises:

Type Description
InvalidSpecError

For a rule the spec or the keys break, a base kit of its own among them.

LaunchError

When the launch fails once the sandbox exists.

fastsandbox.KeySpec dataclass

KeySpec(env: str, service: str | None = None, hosts: tuple[str, ...] = (), is_optional: bool = False)

Where an API key goes: the variable the agent reads it from, and the hosts it is written for.

Its JSON form is {"env", "service"} or {"env", "hosts"}, with "optional": true for an optional key. An optional key may be given no value at launch, and one of a service no kit of the sandbox declares is then left out rather than refused: the report of the launch names the keys left out, never their value.

Attributes:

Name Type Description
env str

The variable of the sandbox holding the placeholder of the key.

service str | None

The service of the kits the key is written for, if it is one.

hosts tuple[str, ...]

The hosts the key is written for, when they are written out.

is_optional bool

Whether the key is left out when it has no value or no kit declares its service, instead of failing the launch.

for_service classmethod

for_service(env: str, service: str) -> KeySpec

The key of the service service, read by the agent from the variable env.

It is written for the hosts the kits of the sandbox inject the key of that service into.

for_hosts classmethod

for_hosts(env: str, hosts: Iterable[str]) -> KeySpec

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

from_json classmethod

from_json(form: Mapping[str, Any]) -> KeySpec

The key of its JSON form.

optional

optional() -> KeySpec

The key, left out when it is given no value or no kit declares its service.

to_json

to_json() -> dict[str, Any]

The JSON form of the key, as a spec holds it.

fastsandbox.Keys

Keys(values: Mapping[str, str | SecretStr] | None = None)

The values of the API keys of a launch, by the variable of their KeySpec.

The values stay in the native extension: envs names the variables given one, and no method reads a value back.

The values values, by variable, each as text or a pydantic.SecretStr.

native property

native: Keys

The handle of the native extension.

envs property

envs: list[str]

The variables given a value.

from_env classmethod

from_env(spec: SandboxSpec) -> Keys

The values the environment of the process holds for the keys of spec.

Each is read from the variable of its key; a variable unset or empty gives no value, which an optional key does without.

with_key

with_key(env: str, value: str | SecretStr) -> Keys

The values, with value given to the key read from the variable env.

fastsandbox.Launch

Launch(spec: SandboxSpec, client: Client, name: str, keys: Keys, cache: KitCache | None, package: KitPackage | None = None)

A sandbox being launched from its spec.

Await it for its Lease, or use it as an async context manager that shuts the lease down on exit, unless it was detached.

Attributes:

Name Type Description
spec

The spec the sandbox is launched from.

name

The name of the sandbox: the one given to the launch, or a unique one.

fastsandbox.Lease

Lease(native: Lease, client: Client)

A sandbox launched from a spec, running while the lease holds its session.

Dropped without shutdown() or detach(), the lease closes its session: the sandbox stops about 30 s later and stays, until it is removed.

sandbox property

sandbox: Sandbox

The sandbox: run commands in it, read and write its files, publish a port.

created property

created: Created

The answer of its creation, the credentials the daemon withholds included.

agent property

agent: KitAgent | None

The agent the base kit declares: its program and the options of its sessions.

report property

report: Report

What the launch did, which never holds the value of a key or of a token.

held property

held: bool

Whether the lease still owns the sandbox: neither shut down nor detached.

mcp_token

mcp_token(server: str) -> str | None

The token of the MCP server of the host named server, drawn at launch.

The server checks it in the Authorization header of the requests the agent sends it; it is the sandbox's own. None for a server the spec does not register.

shutdown async

shutdown() -> None

Closes the session and removes the sandbox, its network rules and its keys with it.

The sandbox removed is the one the launch created, never another one given the same name since.

Raises:

Type Description
RuntimeError

When the lease was shut down or detached already.

SandboxError

When the daemon cannot remove the sandbox, which then stays.

detach

detach() -> tuple[Sandbox, Session]

Gives the sandbox and its session to the program, which keeps the sandbox.

The sandbox runs while the session is held, and the program removes it.

Raises:

Type Description
RuntimeError

When the lease was shut down or detached already.