Skip to content

Choose your API

fastsandbox has three ways to get a sandbox, and one way to change what a sandbox holds. They are layers, not alternatives that exclude each other: a task works with the client inside its run, and every run of a task is launched from a sandbox spec.

direction: down
task: "FastSandbox\na function run in a fresh sandbox per call"
spec: "SandboxSpec\na sandbox described as data"
client: "Client\nevery route of the daemon"
daemon: sandboxd {shape: hexagon}
kits: "Kits and mixins\nwhat a sandbox holds"
task -> spec
task -> client
spec -> client
client -> daemon
kits -> client {style.stroke-dash: 3}
kits -> spec {style.stroke-dash: 3}
kits -> task {style.stroke-dash: 3}
You want to Use You get
Drive a sandbox step by step, react to what each step returns, reach any route of the daemon Client Imperative control. You own the sandbox: async with deletes it, or you delete it yourself.
Describe a sandbox once, check it before anything exists, launch many alike, or hand it to a program in another language as JSON SandboxSpec A declaration checked by the rules of a launch. A Lease owns each sandbox and removes it with its rules and its keys.
Run a function in a fresh sandbox at each call, with validated inputs and result, from Python or the command line, a coding agent included FastSandbox Orchestration. Each run launches its sandbox from a spec and removes it at the end.
Add tools, files, configuration or allowed hosts to what a sandbox starts with Kits and mixins A composition given as kit= to any of the three.

One goal, three ways

Each program below runs echo hello in a sandbox of the shell kit that may not reach example.org, prints hello, and removes the sandbox.

hello_client.py
import fastsandbox


async def main() -> None:
    client = await fastsandbox.connect()
    async with client.create(kit="shell") as sandbox, sandbox.running():
        await sandbox.deny_hosts("example.org")
        output = await sandbox.exec(["echo", "hello"])
        print(output.stdout, end="")


if __name__ == "__main__":
    fastsandbox.run(main())
hello_spec.py
import fastsandbox
from fastsandbox import SandboxSpec

spec = SandboxSpec.base("docker.io/sbx/shell:latest").with_network_deny("example.org")


async def main() -> None:
    client = await fastsandbox.connect()
    async with spec.launch(client) as lease:
        output = await lease.sandbox.exec(["echo", "hello"])
        print(output.stdout, end="")


if __name__ == "__main__":
    fastsandbox.run(main())
hello_task.py
import fastsandbox
from fastsandbox import Context, FastSandbox, Network

app = FastSandbox("choose")


@app.task(kit="shell", network=Network.deny("example.org"))
async def hello(ctx: Context) -> str:
    """Says hello from a sandbox."""
    output = await ctx.sandbox.exec(["echo", "hello"])
    return output.stdout.strip()


if __name__ == "__main__":
    print(fastsandbox.run(hello))

What differs

Client SandboxSpec FastSandbox
The network rule Added once the sandbox runs, by a call Forged into the kit: in force from the creation Forged into the kit, as for a spec
Checked before the creation The host pattern, by the call that adds it The whole spec: kit, rules, mounts, keys The inputs, then the spec of the run
A session Held by sandbox.running() Held by the lease Held for the whole run
Who removes the sandbox The async with block, or your call to delete() The lease, when its block ends The run, when the function ends
Typed inputs and result No No Validated from the signature
Command line No No fastsandbox run

Start with FastSandbox when the work fits in a function: it holds the lifecycle for you. Take the client for tooling around the daemon, or for anything a task does not express; the client is also there inside a run, as ctx.sandbox and ctx.client. Take a spec when the same sandbox is launched many times, or described by another program.

Next