# 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](https://github.com/openai/openai-python) 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](/guides/migrate-from-openai) for a before/after diff. Prefer raw HTTP? See [curl](/guides/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](https://anyrouter.dev/dashboard/keys).

## Set it up

::::steps

:::step{title="Install the SDK"}
```bash
pip install openai     # Python
npm install openai     # TypeScript / JavaScript
```
:::

:::step{title="Point the client at AnyRouter"}
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:

```bash
export ANYROUTER_API_KEY="sk-ar-your-key-here"
```
:::

:::step{title="Pick a model and verify"}
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](https://anyrouter.dev/models) pins one model.

Every request shows up in [Logs](https://anyrouter.dev/dashboard/logs) with the model, provider, tokens, and latency.
:::

::::

## Example

::::tabs

:::tab{label="Python"}
```python
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)
```
:::

:::tab{label="TypeScript"}
```typescript
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](/features/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](/guides/byok-provider-keys) and [BYOK](/features/byok).

## Troubleshooting

:::details{summary="401 Unauthorized"}
Check that the key starts with `sk-ar-` and is the one passed to the client. Manage keys in the [dashboard](https://anyrouter.dev/dashboard/keys).
:::

:::details{summary="404 model not found"}
Use a full catalog id such as `anyrouter/free` or `provider/model`, not a bare model name. Browse ids in the [catalog](https://anyrouter.dev/models).
:::

:::details{summary="429 on anyrouter/free"}
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](/guides/migrate-from-openai) — move an existing app
- [curl](/guides/curl) — the same calls from the terminal
- [Vercel AI SDK](/guides/vercel-ai-sdk) — TypeScript framework
- [LangChain](/guides/langchain) — chains and agents
- [LlamaIndex](/guides/llamaindex) — RAG and agents
- [Chat Completions API](/api-reference/chat-completions) — the underlying endpoint
