Skip to content

Install the TypeScript SDK and learn how it handles cleanup, cancellation, and errors.

Terminal window
npm install dvm-run

Published as dvm-run on npm: ES modules only, built-in types, Node 22.12 or newer, zero dependencies. Includes the dvm-run CLI (Getting in).

Sandbox.create(image, options) resolves once the sandbox runs (or has exited). Options: command, entrypoint, cpus, memoryMib, seed, env, name, network, allow, apiKey, apiUrl, and signal.

Declare sandboxes with await using. When the scope exits:

  • a session (a sandbox accepting commands) closes its input, and the scope waits for exit;
  • a paused sandbox or one-program sandbox is stopped;
  • an ended sandbox is left unchanged.

await using requires a runtime that supports it, such as Node 24. On Node 22, call end() or stop() in a finally block, or call sandbox[Symbol.asyncDispose]() there. An unended sandbox keeps running.

Call Sandbox.connect(sandboxId) to attach to a sandbox created elsewhere.

run and Command.wait() resolve to a CommandResult with exitCode, stdout, and stderr (Uint8Array). File contents are also Uint8Array. A command’s exit code is never an error.

IDs, offsets, and seeds are JavaScript numbers kept below 253.

Every call takes an AbortSignal via signal. Aborting rejects the call without undoing work already sent. Aborting a command’s wait leaves the command running: signal it to end it. Aborting Sandbox.create during startup stops the sandbox.

js/examples/abort.ts
const server = await sandbox.start(["sleep", "600"]);
try {
await server.wait({ signal: AbortSignal.timeout(5_000) });
} catch (error) {
if (!(error instanceof DOMException && error.name === "TimeoutError")) throw error;
await server.signal(9);
console.log((await server.wait()).exitCode);
}

All errors inherit from DvmError. HTTP errors throw ApiError (status, reason), with subclasses for 400, 401, 404, and 409. Calling an ended sandbox throws SandboxEndedError, or SandboxFailedError (with reason) if dvm or the platform ended it. Connectivity errors throw NetworkError. Sandbox-side rejections throw plain DvmError with the message. See Errors.

js/examples/errors.ts
try {
await using sandbox = await Sandbox.create("alpine:3.20", { command: ["true"], cpus: 64 });
await sandbox.wait();
} catch (error) {
if (error instanceof InvalidRequestError) console.log(error.status, error.reason);
else if (error instanceof SandboxFailedError) console.log("dvm ended it:", error.reason);
else if (error instanceof ApiError && error.status === 402) console.log("out of credit:", error.reason);
else if (error instanceof ApiError && error.status === 403) console.log("not let in yet:", error.reason);
else throw error;
}