CodeSandbox
Deprecated: CodeSandbox sandboxes are now Together Sandbox. Use sbox-sdk/together. This adapter is removed in 0.2.0.
Deprecated: use sbox-sdk/together
CodeSandbox's sandbox product has moved to Together Sandbox. Use the
together adapter (sbox-sdk/together) for new work.
sbox-sdk/codesandbox is deprecated in 0.1.0 and only gets crash and leak
fixes. It will be removed in 0.2.0. Calling codesandbox() logs a
one-time deprecation warning.
Migrating
// before
import { codesandbox } from "sbox-sdk/codesandbox";
const client = createSandboxClient({ provider: codesandbox() });
// after
import { together } from "sbox-sdk/together";
const client = createSandboxClient({ provider: together() });Your sandbox code (commands, files, ports, pause()/resume()) stays the same, because both adapters implement the same interface.
Installation
@codesandbox/sdk is an optional peer dependency of sbox-sdk, loaded lazily on first use.
pnpm add sbox-sdk @codesandbox/sdkAuthentication
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. |
codesandbox() reads CSB_API_KEY when you don't pass apiKey. Check the key with client.verify(). It lists one sandbox and never creates anything.
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({ metadata: { team: "a" }, name: "box" });
const res = await sandbox.commands.run("node --version");
console.log(res.stdout);
await sandbox.pause(); // hibernate
await sandbox.resume(); // wakes the VM and reconnects
await sandbox.destroy(); // permanently deletes the sandboxWhat create() does with your spec:
templateis a sandbox/template id to fork from. It defaults to the universal template.namebecomes the sandbox title.metadatais stored askey=valuetags. CodeSandbox allows at most 10 tags.lifecycle.idleTimeoutMssets the hibernation timeout.
Previews
ports.expose(port)returnshttps://<id>-<port>.csb.appwith public auth.ports.expose(port, { visibility: "private" })mints a 24-hour host token. Auth is{ type: "query", param: "preview_token", token }.
The token only matters if the sandbox's privacy is private. Sandboxes created from a public template have public hosts either way.
Capabilities
| Capability | Level | Notes |
|---|---|---|
list | native | walks every page. Status comes from the running filter |
pause | native | hibernate. resume() reconnects a fresh client |
setTimeout | native | hibernation timeout |
streaming | native | runBackground output events, merged into stdout |
filesUpload | native | native client.fs |
exposePort | native | <id>-<port>.csb.app |
privatePreview | native | host token in the preview_token query parameter |
snapshot, fork, background, pty, codeInterpreter | unsupported |
perCommandEnvCwd is true, preservesMemoryOnPause is true and previewModel is subdomain.
Known limitations
- stdout and stderr are one stream. CodeSandbox runs commands in a PTY, so everything comes back as
stdout, with\r\nturned into\n. - State is approximate. The API has no per-sandbox status. A sandbox that isn't running shows as
stoppedinlist(), because hibernated and shut-down sandboxes can't be told apart. The exception is a sandbox that this handle paused itself, which shows aspaused. timeoutMshas no native equivalent. The core's backstop kills the command after the timeout.