Node.js / TypeScript SDK

Use the official openai npm package in Next.js App Router, Express, or NestJS for full-duplex streaming.

npm install openai

Next.js 15 App Router streaming route

// app/api/chat/route.ts
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.CALLAI_API_KEY,
  baseURL: 'https://api.callaiapi.com/v1',
});

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

  const response = await openai.chat.completions.create({
    model: 'claude-3-7-sonnet',
    messages,
    stream: true,
  });

  const encoder = new TextEncoder();
  const readable = new ReadableStream({
    async start(controller) {
      for await (const chunk of response) {
        const text = chunk.choices[0]?.delta?.content || '';
        controller.enqueue(encoder.encode(text));
      }
      controller.close();
    },
  });

  return new Response(readable, {
    headers: { 'Content-Type': 'text/plain; charset=utf-8' },
  });
}
💡AbortController client cancel

Bind an AbortSignal on the client request. When the user hits Stop, abort disconnects from CallAI immediately and stops billing.

Budget tokens for Node.js production traffic?
Compare multi-turn costs across models with the free calculator.
Open cost calculator →