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

# TypeScript SDK

> Official @meshrouter/sdk client for Node.js and the browser.

The TypeScript SDK ships ESM + CommonJS, has full type definitions inlined, and works with Node 18+
and modern browser bundlers.

## Install

```bash theme={null}
npm install @meshrouter/sdk
# or
pnpm add @meshrouter/sdk
# or
yarn add @meshrouter/sdk
```

Package: [`@meshrouter/sdk`](https://www.npmjs.com/package/@meshrouter/sdk).

## First request

```ts theme={null}
import { Mesh } from '@meshrouter/sdk';

const mesh = new Mesh({ apiKey: process.env.MESH_API_KEY! });

const reply = await mesh.ai.chat({
  model: 'auto',
  messages: [{ role: 'user', content: 'route this privately' }],
  maxOutputTokens: 200,
});

console.log(reply.content);
console.log('receipt:', reply.receiptId, '$' + reply.actualCustomerChargeUsd);
```

The SDK defaults to `https://api.meshrouter.app/v1`. Pass `baseUrl` to override.

## Streaming

```ts theme={null}
for await (const chunk of mesh.ai.stream({
  model: 'mesh/claude-sonnet-4.5',
  messages: [{ role: 'user', content: 'stream this' }],
  maxOutputTokens: 500,
})) {
  if (chunk.delta) process.stdout.write(chunk.delta);
}
```

## Quote a request before sending

```ts theme={null}
const quote = await mesh.ai.quote({
  model: 'mesh/claude-opus-4.6',
  messages: [{ role: 'user', content: 'summarize this 10k-token doc...' }],
  maxOutputTokens: 1000,
});

console.log('max charge:', quote.estimatedMaxCharge);
console.log('expires at:', quote.expiresAt);
```

## Models catalogue

```ts theme={null}
const models = await mesh.models.list();
for (const m of models) {
  console.log(m.alias, m.provider, m.inputUsdPerMillion + '/' + m.outputUsdPerMillion);
}
```

## Error handling

The SDK maps response codes to typed errors:

```ts theme={null}
import {
  MeshAuthError,
  MeshInsufficientBalanceError,
  MeshValidationError,
  MeshDuplicateRequestError,
} from '@meshrouter/sdk';

try {
  await mesh.ai.chat({
    /* … */
  });
} catch (err) {
  if (err instanceof MeshInsufficientBalanceError) {
    console.error(`Top up ${err.required} USD; have ${err.available}.`);
  } else if (err instanceof MeshAuthError) {
    console.error('Bad API key.');
  } else if (err instanceof MeshValidationError) {
    console.error('Bad request:', err.code, err.message);
  }
}
```

## OpenAI compatibility

Prefer the OpenAI SDK? See [OpenAI-compatible client](/sdks/openai-compatible) for the drop-in path.
The MESH SDK is for when you want typed receipts, payments, and account helpers without dropping the
OpenAI shape.
