TypeScript SDK
sandboxer-sdk — a typed client for Node 20+, Bun, Deno, edge runtimes and browsers, with a low-level method for every API operation.
Install#
npm install sandboxer-sdk
# or: pnpm add sandboxer-sdk · bun add sandboxer-sdkThe package has no runtime dependencies. For sandbox.pty() in Node, also install ws; browsers use the native WebSocket.
Note On an older version, API responses carry a
Sandboxer-Client-Warningheader (for example, "sandboxer-sdk 0.1.2 for TypeScript is out of date; runnpm i sandboxer-sdk@latest(0.1.3)") andSandboxer-Client-Latestwith the newest version. Runnpm i sandboxer-sdk@latestto update.
Configure#
import { Sandboxer } from "sandboxer-sdk";
export const sandboxer = new Sandboxer({
apiKey: process.env.SANDBOXER_API_KEY, // default: SANDBOXER_API_KEY, then SANDBOXER_TOKEN
baseUrl: "https://sandboxer.com/api/v1", // default: SANDBOXER_API_URL, then this URL
org: "team_abc123", // bill an organization wallet (X-Sandboxer-Org)
timeoutMs: 60_000, // per attempt
maxRetries: 3,
});Every option is optional. Inside a sandbox, SANDBOXER_TOKEN and SANDBOXER_API_URL are already set, so new Sandboxer() works with no arguments.
Create, use, stop#
import { Sandboxer } from "sandboxer-sdk";
const sandboxer = new Sandboxer();
const sandbox = await sandboxer.create({ type: "default", ttlSeconds: 1800, name: "pr-1842" }); // waits until ready
// region: "us-east" or "eu-central"; without it, the region nearest you (/docs/regions)
try {
const run = await sandbox.exec("git clone https://github.com/acme/web && cd web && npm ci && npm test", { timeoutSeconds: 600 });
console.log(run.exitCode, run.stdout);
} finally {
await sandbox.stop(); // archive; sandbox.resume() brings it back
}exec takes a shell string or an argv array (quoted for you), plus cwd, env and timeoutSeconds (1–600, default 30). Pass check: true to throw CommandError on a non-zero exit.
Common calls#
| Task | Method |
|---|---|
| List / get | sandboxer.list({ state: ["ready", "idle", "running"] }), sandboxer.get(id) |
| Lifecycle | stop, resume, fork, snapshot(name), delete, recover, share |
| Auto-stop and names | extend(ttlSeconds), update({ name, subdomain }) |
| Commands | exec, execStream, start, commandStatus, attach, kill, history, processes |
| Files | files.read, files.readBytes, files.write, files.download |
| Hosting / SSH / desktop | host(port), sshKey(publicKey), desktop(), pty() |
| Agents | prompt, events, conversations, steer, interrupt |
| Usage | usage(), snapshots(), latestSnapshot() |
Everything else is on sandboxer.api: one method per operationId in the API reference, taking one flat object (path, query and body fields side by side) and returning the parsed response.
const { api } = sandboxer;
await api.me();
await api.update({ sandboxId: sandbox.id, ttlSeconds: 7200 });
const hook = await api.createWebhook({ url: "https://example.com/hooks", events: ["sandbox.ready"] });Stream a command#
const run = await sandbox.execStream("npm test", { onStdout: (d) => process.stdout.write(d) });
for await (const frame of run) {
if (frame.type === "stderr") process.stderr.write(frame.data!);
}
const { exitCode } = await run.wait();Background work#
const build = await sandbox.start("npm run build", { cwd: "web" });
for await (const frame of build.attach()) { // reconnects transparently
if (frame.type === "stdout") process.stdout.write(frame.data!);
}
const status = await build.status(); // or sandbox.commandStatus(build.processId)
await build.kill(); // or sandbox.kill(processId)
const done = await (await sandbox.start("./ci.sh")).wait(); // collect output until it exits
console.log(done.exitCode, done.stdout);Prompt an agent#
const run = await sandbox.prompt("Add a /health route with a test", { provider: "claude-code" });
for await (const ev of run.events()) { // this prompt's events, until it is done
if (ev.type === "response") console.log((ev.data as { content?: string }).content);
}
const final = await run.wait(); // status: finished | failed | interrupted
console.log(final.status, await run.output()); // the last final response textrun.steer("...") adds an instruction to the running turn; run.interrupt() stops it.
Fork for parallel attempts#
const attempts = await Promise.all(
["approach A", "approach B", "approach C"].map(async (hint) => {
const fork = await base.fork({ ttlSeconds: 3600 }); // copy-on-write, waits until ready
return fork.prompt(`Fix issue #42 using ${hint}`, { provider: "codex" });
}),
);Files#
await sandbox.files.write("notes/a.txt", "hello\n"); // text (UTF-8)
await sandbox.files.write("img/logo.png", pngBytes); // Uint8Array / ArrayBuffer
const text = await sandbox.files.read("notes/a.txt");
const bytes = await sandbox.files.readBytes("img/logo.png"); // up to 5 MiB
const big = await sandbox.files.download("dist/bundle.tar.gz"); // up to 50 MiBPaths are relative to /home/user; absolute paths under /home/user or /tmp work too.
Errors#
Every API failure throws a SandboxerError with the error envelope already parsed. Subclasses by status include BadRequestError, AuthenticationError, PaymentRequiredError, PermissionDeniedError, NotFoundError, ConflictError and RateLimitError; network problems throw APIConnectionError or APITimeoutError.
import { SandboxerError } from "sandboxer-sdk";
try {
await sandboxer.create({ type: "xlarge" });
} catch (e) {
if (e instanceof SandboxerError && e.code === "machine_class_plan_required") console.log("Upgrade to use xlarge");
else throw e; // e.status, e.message, e.details, e.requestId, e.retryable
}Retries, idempotency and organizations#
Requests the API marks as safe are retried automatically with jittered backoff. Create and fork send a generated Idempotency-Key; pass your own to make a job retry across restarts:
await sandboxer.create({ type: "small", idempotencyKey: `job-${jobId}` });
await sandboxer.api.sandboxes({ state: "ready" }, { org: "acme" }); // per-request org overrideCommonJS#
const { Sandboxer } = require("sandboxer-sdk");