Docs
Node.js SDK
Use the official openai package on Node or the browser against the EasyAI gateway, with streaming and error handling.
Install
The gateway works with the standard openai package on npm.
bash
npm install openaiChat completion
Point baseURL at the gateway and use your EasyAI key. The client works in Node 18+, Edge runtimes, and the browser.
chat.ts
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://easyairoute.com/v1",
apiKey: process.env.EASYAI_API_KEY,
});
const resp = await client.chat.completions.create({
model: "claude-fable-5",
messages: [{ role: "user", content: "Explain quic in one paragraph." }],
});
console.log(resp.choices[0].message.content);Streaming
Pass stream: true and consume the async iterator.
stream.ts
const stream = await client.chat.completions.create({
model: "claude-fable-5",
messages: [{ role: "user", content: "Count to five slowly." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}Error handling
Errors follow the OpenAI shape: status plus a typed error body. Handle 401 (bad key), 429 (quota or rate limit), and 5xx (upstream) separately — the SDK throws APIError subclasses you can branch on.
errors.ts
import OpenAI from "openai";
try {
await client.chat.completions.create({ /* ... */ });
} catch (err) {
if (err instanceof OpenAI.AuthenticationError) {
// 401 — check the key
} else if (err instanceof OpenAI.RateLimitError) {
// 429 — top up or slow down
} else if (err instanceof OpenAI.APIError) {
// anything else, including upstream failures
console.error(err.status, err.message);
}
}