API Docs
SDK & Examples

Node.js / TypeScript

Call UnioAPI with the official openai and @anthropic-ai/sdk npm packages — basic and streaming examples for Chat Completions, Responses and Messages.

Keep using the official OpenAI and Anthropic Node SDKs; only change the base URL and the API key. The examples use ESM and top-level await (Node.js 18+).

Prerequisites

  • Node.js 18 or newer.
  • An API key stored in the UNIO_API_KEY environment variable; see Console & top-up.
npm install openai @anthropic-ai/sdk

openai SDK

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.unioapi.com/v1",
  apiKey: process.env.UNIO_API_KEY,
});

Chat Completions

const completion = await client.chat.completions.create({
  model: "gpt-5.6-sol",
  messages: [{ role: "user", content: "Introduce yourself in one sentence" }],
});

console.log(completion.choices[0].message.content);

Responses

const response = await client.responses.create({
  model: "gpt-5.6-sol",
  input: "Introduce yourself in one sentence",
});

console.log(response.output_text);

Responses-only models such as gpt-5.5-pro must use this endpoint; see Choose a Protocol.

Streaming

const stream = await client.chat.completions.create({
  model: "gpt-5.6-sol",
  messages: [{ role: "user", content: "Write a short poem" }],
  stream: true,
  stream_options: { include_usage: true },
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
  if (chunk.usage) console.log("\n", chunk.usage);
}

@anthropic-ai/sdk

The base URL has no /v1; the SDK appends /v1/messages:

import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
  baseURL: "https://api.unioapi.com",
  apiKey: process.env.UNIO_API_KEY,
});

const message = await anthropic.messages.create({
  model: "claude-sonnet-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Introduce yourself in one sentence" }],
});

for (const block of message.content) {
  if (block.type === "text") console.log(block.text);
}

Streaming

const stream = anthropic.messages.stream({
  model: "claude-sonnet-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Write a short poem" }],
});

stream.on("text", (text) => process.stdout.write(text));
const final = await stream.finalMessage();
console.log("\n", final.usage);

Error handling

Both SDKs throw non-2xx responses as error objects with a status, and retry 429 and 5xx automatically (twice by default):

try {
  await client.chat.completions.create({ model: "gpt-5.6-sol", messages: [{ role: "user", content: "hi" }] });
} catch (err) {
  if (err instanceof OpenAI.APIError) {
    console.error(err.status, err.code, err.headers?.["x-request-id"]);
  }
}

Tune maxRetries and timeout as needed; for long tasks use a timeout of several minutes. Status codes are explained in Errors.

Next steps