> ## 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

> OpenAI、Anthropic、または ARouter Node.js SDK で ARouter を使用する。

## OpenAI SDK

[OpenAI Node.js SDK](https://github.com/openai/openai-node) はそのまま使用できます。

### インストール

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

### 基本的な使い方

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

### マルチプロバイダー

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

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

### ストリーミング

```typescript theme={null}
const stream = await client.chat.completions.create({
  model: "openai/gpt-5.4",
  messages: [{ role: "user", content: "詩を書いてください。" }],
  stream: true,
});

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

***

## Anthropic SDK

[Anthropic Node.js SDK](https://github.com/anthropics/anthropic-sdk-typescript) はそのまま使用できます。

### インストール

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

### 基本的な使い方

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

### ストリーミング

```typescript theme={null}
const stream = client.messages.stream({
  model: "claude-sonnet-4.6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "物語を話してください。" }],
});

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

ファーストパーティの `@arouter/sdk` は key 管理や使用量追跡などの追加機能を提供します。

### インストール

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

### チャット補完

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

### ストリーミング

```typescript theme={null}
const stream = await router.chatCompletionStream({
  model: "openai/gpt-5.4",
  messages: [{ role: "user", content: "俳句を書いてください。" }],
});

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

### key 管理

管理キー（`lr_mgmt_`）を使用して API key を作成・管理します。

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

### ウォレット JWT + x402 支払い

暗号ウォレットを持つ AI エージェントは SIWx で認証し、x402 でクレジットを支払えます。
SDK はウォレット JWT をキャッシュし、以降のリクエストの Bearer token として使用します。

#### 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 のみ（自動支払いなし）

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

完全なプロトコルフローは [x402 支払いガイド](/jp/guides/x402-payments) をご参照ください。
