Skip to content

About

TypeScript SDK for Pyo Sandbox

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

@pyodev/sandbox

The TypeScript SDK for Pyo Sandbox. A sandbox is an isolated machine made on demand through an API, for AI agents, code interpreters, previews and CI jobs. Create one, run commands, read and write files, reach its ports and throw it away. Sandboxes run on your own sandbox servers, on Firecracker where the server has KVM and on gVisor elsewhere.

import { Sandbox } from "@pyodev/sandbox";

const sbx = await Sandbox.create({ ttl: "1h" });
const out = await sbx.exec("node -v");
await sbx.files.write("/app/index.js", "console.log('hi')");
const url = await sbx.ports.open(3000);
await sbx.pause();

Without a template, a sandbox starts from base, Pyo's built-in template: Ubuntu 24.04 with git, Python 3 with pip and uv, Node.js LTS with npm and pnpm, Bun, Deno, build tools, the GitHub and Docker CLIs, Claude Code, Codex and opencode. A sandbox server makes base the first time it's used there, so the first Sandbox.create() on a server takes longer. Later ones start at once.

Install

npm install @pyodev/sandbox

It has no runtime dependencies. It works in Node 20 and later and in Bun, with the global fetch and WebSocket.

Node 20 has no global WebSocket. Plain exec and files work without one. For streaming output, terminals and port connections, install ws and pass it in:

import WebSocket from "ws";
const sbx = await Sandbox.create({ WebSocket });

Setup

The SDK reads these when you don't pass them:

Variable Option What it is
PYO_URL baseUrl Your Pyo, like https://pyo.example.com, or https://cloud.pyo.dev on Pyo Cloud
PYO_API_KEY apiKey A Pyo API key with sandbox access
PYO_TEAM teamId The team the sandboxes belong to. create may leave it out when the API key has one team.

The same code works on Pyo Cloud and on a Pyo you run yourself. Only PYO_URL and the key change.

Pyo is the control plane only. It creates sandboxes, signs tokens and handles pause, resume, lifetime and delete. Commands, terminals and files go straight from your code to the sandbox server with a short-lived token, so Pyo is never in the hot path. The sandbox server answers on your tailnet, so the code that uses a sandbox runs on a machine in your tailnet.

Sandboxes

const sbx = await Sandbox.create({
  name: "checkout tests",  // for people. Not unique. Made up by default, like "quiet-river".
  template: "node-22",     // a sandbox template's name or id. "base" by default.
  server: "sandbox-1",     // a sandbox server's name or id. Pyo picks one by default.
  cpus: 2,                 // 1 by default, 2 for "desktop"
  memoryMib: 1024,         // 512 by default, 2048 for "desktop"
  diskMib: 4096,           // 2048 by default, sparse
  ttl: "1h",               // "30m", "1h", "7d" or seconds, up to 365 days. "none" runs it until you delete it. 5 minutes by default.
  env: { API_URL: "https://example.com" },
  network: "none",         // "internet" by default
  isolation: "vm",         // "auto" by default. "vm" needs Firecracker.
  labels: { job: "ci-1234" },
  projectId: "prj_123",
});

sbx.id;          // "sbx_…"
sbx.info;        // the sandbox as Pyo shows it: state, runtime, sizes, expiresAt…

Reconnect to a sandbox that already exists:

const sbx = await Sandbox.connect("sbx_abc123");

List a team's sandboxes:

const live = await Sandbox.list();                 // not stopped or failed
const all = await Sandbox.list({ state: "all" });  // the latest 500
const found = await Sandbox.list({ search: "nightly" });  // by name or id, ignoring case

// The team's sandbox servers, each with its runtime: "firecracker" or "gvisor".
const servers = await Sandbox.servers();

Lifecycle calls go to Pyo:

await sbx.rename("nightly build");  // the id stays the same
await sbx.extend("2h");  // it now ends two hours from now
await sbx.pause();       // keeps memory and files, drops connections
await sbx.resume();
await sbx.delete();
await sbx.getInfo();     // fresh from Pyo

Commands

const r = await sbx.exec("ls -la /app");
r.stdout; r.stderr; r.exitCode; r.timedOut; r.durationMs;

await sbx.exec(["python3", "-c", "print(1 + 1)"]);   // an argv runs as is
await sbx.exec("npm test", { cwd: "/app", env: { CI: "1" }, timeoutMs: 300_000 });
await sbx.exec("wc -c", { stdin: "some input" });

