AnyRouter

Vercel AI SDK

Use the Vercel AI SDK with AnyRouter. Point the OpenAI provider at one endpoint to generate and stream text from free or paid models.

Build AI features in JavaScript and TypeScript with the Vercel AI SDK, routed through AnyRouter. Because AnyRouter is OpenAI-compatible, you configure the @ai-sdk/openai provider with a new base URL and key — everything else stays the same.

Before you start

  • A JavaScript or TypeScript project.
  • An AnyRouter API key (prefix sk-ar-). Create one in the dashboard.

Set it up

npm install ai @ai-sdk/openai

Prefer pnpm add ai @ai-sdk/openai or yarn add ai @ai-sdk/openai if that matches your project.

Create the provider with createOpenAI, set baseURL to https://anyrouter.dev/api/v1, and pass your key. Select models with anyrouter(...).

import { createOpenAI } from "@ai-sdk/openai"
import { generateText } from "ai"

const anyrouter = createOpenAI({
  baseURL: "https://anyrouter.dev/api/v1",
  apiKey: process.env.ANYROUTER_API_KEY, // prefixed sk-ar-
})

const { text, response } = await generateText({
  model: anyrouter("anyrouter/free"),
  prompt: "Write a two-line poem about routers.",
})

console.log(response.modelId) // the model that served the request
console.log(text)

For real-time chat, swap generateText for streamText and iterate the token stream.

import { createOpenAI } from "@ai-sdk/openai"
import { streamText } from "ai"

const anyrouter = createOpenAI({
  baseURL: "https://anyrouter.dev/api/v1",
  apiKey: process.env.ANYROUTER_API_KEY,
})

const result = streamText({
  model: anyrouter("anyrouter/free"),
  prompt: "Explain routing tables in two sentences.",
})

for await (const textPart of result.textStream) {
  process.stdout.write(textPart)
}

Verify

Set your key, then run the first snippet as an ES module (for example npx tsx index.ts). It prints the model that served the request, then the completion.

export ANYROUTER_API_KEY="sk-ar-your-key-here"

Every request also shows up in Logs with the model, provider, tokens, and latency.

anyrouter("model-id") uses the Responses API (/api/v1/responses), with streaming and non-streaming. anyrouter.chat("model-id") uses Chat Completions and works too.

Free models

Set the model to anyrouter/free and AnyRouter routes the request to a free model that is healthy right now — no credits used. Free models have no daily cap on any plan. Details on Free Tier.

Use anyrouter/auto to let AnyRouter pick from the full catalog, or any provider/model id from the catalog to pin one model.

Bring your own keys

Already pay for OpenAI, Anthropic, Google, or another provider? Add those keys to AnyRouter and your requests use them, with no credits deducted. See Get a provider API key and BYOK.

Troubleshooting

Ensure apiKey holds a key that starts with sk-ar-. Create and manage keys in the dashboard.

Update to a current ai and @ai-sdk/openai. If it persists, switch to anyrouter.chat("model-id") and include the request id from the error when you contact support.

Pass a full catalog id to anyrouter(...) — anyrouter/free, anyrouter/auto, or a provider/model id such as z-ai/glm-4.7-flash. Valid ids live in the catalog.