Use the OpenAI SDK
Python and Node.js use the same URL and key.
URL and authentication
Use https://api.hanrouter.ai/v1 as the OpenAI-compatible Base URL and Authorization: Bearer $HANROUTER_API_KEY. Do not duplicate /v1. All manufacturers use your HanRouter key, not an upstream vendor key.
For the Anthropic SDK and Claude Code, use https://api.hanrouter.ai without /v1; the client appends /v1/messages. Use the OpenAI-compatible interface for Google models.
Python
Install: pip install openai.
Request example
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["HANROUTER_API_KEY"],
base_url="https://api.hanrouter.ai/v1",
)
model_id = os.environ["HANROUTER_MODEL"]
result = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": "Hello"}],
)
print(result.choices[0].message.content)Node.js
Install: npm install openai.
Request example
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.HANROUTER_API_KEY,
baseURL: "https://api.hanrouter.ai/v1" });
const result = await client.chat.completions.create({
model: process.env.HANROUTER_MODEL,
messages: [{ role: "user", content: "Hello" }]
});
console.log(result.choices[0].message.content);