CodeSandbox

A sandbox per CodeSandbox microVM. Native filesystem, fast hibernate/resume, and public preview hosts, via @codesandbox/sdk.

Installation

@codesandbox/sdk is an optional peer dependency of sbox-sdk, loaded lazily on first use.

pnpm add sbox-sdk @codesandbox/sdk

Usage

import { createSandboxClient } from "sbox-sdk";
import { codesandbox } from "sbox-sdk/codesandbox";

const client = createSandboxClient({
  provider: codesandbox({ apiKey: process.env.CSB_API_KEY! }),
});

const sandbox = await client.create();
const res = await sandbox.commands.run("node --version");
console.log(res.stdout);

await sandbox.files.write("/project/sandbox/state.json", '{"v":1}');

await sandbox.pause(); // hibernate — VM state snapshotted, resumes in ~2–3s

template is a sandbox/template id to fork from (defaults to CodeSandbox's universal template). The SDK splits into a control object (sdk.sandboxes.*) and a per-sandbox client from connect() — the adapter manages both.

Filesystem is native (client.fs.*). commands.run is buffered and throws a CommandError on a non-zero exit — the adapter catches it and emits a normal exit event, so sandbox.commands.run never throws on non-zero (it returns an ExecResult with the real exitCode). pause() hibernates (memory snapshotted), resume() wakes it, setTimeout() sets the hibernation timeout, and destroy() shuts it down.

Authentication

A single API key.

Authentication variables for the codesandbox adapter.
OptionEnv varRequiredDescription
apiKeyCSB_API_KEYrequiredCodeSandbox API key.
templateIdoptionalDefault template/sandbox id to fork from when `template` is omitted.

Options

Prop

Type

Capabilities

CapabilityLevelNotes
listnative
pausenativehibernate (memory snapshotted)
setTimeoutnativehibernation timeout
filesUploadnativenative client.fs
exposePortnativepreview at <id>-<port>.csb.app
privatePreviewnativesigned preview tokens
codeInterpreterunsupported
snapshot / forkunsupportedhibernate is the memory snapshot

perCommandEnvCwd is true (commands.run takes cwd/env); preservesMemoryOnPause is true; previewModel is subdomain.

On this page