Fluxio
← Volver al blog

Streaming in Node.js with the OpenAI SDK

SSE y APIsEN

§1 Choose Chat Completions for This Streaming Walkthrough

This walkthrough builds a terminal application that prints an AI response as text arrives. The OpenAI SDK exposes streaming Chat Completions through an asynchronous iterator, so your application can process incremental chunks instead of waiting for a complete message. The underlying transport uses server-sent events. ([developers.openai.com](https://developers.openai.com/api/docs/guides/streaming-responses?utm_source=openai))

OpenAI recommends the Responses API for new OpenAI-native applications. Here, we use Chat Completions to demonstrate the OpenAI-style interface relevant to a relay integration; do not assume that compatible providers implement every Responses API feature. ([developers.openai.com](https://developers.openai.com/api/docs/guides/migrate-to-responses?lang=javascript&tool-use=chat-completions&update-item-definitions=chat-completions&update-multiturn=responses&utm_source=openai))

§2 Step 1: Install the OpenAI SDK in Node.js

Create a project directory, run npm init -y, and install the official JavaScript package with npm install openai. Create a file named stream.mjs for the example. The official SDK documentation demonstrates this package and file format for server-side JavaScript. ([developers.openai.com](https://developers.openai.com/api/docs/libraries?utm_source=openai))

Keep credentials on your server. Do not move this client into browser code or expose its API key through frontend environment variables. The SDK disables browser use by default because exposing secret credentials allows other users to extract and misuse them. ([developers.openai.com](https://developers.openai.com/api/reference/typescript))

§3 Step 2: Configure the API Key, Endpoint, and Model

Define three environment variables before launching your application: AI_API_KEY, AI_BASE_URL, and AI_MODEL. Keeping these settings outside the script makes the example easier to reuse across deployment environments.

For the Fluxio configuration supplied for this walkthrough, use its API origin with /v1 appended as the base URL. Set AI_MODEL to the exact API identifier shown in your account, rather than guessing an identifier from a display name such as Gemini 2.5 Flash.

Fluxio is presented in the supplied product brief as a streaming-first, pay-per-token relay with one key for multiple model providers. That makes it a relevant option when your application needs a shared integration rather than separate provider clients. Treat endpoint and feature compatibility as explicit integration checks.

§4 Step 3: Create the Client and Request a Stream

Add the following JavaScript to stream.mjs. The environment checks deliberately fail before making a request if configuration is missing.

import OpenAI from 'openai'; const apiKey = process.env.AI_API_KEY; const baseURL = process.env.AI_BASE_URL; const model = process.env.AI_MODEL; if (!apiKey || !baseURL || !model) { throw new Error('Set AI_API_KEY, AI_BASE_URL, and AI_MODEL.'); } const client = new OpenAI({ apiKey, baseURL });

Next, append the request and consumer below. Setting stream: true makes Chat Completions return a stream of completion chunks. ([developers.openai.com](https://developers.openai.com/api/reference/typescript/resources/chat/subresources/completions?utm_source=openai))

try { const stream = await client.chat.completions.create({ model, messages: [{ role: 'user', content: 'Explain Node.js streams in three short sentences.' }], stream: true }); for await (const chunk of stream) { const text = chunk.choices[0]?.delta?.content; if (text) process.stdout.write(text); } process.stdout.write('\n'); } catch (error) { console.error('Streaming request failed:', error instanceof Error ? error.message : String(error)); process.exitCode = 1; }

§5 Step 4: Run Node.js and Inspect Text Deltas

Run node stream.mjs after setting your environment variables. Read incremental text from delta.content, not message.content. Some chunks contain role information or no text, which is why the example checks the value before printing. Do not assume each chunk represents exactly one token. ([developers.openai.com](https://developers.openai.com/api/docs/guides/streaming-responses?utm_source=openai))

For your first test, request a sufficiently long explanation to observe incremental output. Then test an invalid model identifier and an invalid credential separately, confirming that your application reports failures rather than silently displaying an empty answer.

§6 Step 5: Harden the Node.js Streaming Integration

  • – Keep the request and iteration inside the same error-handling boundary.
  • – Preserve partially displayed text when reporting an interrupted response.
  • – Test cancellation, slow consumers, and buffering before connecting a browser interface.
  • – Measure first-text latency separately from total response duration.

If you later request usage information with stream_options.include_usage, accommodate a final usage chunk whose choices array is empty. Confirm that your relay supports this option before relying on it for accounting. ([developers.openai.com](https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create?utm_source=openai))

Sign up for Fluxio to try streaming.

Crear una cuenta de Fluxio →

Más artículos de Corrientes