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/sdk

Authentication

A single API key.

Authentication variables for the codesandbox adapter.
OptionEnv varRequiredDescription
apiKeyCSB_API_KEYrequiredCodeSandbox API key.
templateId—optionalDefault 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 sandbox

What create() does with your spec:

  • template is a sandbox/template id to fork from. It defaults to the universal template.
  • name becomes the sandbox title.
  • metadata is stored as key=value tags. CodeSandbox allows at most 10 tags.
  • lifecycle.idleTimeoutMs sets the hibernation timeout.

Previews

  • ports.expose(port) returns https://<id>-<port>.csb.app with 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

CapabilityLevelNotes
listnativewalks every page. Status comes from the running filter
pausenativehibernate. resume() reconnects a fresh client
setTimeoutnativehibernation timeout
streamingnativerunBackground output events, merged into stdout
filesUploadnativenative client.fs
exposePortnative<id>-<port>.csb.app
privatePreviewnativehost token in the preview_token query parameter
snapshot, fork, background, pty, codeInterpreterunsupported

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\n turned into \n.
  • State is approximate. The API has no per-sandbox status. A sandbox that isn't running shows as stopped in list(), because hibernated and shut-down sandboxes can't be told apart. The exception is a sandbox that this handle paused itself, which shows as paused.
  • timeoutMs has no native equivalent. The core's backstop kills the command after the timeout.

On this page