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/sdkUsage
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–3stemplate 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.
| Option | Env var | Required | Description |
|---|---|---|---|
apiKey | CSB_API_KEY | required | CodeSandbox API key. |
templateId | — | optional | Default template/sandbox id to fork from when `template` is omitted. |
Options
Prop
Type
Capabilities
| Capability | Level | Notes |
|---|---|---|
list | native | |
pause | native | hibernate (memory snapshotted) |
setTimeout | native | hibernation timeout |
filesUpload | native | native client.fs |
exposePort | native | preview at <id>-<port>.csb.app |
privatePreview | native | signed preview tokens |
codeInterpreter | unsupported | |
snapshot / fork | unsupported | hibernate is the memory snapshot |
perCommandEnvCwd is true (commands.run takes cwd/env); preservesMemoryOnPause is true; previewModel is subdomain.