Skip to main content

OpenAI SDK

The OpenAI Node.js SDK works out of the box.

Installation

npm install openai

Basic Usage

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

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

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 works natively.

Installation

npm install @anthropic-ai/sdk

Basic Usage

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

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

npm install @arouter/sdk

Chat Completion

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

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

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

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)

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 for the full protocol flow.