DEVSAIO

OpenAI Node.js SDK with a custom base URL: complete guide

Set up the openai npm package for any model: streaming, timeouts, retries, and a minimal Express endpoint.

DEVSAIO team · 1 min read
Share on X

The openai npm package works with any OpenAI-compatible API by setting baseURL.

Setup

bash
npm install openai
ts
import OpenAI from "openai";

export const ai = new OpenAI({
  baseURL: "https://devsaio.lol/v1",
  apiKey: process.env.DEVSAIO_API_KEY,
  timeout: 120_000,
  maxRetries: 2,
});

Request

ts
const r = await ai.chat.completions.create({
  model: "gpt-5.6-luna",
  messages: [{ role: "user", content: "Name 3 uses for a Raspberry Pi." }],
  max_tokens: 400,
});
console.log(r.choices[0].message.content, r.usage);

Streaming

ts
const stream = await ai.chat.completions.create({
  model: "claude-sonnet-4-6",
  messages: [{ role: "user", content: "Explain event loops simply." }],
  stream: true,
});
for await (const chunk of stream) process.stdout.write(chunk.choices[0]?.delta?.content ?? "");

A minimal Express endpoint with streaming

ts
import express from "express";
const app = express();
app.use(express.json());

app.post("/api/chat", async (req, res) => {
  res.setHeader("Content-Type", "text/plain; charset=utf-8");
  const stream = await ai.chat.completions.create({
    model: "claude-sonnet-4-6",
    messages: req.body.messages.slice(-20),
    stream: true,
    max_tokens: 1000,
  });
  for await (const c of stream) res.write(c.choices[0]?.delta?.content ?? "");
  res.end();
});

app.listen(3000);

Limiting history (slice(-20)) and max_tokens keeps each request's cost bounded.

Try it with one API key

Claude, GPT, Gemini, DeepSeek, image and video models through one OpenAI- and Anthropic-compatible endpoint. Prepaid, pay only for what you use.

Keep reading