Menu: OpenAI Node.js SDK

Guides

OpenAI Node.js SDK

Use the official openai package for JavaScript and TypeScript with Scalaix.

Install

npm install openai
export SCALAIX_API_KEY="sx_..."

Chat completions

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://www.scalaix.com/v1",
  apiKey: process.env.SCALAIX_API_KEY,
});

const reply = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Explain idempotency keys in two sentences." }],
});
console.log(reply.choices[0].message.content);

Streaming

const stream = await client.chat.completions.create({
  model: "auto",
  messages: [{ role: "user", content: "Write a haiku about caches." }],
  stream: true,
  stream_options: { include_usage: true },
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

Tools

const reply = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "What's the weather in Oslo?" }],
  tools: [
    {
      type: "function",
      function: {
        name: "get_weather",
        description: "Current weather for a city",
        parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] },
      },
    },
  ],
});
console.log(reply.choices[0].message.tool_calls);

Scalaix headers

const { data, response } = await client.chat.completions
  .create({ model: "auto", messages: [{ role: "user", content: "Hello" }] })
  .withResponse();

console.log(response.headers.get("x-scalaix-model"), response.headers.get("x-scalaix-cache"));

Pass X-Scalaix-Cache per request with the second argument:

await client.chat.completions.create(
  { model: "gpt-4o-mini", messages: [{ role: "user", content: "2 + 2?" }] },
  { headers: { "X-Scalaix-Cache": "off" } },
);

Keep the key on the server. To call Scalaix from a browser, see Keys in the browser.