A string runs with /bin/sh -c. A non-zero exit code is not an error. Commands get the sandbox's env, with env on top. They time out after 60 seconds unless you say otherwise, and keep at most 8 MiB of each output (truncated says when more was dropped). The sandbox server enforces both.

Pass onStdout or onStderr to get output as it arrives, and an AbortSignal to stop a command:

const ac = new AbortController();
await sbx.exec("npm install", {
  onStdout: (d) => process.stdout.write(d),
  onStderr: (d) => process.stderr.write(d),
  signal: ac.signal,
});

spawn starts a command and returns at once, for servers and interactive input. It has no time limit or output cap unless you pass timeoutMs:

const proc = await sbx.spawn("python3 -i", { stdin: true, onStdout: console.log });
proc.pid;
proc.write("print('hi')\n");
proc.end();                   // closes stdin
proc.kill();                  // SIGKILL, or pass a signal number
const result = await proc.wait();

Terminals

const pty = await sbx.pty({ cols: 120, rows: 30, onData: (bytes) => term.write(bytes) });
pty.write("ls\r");
pty.resize(160, 40);
await pty.wait();

cmd picks the program. The user's login shell runs by default.

Files

await sbx.files.mkdir("/app/src");
await sbx.files.write("/app/src/index.js", "console.log('hi')", { mode: 0o644 });
await sbx.files.write("/app/logo.png", bytes);      // Uint8Array, ArrayBuffer or Blob
const text = await sbx.files.read("/app/src/index.js");
const data = await sbx.files.readBytes("/app/logo.png");
const entries = await sbx.files.list("/app");        // name, path, kind, size, mode, modified
const st = await sbx.files.stat("/app/src/index.js");
await sbx.files.exists("/app/tmp");
await sbx.files.remove("/app/tmp", { recursive: true });

Each call moves at most 64 MiB.

Ports

ports.open opens a port on the sandbox server's edge proxy and returns its URL, like https://3000-sbx-abc.sbx.example.com:

await sbx.spawn("npx serve -l 3000");
const url = await sbx.ports.open(3000);                       // public
const admin = await sbx.ports.open(8080, { public: false });  // needs a sandbox token
await sbx.ports.close(8080);                                  // stops serving it at its URL

A private port needs the sandbox token on each request, as X-Pyo-Token, or once as ?pyo_token= in a browser. sbx.info.ports lists the open ports with their URLs. A server without an edge proxy refuses with code conflict.

ports.connect opens a TCP connection to a port inside the sandbox through the sandbox server, without any network in the sandbox:

const conn = await sbx.ports.connect(3000, { onData: (d) => process.stdout.write(d) });
conn.write("GET / HTTP/1.0\r\nHost: localhost\r\n\r\n");
conn.end();
await conn.closed;

ports.webUrl gives a port's web page over HTTPS on your tailnet, at the address Pyo gives, like https://sandboxes.tail1234.ts.net/v1/sandboxes/sbx_abc/web/3000/. It needs Tailscale set up in Pyo, not an edge domain, and opens only for people on your tailnet. The URL carries the sandbox token, so give it only to someone who may use the sandbox:

const page = await sbx.ports.webUrl(3000);

Desktop

The desktop template is base with a desktop and a browser. Its sandboxes get 2 CPUs and 2 GiB of memory unless you ask for more. desktopUrl gives its screen to open in a browser, over HTTPS on your tailnet:

const desk = await Sandbox.create({ template: "desktop", ttl: "1h" });
const screen = await desk.desktopUrl();                     // the desktop
const browser = await desk.desktopUrl({ app: "chromium" }); // with the browser in front

Programs in the sandbox drive the browser over the Chrome DevTools Protocol at 127.0.0.1:9222, with Playwright or Puppeteer.

Tokens

The token for the sandbox server lasts an hour. The SDK asks Pyo for a new one (POST /sandboxes/{id}/token) five minutes before it runs out, and once more if the server refuses one. Set tokenRefreshMarginMs to change the margin, or call sbx.refresh() yourself.

A client without an API key, like a browser, can use a connection your backend made and passed on:

// On your backend
const sbx = await Sandbox.create({ template: "node-22" });
const conn = await sbx.connection(); // { url, sandboxId, token, expiresAt, webUrl }

// In the client
const sbx = Sandbox.fromConnection(conn, {
  refreshToken: () => fetch(`/api/sandboxes/${conn.sandboxId}/token`).then((r) => r.json()),
});

Errors

Every error is a PyoSandboxError with a plain message:

