> ## Documentation Index
> Fetch the complete documentation index at: https://docs.deepest.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Use the TypeScript and JavaScript SDK

> Install @deepest/sdk from npm, configure an ESM client, narrow typed envelopes, and preserve server retry and recovery semantics.

`@deepest/sdk` provides generated TypeScript types and a zero-dependency client covering the entire Public API V1 contract. Authorization, resource admission, and lifecycle behavior remain server decisions: the SDK never retries automatically, manages lifecycle orchestration, or masks errors.

Use it from a trusted environment that can securely hold an Agent API Key. For an end-to-end Agent example, see [For Agents](https://docs.deepest.dev/for-agents.md).

## Install from npm

[`@deepest/sdk`](https://www.npmjs.com/package/@deepest/sdk/v/0.1.0) (`0.1.0`, MIT) is **ESM-only** and supports **Node.js 18 or later** using standard `fetch`:

```sh Latest theme={null}
npm install @deepest/sdk
```

```sh Pinned release theme={null}
npm install @deepest/sdk@0.1.0
```

Use `.mjs` files or `"type": "module"` in `package.json`.

## Create a client and read resources

Set `DEEPEST_API_URL` (`https://console.deepest.dev` or your configured control origin, **without** appending `/v1`) and `DEEPEST_API_KEY` in the caller's environment:

```js theme={null}
// Save as resources.mjs
import { Deepest } from "@deepest/sdk";

const { DEEPEST_API_URL, DEEPEST_API_KEY } = process.env;
if (!DEEPEST_API_URL || !DEEPEST_API_KEY) {
  throw new TypeError("Set DEEPEST_API_URL and DEEPEST_API_KEY.");
}

const deepest = new Deepest({
  baseUrl: DEEPEST_API_URL,
  apiKey: DEEPEST_API_KEY,
});

const reply = await deepest.resources.get();
if (reply.ok) {
  console.log({
    account_id: reply.result.account_id,
    entitlement: reply.result.entitlement.status,
    available_memory_mib: reply.result.ram_limit_mib - reply.result.ram_used_mib,
    storage_status: reply.result.storage_status,
    count_constraints: reply.result.machine_count_constraints,
  });
} else {
  console.error({
    code: reply.error.code,
    resource_id: reply.resource_id,
    retry: reply.retry,
    limit: reply.error.limit,
    retry_after_seconds: reply.error.retry_after_seconds,
  });
  process.exitCode = 1;
}
```

> [!TIP]
>
> Pass the control origin without `/v1` (the SDK includes operation paths automatically). Never pass a Preview, Share, or Viewer runtime URL as `baseUrl`.

## Select a specific Machine

Collection namespaces (`computers`, `sandboxes`, `tools`, `vault`, `agentKeys`, `resources`, `invites`) list or create resources; `computer(id)` and `sandbox(id)` operate on a specific stable ID:

```js theme={null}
const list = await deepest.computers.list();
if (!list.ok) throw new Error(list.error.code);
for (const item of list.result) {
  console.log(item.id, item.display_name, item.state, item.ram_mib);
}

const computer = deepest.computer(process.env.DEEPEST_COMPUTER_ID);
const current = await computer.get();
if (!current.ok) throw new Error(`${current.error.code} (${current.retry})`);
```

Always identify Machines by `id`, not `display_name`. Use `deepest.sandboxes` and `deepest.sandbox(id)` for Sandboxes—Computer-only namespaces (`ssh`, `desktop`, `assistance`, `snapshots`, `preview`, `shares`, `tools`, `resize`, `clone`, `storage`) do not exist on Sandboxes.

## TypeScript result handling

All SDK methods return a discriminated union `{ ok: true; ... } | Failure`. A small helper can narrow results while keeping the structured failure envelope intact:

```ts theme={null}
import type { Failure } from "@deepest/sdk";

export function requireResult<T>(reply: { ok: true; result: T } | Failure): T {
  if (!reply.ok) {
    throw new Error(
      JSON.stringify({
        code: reply.error.code,
        resource_id: reply.resource_id,
        retry: reply.retry,
        limit: reply.error.limit,
        retry_after_seconds: reply.error.retry_after_seconds,
      }),
    );
  }
  return reply.result;
}
```

Valid API errors return as `Failure` envelopes rather than rejected promises, while invalid constructor options or path identifiers (`""`, `"."`, `".."`) throw a client-side `TypeError` before any network call.

## Authentication modes

Pass **exactly one** of `apiKey` or `getSessionToken`:

* **`apiKey`:** Standard mode for Agents, CLI scripts, and trusted backend services using an Account-wide Agent API Key.
* **`getSessionToken`:** Async callback (`() => Promise<string | null>`) used by authenticated Web Console sessions to obtain a fresh Human-session token per request (required for `agentKeys.*`, `invites.redeem`, and `assistance.begin/touch/complete`). The SDK never caches Human tokens across calls—never prompt users to paste browser session tokens into scripts or Agents.

## Transport and recovery guarantees

* **Identifier & URL safety:** Resource IDs are encoded as path components; empty, `.`, and `..` identifiers are rejected before dispatch.
* **No redirects or browser cookies:** Requests use `redirect: "error"` and `credentials: "omit"`. If you supply a custom `fetch`, preserve those guarantees and avoid logging `Authorization` headers or Secret payloads.
* **No automatic retries:** Transport failures, non-JSON responses, or HTTP/envelope mismatches return `{ ok: false, resource_id, retry: "query-first", error: { code: "UNKNOWN", ... } }`. When `resource_id` falls back to `"request"`, it means no Machine ID was known—never treat `"request"` as a created Machine ID.

> [!WARNING]
>
> On `UNKNOWN` (`retry: "query-first"`), always observe the original resource or task effect before retrying. For uncertain `computers.create`, `sandboxes.create`, `clone`, or `snapshots.create`, preserve the exact original `idempotency_key` and request parameters.

Continue with [Account resources](https://docs.deepest.dev/concepts/account-resources.md), [Commands](https://docs.deepest.dev/connect/commands.md), [Errors](https://docs.deepest.dev/api-and-sdk/errors.md), [Reliability and recovery](https://docs.deepest.dev/api-and-sdk/reliability.md), or the [API Reference](https://docs.deepest.dev/api-reference.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.