OpenAI SDK
Use the official OpenAI SDK for Python or TypeScript with AnyRouter. Change the base URL and key, then call free or paid models from one endpoint.
Use the OpenAI SDK with AnyRouter
AnyRouter speaks the OpenAI API, so the official OpenAI SDK works as-is. Point it at AnyRouter, pass your AnyRouter key, and every model in the catalog — including free ones — is one model string away.
Moving an existing OpenAI app? See Migrate from OpenAI for a before/after diff. Prefer raw HTTP? See curl.
Before you start
- Python 3.10+ or Node.js 22+ (current SDK requirements).
- An AnyRouter API key (prefix
sk-ar-). Create one in the dashboard.
Set it up
pip install openai # Python
npm install openai # TypeScript / JavaScriptSet the client's base URL to https://anyrouter.dev/api/v1 and pass your AnyRouter key. Export the key so the examples below can read it:
export ANYROUTER_API_KEY="sk-ar-your-key-here"Run the example below. It prints the model that served the request, then the reply. anyrouter/free picks a free model for you; anyrouter/auto picks a model automatically from the full catalog; any provider/model id from the catalog pins one model.
Every request shows up in Logs with the model, provider, tokens, and latency.
Example
import os
from openai import OpenAI
client = OpenAI(
base_url="https://anyrouter.dev/api/v1",
api_key=os.environ["ANYROUTER_API_KEY"], # sk-ar-...
)
completion = client.chat.completions.create(
model="anyrouter/free",
messages=[{"role": "user", "content": "Say hello in five words."}],
)
print(completion.model)
print(completion.choices[0].message.content)import OpenAI from "openai"
const client = new OpenAI({
baseURL: "https://anyrouter.dev/api/v1",
apiKey: process.env.ANYROUTER_API_KEY, // sk-ar-...
})
const completion = await client.chat.completions.create({
model: "anyrouter/free",
messages: [{ role: "user", content: "Say hello in five words." }],
})
console.log(completion.model)
console.log(completion.choices[0].message.content)Free models
Set model to anyrouter/free and AnyRouter routes the request to a free model that is healthy right now — no credits used. The Free plan includes 10 free-model requests per day; paid plans include 1000 per day. Requests that go through your own provider keys do not count toward that cap. Details on Free Tier.
Bring your own keys
Already pay for OpenAI, Anthropic, Google, or another provider? Add those keys to AnyRouter and your SDK calls use them, with no credits deducted. See Get a provider API key and BYOK.
Troubleshooting
Check that the key starts with sk-ar- and is the one passed to the client. Manage keys in the dashboard.
Use a full catalog id such as anyrouter/free or provider/model, not a bare model name. Browse ids in the catalog.
You have used today's free-model requests. Wait for the daily reset, upgrade your plan, use a paid model with credits, or add your own provider keys.
Related
- Migrate from OpenAI — move an existing app
- curl — the same calls from the terminal
- Vercel AI SDK — TypeScript framework
- LangChain — chains and agents
- LlamaIndex — RAG and agents
- Chat Completions API — the underlying endpoint