Back to Microsandbox

Overview

docs/sdk/overview.mdx

0.5.13.6 KB
Original Source

The SDK embeds the microsandbox runtime directly into whatever application uses it. Sandbox.builder(name).create() spawns the VM as a child process. No daemon to install, no server to connect to.

Installation

<CodeGroup> ```bash Rust cargo add microsandbox ```
bash
npm install microsandbox
bash
pip install microsandbox
bash
go get github.com/superradcompany/microsandbox/sdk/go
</CodeGroup>

Quick start

Create a sandbox from a container image, run a command, print the output, and stop it.

<CodeGroup> ```rust Rust use microsandbox::{Sandbox, NetworkPolicy};

#[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let sb = Sandbox::builder("my-sandbox") .image("python") .memory(512) .cpus(2) .env("PYTHONDONTWRITEBYTECODE", "1") .volume("/app/src", |v| v.bind("./src").readonly()) .network(|n| n.policy(NetworkPolicy::public_only())) .create() .await?;

let output = sb.exec("python", ["-c", "print('Hello, World!')"]).await?;
println!("{}", output.stdout()?);

sb.stop().await?;
Ok(())

}


```typescript TypeScript
import { NetworkPolicy, Sandbox } from "microsandbox";

await using sb = await Sandbox.builder("my-sandbox")
    .image("python")
    .memory(512)
    .cpus(2)
    .env("PYTHONDONTWRITEBYTECODE", "1")
    .volume("/app/src", (m) => m.bind("./src").readonly())
    .network((n) => n.policy(NetworkPolicy.publicOnly()))
    .create();

const output = await sb.exec("python", ["-c", "print('Hello, World!')"]);
console.log("Output:", output.stdout());
python
import asyncio
from microsandbox import Network, Sandbox, Volume

async def main():
    sb = await Sandbox.create(
        "my-sandbox",
        image="python",
        memory=512,
        cpus=2,
        env={"PYTHONDONTWRITEBYTECODE": "1"},
        volumes={
            "/app/src": Volume.bind("./src", readonly=True),
        },
        network=Network.public_only(),
    )

    output = await sb.exec("python", ["-c", "print('Hello, World!')"])
    print("Output:", output.stdout_text)

    await sb.stop()

asyncio.run(main())
go
package main

import (
    "context"
    "fmt"
    "log"

    m "github.com/superradcompany/microsandbox/sdk/go"
)

func main() {
    ctx := context.Background()
    if err := m.EnsureInstalled(ctx); err != nil {
        log.Fatal(err)
    }

    sb, err := m.CreateSandbox(ctx, "my-sandbox",
        m.WithImage("python"),
        m.WithMemory(512),
        m.WithCPUs(2),
        m.WithEnv(map[string]string{"PYTHONDONTWRITEBYTECODE": "1"}),
        m.WithMounts(map[string]m.MountConfig{
            "/app/src": m.Mount.Bind("./src", m.MountOptions{Readonly: true}),
        }),
        m.WithNetwork(m.NetworkPolicy.PublicOnly()),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer func() {
        _, _ = sb.StopAndWait(context.Background())
        _ = sb.Close()
    }()

    out, err := sb.Exec(ctx, "python", []string{"-c", "print('Hello, World!')"})
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Output:", out.Stdout())
}
</CodeGroup>

The SDK reference is split by language - choose Rust SDK, TypeScript SDK, Python SDK, or Go SDK for the full API. For low-level protocol integrations, see the Agent Client references for Rust, TypeScript, Python, and Go. See also error handling.