try {
  await sbx.files.read("/missing");
} catch (e) {
  if (e instanceof PyoSandboxError) {
    e.source;  // "pyo", "server", "sandbox" or "client"
    e.code;    // "notFound", "forbidden", "permissionDenied", "conflict", "unsupported", "noCapacity"…
    e.status;  // the HTTP status, when there was one
  }
}

forbidden from the sandbox server means the token doesn't allow the call. permissionDenied means the sandbox itself refused, like a file its user can't read, so a new token won't help.

Tools for AI agents

Ready-made tools let a model use a sandbox: exec, read_file, write_file, list_files and open_port. Neither the Anthropic SDK nor the AI SDK is a dependency.

Claude API

pyoSandboxTools gives tool definitions in the Messages API shape and runs the tool_use blocks Claude sends back. Pass a sandbox, or a function that makes one on the first tool call.

import Anthropic from "@anthropic-ai/sdk";
import { Sandbox, pyoSandboxTools } from "@pyodev/sandbox";

const client = new Anthropic();
let sbx: Sandbox | undefined;
const toolkit = pyoSandboxTools(async () => (sbx ??= await Sandbox.create({ template: "node-22", ttl: "30m" })));

const messages: Anthropic.MessageParam[] = [
  { role: "user", content: "Write a Node script that prints the first 20 primes, run it and tell me the output." },
];

try {
  while (true) {
    const res = await client.messages.create({
      model: "claude-opus-5-5",
      max_tokens: 16000,
      tools: toolkit.tools,
      messages,
    });
    messages.push({ role: "assistant", content: res.content });
    if (res.stop_reason === "pause_turn") continue;
    if (res.stop_reason !== "tool_use") {
      for (const b of res.content) if (b.type === "text") console.log(b.text);
      break;
    }
    // Every tool call at once, and all results back in one message.
    messages.push({ role: "user", content: await toolkit.handleAll(res.content) });
  }
} finally {
  await sbx?.delete();
}

toolkit.handle(block) runs one call. A failed tool comes back as a tool_result with is_error: true, so the model can see what went wrong and try again. Options: include picks the tools, maxOutputChars, maxFileChars and maxTimeoutMs keep results small. examples/claude-agent.ts adds refusal fallbacks and a turn limit.

Vercel AI SDK

import { anthropic } from "@ai-sdk/anthropic";
import { generateText, jsonSchema, stepCountIs } from "ai";
import { Sandbox, pyoSandboxAiTools } from "@pyodev/sandbox";

const sbx = await Sandbox.create({ template: "python-3.13" });
const { text } = await generateText({
  model: anthropic("claude-opus-5-5"),
  tools: pyoSandboxAiTools(sbx, { jsonSchema }),
  stopWhen: stepCountIs(20),
  prompt: "How many lines does /etc/passwd have in the sandbox?",
});

Pass the AI SDK's own jsonSchema helper, since this package doesn't import ai. For other frameworks, sandboxToolSpecs has the plain names, descriptions and JSON schemas.

How it talks

  • Pyo (/api/v1/sandboxes…, Authorization: Bearer pyo_…): create, connect, list, rename, pause, resume, extend, delete, tokens, opening and closing ports, and the team's sandbox servers.
  • The sandbox server (connection.url, Authorization: Bearer <sandbox token>): plain exec and files over HTTP under /v1/sandboxes/{id}.
  • The sandbox's WebSocket (/v1/sandboxes/{id}/connect): streaming commands, spawn, terminals and port connections. The token goes as the subprotocol pyo.token.<token>, next to pyo, so it stays out of URLs. A refused connection closes with code 4000 plus the HTTP status (4401, 4403…) and the reason. A terminal keeps working after its token runs out. It speaks envd's frame format, one frame per binary message: [len u32 BE][kind u8][stream u32 BE][payload]. envd sends Hello on stream 0 first, and the SDK picks the newest protocol version both sides speak. Each command gets its own WebSocket, closed when it ends. The codec is exported as wire for other tools.

Development

bun install
npm run typecheck
npm test            # vitest, against a mock Pyo and a mock sandbox server
npm run test:bun    # the same tests under Bun
npm run build       # ESM, CJS and types in dist/

The mock sandbox server speaks the real frame format and runs commands as real processes.

npm run test:e2e runs the SDK against a real pyo-sandbox daemon that boots a Firecracker microVM on this machine, without root. It needs a /dev/kvm you can open and the daemon, pyo-envd, Firecracker and a guest kernel from a pyo-sandbox checkout (see the top of test/e2e.test.ts). It skips when any of them is missing.

License

Apache-2.0, like Pyo.

About

TypeScript SDK for Pyo Sandbox

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages