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

# Node.js / TypeScript

> Use ARouter with the OpenAI, Anthropic, or ARouter Node.js SDK.

## OpenAI SDK

The [OpenAI Node.js SDK](https://github.com/openai/openai-node) works out of the box.

### Installation

<Tabs>
  <Tab title="npm">
    ```bash theme={null}
    npm install openai
    ```
  </Tab>

  <Tab title="yarn">
    ```bash theme={null}
    yarn add openai
    ```
  </Tab>

  <Tab title="pnpm">
    ```bash theme={null}
    pnpm add openai
    ```
  </Tab>
</Tabs>

### Basic Usage

```typescript theme={null}
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.arouter.ai/v1",
  apiKey: "lr_live_xxxx",
});

const response = await client.chat.completions.create({
  model: "openai/gpt-5.4",
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0].message.content);
```

### Multi-Provider

```typescript theme={null}
// Anthropic via OpenAI SDK
const response = await client.chat.completions.create({
  model: "anthropic/claude-sonnet-4.6",
  messages: [{ role: "user", content: "Hello!" }],
});

// DeepSeek via OpenAI SDK
const response = await client.chat.completions.create({
  model: "deepseek/deepseek-v3.2",
  messages: [{ role: "user", content: "Hello!" }],
});
```

### Streaming

```typescript theme={null}
const stream = await client.chat.completions.create({
  model: "openai/gpt-5.4",
  messages: [{ role: "user", content: "Write a poem." }],
  stream: true,
});

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content;
  if (content) process.stdout.write(content);
}
```

***

## Anthropic SDK

The [Anthropic Node.js SDK](https://github.com/anthropics/anthropic-sdk-typescript) works natively.

### Installation

<Tabs>
  <Tab title="npm">
    ```bash theme={null}
    npm install @anthropic-ai/sdk
    ```
  </Tab>

  <Tab title="yarn">
    ```bash theme={null}
    yarn add @anthropic-ai/sdk
    ```
  </Tab>

  <Tab title="pnpm">
    ```bash theme={null}
    pnpm add @anthropic-ai/sdk
    ```
  </Tab>
</Tabs>

### Basic Usage

```typescript theme={null}
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://api.arouter.ai",
  apiKey: "lr_live_xxxx",
});

const message = await client.messages.create({
  model: "claude-sonnet-4.6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(message.content[0].text);
```

### Streaming

```typescript theme={null}
const stream = client.messages.stream({
  model: "claude-sonnet-4.6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Tell me a story." }],
});

for await (const event of stream) {
  if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
    process.stdout.write(event.delta.text);
  }
}
```

***

## ARouter SDK

The first-party `@arouter/sdk` provides additional features like key management
and usage tracking.

### Installation

<Tabs>
  <Tab title="npm">
    ```bash theme={null}
    npm install @arouter/sdk
    ```
  </Tab>

  <Tab title="yarn">
    ```bash theme={null}
    yarn add @arouter/sdk
    ```
  </Tab>

  <Tab title="pnpm">
    ```bash theme={null}
    pnpm add @arouter/sdk
    ```
  </Tab>
</Tabs>

### Chat Completion

```typescript theme={null}
import { ARouter } from "@arouter/sdk";

const router = new ARouter({
  apiKey: "lr_live_xxxx",
  baseURL: "https://api.arouter.ai",
});

const response = await router.chatCompletion({
  model: "openai/gpt-5.4",
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0].message.content);
```

### Streaming

```typescript theme={null}
const stream = await router.chatCompletionStream({
  model: "openai/gpt-5.4",
  messages: [{ role: "user", content: "Write a haiku." }],
});

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content;
  if (content) process.stdout.write(content);
}
```

### Key Management

Use a management key (`lr_mgmt_`) to create and manage API keys.

```typescript theme={null}
const mgmt = new ARouter({
  apiKey: "lr_mgmt_xxxx",
  baseURL: "https://api.arouter.ai",
});

const key = await mgmt.createKey({
  name: "backend-service",
  allowed_providers: ["openai", "anthropic"],
  limit: 150,
  limit_reset: "monthly",
});
console.log("API Key:", key.key);

const keys = await mgmt.listKeys();
console.log("Keys:", keys.data);

await mgmt.updateKey(key.data.hash, { disabled: true });

await mgmt.deleteKey(key.data.hash);
```

### Wallet JWT + x402 Payments

AI agents with crypto wallets can authenticate via SIWx and pay for credits via x402.
The SDK caches a wallet JWT and uses it as the Bearer token for subsequent requests.

#### EVM (Base)

```typescript theme={null}
import { ARouter, withX402EvmPayment } from "@arouter/sdk";
import { privateKeyToAccount } from "viem/accounts";

const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`);

const client = withX402EvmPayment(
  new ARouter({ baseURL: "https://api.arouter.ai", apiKey: "" }),
  signer,
);

const response = await client.chatCompletion({
  model: "openai/gpt-5.4",
  messages: [{ role: "user", content: "Hello" }],
});
```

#### Solana

```typescript theme={null}
import { ARouter, withX402SolanaPayment } from "@arouter/sdk";

const client = withX402SolanaPayment(
  new ARouter({ baseURL: "https://api.arouter.ai", apiKey: "" }),
  solanaSigner,
  { address: solanaAddress, signMessage: (msg) => signEd25519(msg) },
);
```

#### SIWx Only (No Auto-Payment)

```typescript theme={null}
import { ARouter, withSIWxAuth } from "@arouter/sdk";

const client = await withSIWxAuth(
  new ARouter({ baseURL: "https://api.arouter.ai", apiKey: "" }),
  { address: "0xYourAddress", signMessage: (msg) => signer.signMessage({ message: msg }) },
);
```

See the [x402 Payments Guide](/en/guides/x402-payments) for the full protocol flow.
