Verified against the current service routes · September 25, 2026
AnRouter integration guide
From your first request to Codex, Claude Code, OpenCode, OpenClaw, and Hermes Agent. Every example distinguishes a Base URL from a full endpoint to prevent duplicate paths.
5-minute quickstart
Create an API key in the AnRouter dashboard, then copy a model ID from the models page. The examples below use the currently listed glm-5.3.
Python requires the official openai package. The Node.js example requires the openai package and must be saved as an ES module, for example example.mjs. Set ANROUTER_API_KEY in the process environment before running either example.
1 · Key
Create an API key
2 · URL
Use the correct Base URL
3 · Model
Copy the exact model ID
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["ANROUTER_API_KEY"],
base_url="https://api.anrouter.com/v1",
)
response = client.chat.completions.create(
model="glm-5.3",
messages=[{"role": "user", "content": "Introduce yourself in three sentences."}],
)
print(response.choices[0].message.content)
// Save as example.mjs, then run: node example.mjs
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ANROUTER_API_KEY,
baseURL: "https://api.anrouter.com/v1",
});
const response = await client.chat.completions.create({
model: "glm-5.3",
messages: [{ role: "user", content: "Introduce yourself in three sentences." }],
});
console.log(response.choices[0].message.content);
Success criteria: the response has HTTP status 200 and contains assistant output. Before production, separately test streaming, tool calling, and any required multimodal features. A successful text response does not prove every advanced feature is supported.
Codex CLI / desktop app
Add AnRouter as a separate provider in ~/.codex/config.toml, verify the Responses route, then launch with your key.
OpenAI-compatible requests use a Bearer token. The AnRouter Anthropic Messages route accepts both the Bearer authentication used by Claude Code and the x-api-key header commonly used by Anthropic SDKs.
Keep keys in trusted environments only. Never put them in browser code, public repositories, screenshots, or committed project files. Revoke and replace a key immediately if exposure is suspected.
Endpoints and Base URLs
A Base URL is the prefix to which a client appends a route; it is not always the full request URL. Different clients append different paths, so one URL cannot be copied mechanically into every app.
Use case
Enter this
Final request
OpenAI SDK / OpenCode / OpenClaw / Hermes
https://api.anrouter.com/v1
/v1/chat/completions
Codex custom provider
https://api.anrouter.com/v1
/v1/responses
Claude Code / Claude Desktop Gateway / Anthropic SDK
The most common mistake is a duplicate path. Putting the complete /v1/chat/completions endpoint into an SDK Base URL, or giving Claude Code https://api.anrouter.com/v1, can produce an invalid /v1/v1/… request.
Primary AnRouter API routes
Text protocols
POST /v1/chat/completions POST /v1/responses POST /v1/messages
Model discovery and rerank
GET /v1/models GET /v1/models/{model} POST /v1/rerank
Images and audio
POST /v1/images/generations POST /v1/images/edits POST /v1/audio/transcriptions POST /v1/audio/speech
A route existing does not mean every model supports that capability. The model, upstream channel, and request fields must all be compatible.
Choosing a protocol: start ordinary third-party clients with Chat Completions; Codex requires Responses; Claude Code uses Anthropic Messages. Test tool calling, streaming, image input, and structured output against the actual model.
Troubleshooting
401 / Unauthorized
Check that the key is complete and has not been revoked. OpenAI-compatible requests require Authorization: Bearer; when Claude Code uses Bearer authentication, set ANTHROPIC_AUTH_TOKEN.
404 / Not Found
Inspect the final URL. Look for /v1/v1, a full endpoint entered as a Base URL, or a model ID that does not exactly match GET /v1/models.
429 / Too Many Requests
This can indicate token, concurrency, or upstream capacity limits. Use exponential backoff with jitter and a bounded retry count.
Chat works, but an agent fails
Agents usually also depend on tool calling, streaming, and a particular protocol. Test Codex on /v1/responses and Claude Code on /v1/messages.
503 / non_streaming_response_timeout
A non-streaming request is disconnected after about 110 seconds and returns HTTP 503 non_streaming_response_timeout. For long outputs or very long contexts, use streaming (stream: true) instead.
When contacting support, include the time, request endpoint, model ID, HTTP status, and any request/error ID in the response. Never send the full API key; at most retain its first and last four characters for identification.
OpenClaw
Add AnRouter as a model provider without touching the Gateway login token, then validate and restart the Gateway.
When a client explicitly supports an “OpenAI-compatible” or “Custom OpenAI endpoint,” the four values below will usually connect it. A similar product name does not prove that a custom Base URL is supported; do not force undocumented environment-variable overrides into software with no public configuration path.
Provider type
OpenAI compatible
Base URL
https://api.anrouter.com/v1
API key
AnRouter API key
Model
Copy from the models page or GET /v1/models
Confirm whether the final client request uses /v1/chat/completions or /v1/responses.
Start with a minimal text request, then test streaming, tools, vision, JSON, and other required capabilities.
If a client accepts only an official vendor key and exposes no Base URL field, do not claim that it natively supports AnRouter.
Security and production checklist
Key isolation
Create separate keys for people, CI, and production services. Do not share one long-lived key across a team. Use environment variables, a system secret manager, or the client's credential store.
Capability acceptance
Validate the exact model ID, protocol, streaming, tool calls, timeouts, and maximum inputs with realistic requests. Do not put an agent into production merely because ordinary chat succeeds.
Retry policy
Retry only recoverable failures, with exponential backoff, jitter, and a maximum attempt count. Log request IDs—not complete keys or sensitive prompts.
Change control
Clients and models change. After a client upgrade, protocol change, or model switch, rerun the minimal request and agent capability checks in a test environment.