AnyRouter SDK
Official TypeScript SDK for AnyRouter, with a typed client and a Vercel AI SDK provider.
The official TypeScript SDK: one key and base URL for chat, embeddings, the Responses API, and the Anthropic Messages API, with typed requests, responses, errors, and automatic retries.
| Package | Use it for |
|---|---|
@anyr/sdk | Direct client: chat, embeddings, models, credits, keys, presets, Responses, Messages |
@anyr/ai-sdk-provider | Vercel AI SDK provider via createAnyRouter |
Quickstart
-
Install and set your key (from anyrouter.dev/dashboard/keys). The client reads
ANYROUTER_API_KEYwhen you omit it:npm install @anyr/sdk export ANYROUTER_API_KEY=sk-ar-v1-... -
Make a request:
import { AnyRouter } from "@anyr/sdk" const client = new AnyRouter() const completion = await client.chat.completions.create({ model: "z-ai/glm-4.7-flash", messages: [{ role: "user", content: "Hello!" }], }) console.log(completion.choices[0].message.content)
Streaming
Pass stream: true:
const stream = await client.chat.completions.create({
model: "z-ai/glm-4.7-flash",
messages: [{ role: "user", content: "Write a haiku about TypeScript." }],
stream: true,
})
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "")
}Retries and timeouts
Transient failures (connection errors, timeouts, 429, 5xx) retry with exponential backoff that honors Retry-After. The timeout bounds the wait for the first response without aborting a running stream. Per call, pass signal (an AbortSignal), timeout, maxRetries, or headers:
const controller = new AbortController()
const stream = await client.chat.completions.create(
{ model: "z-ai/glm-4.7-flash", messages, stream: true },
{ signal: controller.signal, timeout: 30_000 },
)Vercel AI SDK
import { createAnyRouter } from "@anyr/ai-sdk-provider"
import { generateText } from "ai"
const anyrouter = createAnyRouter() // reads ANYROUTER_API_KEY
const { text } = await generateText({
model: anyrouter("z-ai/glm-4.7-flash"),
prompt: "What is AnyRouter?",
})AnyRouter routes requests to third-party models. Models and outputs are provided as is, with no warranty; you use them at your own risk. Disclaimer