OpenAI SDK

Use the official OpenAI SDK for Python or TypeScript with AnyRouter by changing the base URL and key.

The official OpenAI SDK works as-is. Set the base URL to https://anyrouter.dev/api/v1 and use an AnyRouter key (sk-ar-...) from the dashboard. Requires Python 3.10+ or Node.js 22+.

bash
pip install openai     # Python
npm install openai     # TypeScript
export ANYROUTER_API_KEY="sk-ar-your-key-here"
python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://anyrouter.dev/api/v1",
    api_key=os.environ["ANYROUTER_API_KEY"],
)

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)

The example prints the model that served the request. Every request also appears in Logs.

Models

  • anyrouter/free: a healthy free model, no credits used. Needs the Go plan ($2/mo, or donate one working provider key to the Pool). See Free Tier.
  • anyrouter/auto: picks automatically from the full catalog.
  • Any provider/model id from the catalog.

To use your own provider keys with no credits deducted, see BYOK.

Troubleshooting

SymptomFix
401 UnauthorizedThe key must start with sk-ar-.
404 model not foundUse a full id such as anyrouter/free or provider/model.
429 on anyrouter/freePer-account free-model limit per minute: 150 on Go, 60 from a donated key, 400 on Pro and up (no daily cap). Wait for Retry-After seconds. More API keys do not add budget.

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