Install
npm i openai
Configure
Point the client at https://api.routeur.ai/v1 and pass your routeur.ai key as the API key:
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.routeur.ai/v1",
apiKey: process.env.ROUTEUR_KEY,
});
Example
const res = await client.chat.completions.create({
model: "auto",
messages: [{ role: "user", content: "Say hello in one short sentence." }],
});
console.log(res.choices[0].message.content);
// Inspect routing metadata:
console.log(res.routeur.ai);
Streaming
Streaming works exactly as it does against OpenAI:
const stream = await client.chat.completions.create({
model: "auto",
stream: true,
messages: [{ role: "user", content: "Stream a haiku." }],
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
Choosing a model
Every request picks an upstream model in one of three ways — in this order of precedence:
- Routeur-* request headers. If
Routeur-ProviderorRouteur-Modelare set, they win, regardless of what is in the body. Useful from middleware or transports that can't change the JSON. - An explicit upstream id in
model. Pass something like"gpt-4o-mini"or"claude-3-5-sonnet"to bypass routing rules and pin the request to that model. model: "auto"(or any alias). Lets routeur.ai pick the upstream model from your routing rules. This is the recommended default.
Force a specific provider+model on a single call via headers (the body's model is ignored when headers are set):
await client.chat.completions.create(
{ model: "auto", messages },
{ headers: { "Routeur-Provider": "openai", "Routeur-Model": "gpt-4o" } }
);