Vercel AI SDK
Use the Vercel AI SDK with AnyRouter by pointing the OpenAI provider at one endpoint.
Configure the @ai-sdk/openai provider with AnyRouter's base URL and an API key (sk-ar-..., from the dashboard); everything else in the Vercel AI SDK stays the same.
npm install ai @ai-sdk/openai
export ANYROUTER_API_KEY="sk-ar-your-key-here"import { createOpenAI } from "@ai-sdk/openai"
import { generateText, streamText } from "ai"
const anyrouter = createOpenAI({
baseURL: "https://anyrouter.dev/api/v1",
apiKey: process.env.ANYROUTER_API_KEY,
})
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)
// Streaming
const result = streamText({
model: anyrouter("anyrouter/free"),
prompt: "Explain routing tables in two sentences.",
})
for await (const part of result.textStream) process.stdout.write(part)Run it as an ES module (for example npx tsx index.ts). Requests appear in Logs.
anyrouter("model-id") uses the Responses API (/api/v1/responses). anyrouter.chat("model-id") uses Chat Completions.
Models
anyrouter/free: a healthy free model, no credits used. Needs the Go plan ($2/mo, or donate one working provider key). See Free Tier.anyrouter/auto: AnyRouter picks from the full catalog.- Any
provider/modelid from the catalog.
To use your own provider keys, see BYOK.
Troubleshooting
| Symptom | Fix |
|---|---|
401 Unauthorized | apiKey must hold a key starting with sk-ar-. |
AI_APICallError: Invalid JSON response | Update ai and @ai-sdk/openai. If it persists, use anyrouter.chat("model-id") and include the request id when you contact support. |
404 model not found | Pass a full id such as anyrouter/free or z-ai/glm-4.7-flash. |
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