Node.js Examples

Use the official OpenAI Node SDK with gpt88.cc. Includes basic calls, streaming, function calling, backoff retries, and a Next.js Edge route example.

Install and configure

Point process.env.GPT88_API_KEY at the key created in the Agent API Keys.

bash
npm i openai
# or
pnpm add openai

Basic call

basic.tstypescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.gpt88.cc",
  apiKey: process.env.GPT88_API_KEY,
});

const resp = await client.chat.completions.create({
  model: "claude-opus-4-8",
  messages: [
    { role: "system", content: "You are a concise assistant." },
    { role: "user", content: "Introduce gpt88.cc in under 30 words" },
  ],
  temperature: 0.7,
  max_tokens: 1024,
});

console.log(resp.choices[0].message.content);
console.log("usage:", resp.usage?.total_tokens, "tokens");

Streaming

stream.tstypescript
const stream = await client.chat.completions.create({
  model: "claude-opus-4-8",
  stream: true,
  messages: [{ role: "user", content: "Tell a short joke about API gateways" }],
});

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

Function calling

tools.tstypescript
const tools = [{
  type: "function",
  function: {
    name: "get_weather",
    description: "Get the current weather for a city",
    parameters: {
      type: "object",
      properties: { city: { type: "string" } },
      required: ["city"],
    },
  },
}];

const resp = await client.chat.completions.create({
  model: "claude-opus-4-8",
  messages: [{ role: "user", content: "What's the weather in Shanghai?" }],
  tools,
  tool_choice: "auto",
});

const call = resp.choices[0].message.tool_calls?.[0];
if (call) {
  console.log("model wants:", call.function.name, call.function.arguments);
}

Backoff retries

retry.tstypescript
import OpenAI, { APIError, RateLimitError } from "openai";

const client = new OpenAI({
  baseURL: "https://api.gpt88.cc",
  maxRetries: 0,
});

async function callWithRetry(messages, model = "claude-opus-4-8", maxRetries = 3) {
  for (let attempt = 0; attempt < maxRetries; attempt++) {
    try {
      return await client.chat.completions.create({ model, messages });
    } catch (err) {
      if (err instanceof RateLimitError) {
        await sleep(2 ** attempt * 1000);
        continue;
      }
      if (err instanceof APIError && err.status >= 500) {
        await sleep(2 ** attempt * 1000);
        continue;
      }
      throw err;
    }
  }
  throw new Error("max retries exceeded");
}

const sleep = (ms) => new Promise(r => setTimeout(r, ms));

For error categories and retry advice, see Error Codes.

Next.js Edge / serverless

A streaming Edge or serverless route is the most common integration shape for React applications.

app/api/chat/route.tstypescript
import OpenAI from "openai";

export const runtime = "edge";

const client = new OpenAI({
  baseURL: "https://api.gpt88.cc",
  apiKey: process.env.GPT88_API_KEY,
});

export async function POST(req: Request) {
  const { messages } = await req.json();

  const stream = await client.chat.completions.create({
    model: "claude-opus-4-8",
    stream: true,
    messages,
  });

  const encoder = new TextEncoder();
  const body = new ReadableStream({
    async start(controller) {
      for await (const chunk of stream) {
        const delta = chunk.choices[0].delta.content ?? "";
        controller.enqueue(encoder.encode(delta));
      }
      controller.close();
    },
  });

  return new Response(body, {
    headers: { "content-type": "text/plain; charset=utf-8" },
  });
}