Back to Microsandbox

Overview

docs/sdk/overview.mdx

0.5.42.5 KB
Original Source

The SDK embeds the microsandbox runtime directly into your application. Creating a sandbox spawns a local VM as a child process; there is no daemon to install and 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>

Small example

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

<CodeGroup> ```rust Rust use microsandbox::Sandbox;

#[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let sb = Sandbox::builder("hello") .image("python") .create() .await?;

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

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

}


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

await using sb = await Sandbox.builder("hello")
    .image("python")
    .create();

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

async def main():
    sb = await Sandbox.create(
        "hello",
        image="python",
    )

    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()
    sb, err := m.CreateSandbox(ctx, "hello", m.WithImage("python"))
    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>

Reference

Choose the SDK for your language:

For low-level protocol integrations, see the Agent Client references for Rust, TypeScript, Python, and Go. See also error handling.