OpenAI
Move an OpenAI Chat Completions or Responses client to AnyRouter by changing the base URL, API key, and model id.
Change three values — base URL, API key, and model id — and your existing OpenAI SDK works against AnyRouter. Your chat.completions and responses calls stay exactly the same. This guide is for anyone already calling the OpenAI API who wants one gateway, unified billing, and 180+ models.
Before you start
- An AnyRouter API key (prefixed
sk-ar-). Create one in the dashboard. - An existing OpenAI client (Python, TypeScript, or raw HTTP) you want to repoint.
Prefer to let a coding agent do the swap? Paste the agent prompt below into Claude Code, Cursor, or Codex and it will make all three changes across your project.
What changes
| Setting | Before (OpenAI) | After (AnyRouter) |
|---|---|---|
| Base URL | https://api.openai.com/v1 | https://anyrouter.dev/api/v1 |
| API key | OPENAI_API_KEY | Your AnyRouter key, prefixed sk-ar- |
| Model | gpt-4o-mini | openai/gpt-5.4-mini or any model from Models |
Everything else — request bodies, streaming, error handling — is unchanged.
Before and after
The only diff is the base URL, the key, and the model id — same request body either way.
import OpenAI from "openai"
const client = new OpenAI({
baseURL: "https://api.openai.com/v1",
apiKey: process.env.OPENAI_API_KEY,
})
const completion = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Summarize this pull request." }],
})
import OpenAI from "openai"
const client = new OpenAI({
baseURL: "https://anyrouter.dev/api/v1",
apiKey: process.env.ANYROUTER_API_KEY,
})
const completion = await client.chat.completions.create({
model: "openai/gpt-5.4-mini",
messages: [{ role: "user", content: "Summarize this pull request." }],
})
Migrate
Point the client at AnyRouter
Set the base URL to https://anyrouter.dev/api/v1 and the API key to your AnyRouter key. The request body is identical to what you send OpenAI today.
Update model ids to the provider/model form
AnyRouter model ids include the owner. Rewrite your ids:
gpt-4o-mini→openai/gpt-5.4-minigpt-4o→openai/gpt-5.4
Keep any openai/* id, or pick a different model from the catalog.
Responses works with the same client
The Responses API is repointed the same way — no extra setup:
const response = await client.responses.create({
model: "openai/gpt-5.4-mini",
input: "Extract the action items from this transcript.",
})
Verify
Send one request with your new key and confirm you get a reply:
curl https://anyrouter.dev/api/v1/chat/completions \
-H "Authorization: Bearer sk-ar-your-key" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-5.4-mini",
"messages": [{"role": "user", "content": "Write a release note."}]
}'
List every model available to your key:
curl https://anyrouter.dev/api/v1/models -H "Authorization: Bearer sk-ar-your-key"
Routing preferences (optional)
Add a provider block to any request to cap price, prefer latency, or pin upstream order:
{
"provider": {
"sort": "latency",
"max_price": { "prompt": "2.00", "completion": "8.00" },
"allow_fallbacks": true
}
}
See Provider Routing for every field.
Migrate with an agent prompt
Paste this into a coding agent (Claude Code, Cursor, Codex, …) to make the three changes across the whole project:
Migrate this project from the OpenAI API to AnyRouter.
1. Change the OpenAI client base URL to https://anyrouter.dev/api/v1
2. Swap OPENAI_API_KEY for ANYROUTER_API_KEY (prefix sk-ar-). Never hardcode it.
3. Rewrite model ids to the provider/model form:
gpt-4o-mini → openai/gpt-5.4-mini
gpt-4o → openai/gpt-5.4
Keep openai/* ids, or pick any model from https://anyrouter.dev/models
4. chat.completions and responses calls stay unchanged — same request/response.
5. Keep streaming and error handling exactly as they are.
6. Add a smoke test that sends one short prompt and prints the reply.
Only swap the provider config — do not change UI or business logic.
Troubleshooting
I get a 401 Unauthorized
Check that your key starts with sk-ar- and is sent as Authorization: Bearer sk-ar-your-key. The old OPENAI_API_KEY will not work against AnyRouter.
I get a 404 for my model
The model id must include the owner, e.g. openai/gpt-5.4-mini, not gpt-4o-mini. Confirm the id exists by listing Models.
Related
- Chat Completions API — request and response reference.
- Responses API — typed output items and tool calls.
- Smart Routing — failover and model aliases.
