AnyRouter

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.

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 / JavaScript

Set 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)

Free models

Set model to anyrouter/free and AnyRouter routes the request to a free model that is healthy right now — no credits used. Free models need the Go plan ($2/mo, or free when you donate one working provider key to the shared Pool). They have no daily 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.

This account went over its plan's free-model limit per minute (150 on Go, 60 from a donated key, 400 on Pro and up). Free models have no daily cap. Wait for the number of seconds in the Retry-After header and retry, or upgrade for a higher limit. The limit counts per account, so 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