Connect your application with one API key
HanRouter is compatible with the OpenAI API. Use an OpenAI SDK when possible, or send standard HTTPS requests with your API key.
1. Create an API key
Create a dedicated key in the console. Copy it once, store it in an environment variable, and never expose it in browser code or a repository.
Open API Keys2. Set the Base URL
Use this Base URL for all OpenAI-compatible requests. Keep the /v1 suffix.
https://www.hanrouter.com/v13
3. Connect with an OpenAI SDK
HanRouter is compatible with the OpenAI API. Use an OpenAI SDK when possible, or send standard HTTPS requests with your API key.
Python
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ["HANROUTER_API_KEY"],
base_url="https://www.hanrouter.com/v1",
)
response = client.chat.completions.create(
model="your-model-id",
messages=[{"role": "user", "content": "Hello, HanRouter"}],
)
print(response.choices[0].message.content)Node.js
import OpenAI from "openai"
const client = new OpenAI({
apiKey: process.env.HANROUTER_API_KEY,
baseURL: "https://www.hanrouter.com/v1",
})
const response = await client.chat.completions.create({
model: "your-model-id",
messages: [{ role: "user", content: "Hello, HanRouter" }],
})
console.log(response.choices[0].message.content)4
4. Verify the key before sending traffic
List the models available to your key first, then use one of the returned model IDs in a chat request.
cURL
curl https://www.hanrouter.com/v1/models \
-H "Authorization: Bearer $HANROUTER_API_KEY"