# TypeScript

> Install the dvm-run npm package (Node 22.12+), clean up with await using, cancel calls with AbortSignal, and handle errors.

This is the Markdown of https://docs.dvm.run/sdks/typescript/. Every page of the dvm.run docs is served as Markdown at its address plus `.md`; the list of pages is https://docs.dvm.run/llms.txt.

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



```sh
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](/start/access/)).

## Create and end a sandbox

`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.

## Bytes and results

`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.

## Cancel a call

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`

```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);
}
```

## Errors

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](/reference/errors/).

`js/examples/errors.ts`

```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;
}
```

## Where next

* [Sessions](/guides/sessions/): run commands and transfer files.
* [TypeScript reference](/reference/typescript/): every class and method.
* [npm](https://www.npmjs.com/package/dvm-run) and [source](https://github.com/deterministic-machines/sdk/tree/main/js).
