Skip to content

Docs / Node.js

BrokenGPT Node.js API quickstart

Call the BrokenGPT chat-completions API from Node.js, stream server-sent events, protect your key, and prepare the integration for production.

UPDATED 01 Aug 20267 MIN READTECHNICAL GUIDE
01

Install the SDK in a server-side Node.js project.

  1. Create a scoped API key in the BrokenGPT dashboard.
  2. Install the client with npm install openai.
  3. Put BROKENGPT_API_KEY in your host secret store or local .env file that is excluded from Git.

INSTALL

npm install openai

# .env.local (never commit this value)
BROKENGPT_API_KEY=your-key
02

Create the client with the BrokenGPT base URL.

Run this code only on your server, worker, or trusted command line. The base URL is https://brokengpt.com/v1 and the public model alias is broken-one.

NODE.JS

import OpenAI from "openai";

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

const response = await client.chat.completions.create({
  model: "broken-one",
  messages: [{ role: "user", content: "Give me three test cases." }],
});

console.log(response.choices[0]?.message?.content);
03

Iterate streamed chunks and handle an interrupted stream.

In an HTTP application, propagate caller cancellation and avoid assuming every stream ends normally. Keep partially generated content separate from committed application state until your completion policy accepts it.

NODE.JS STREAM

const stream = await client.chat.completions.create({
  model: "broken-one",
  messages: [{ role: "user", content: "Draft a concise release note." }],
  stream: true,
});

for await (const chunk of stream) {
  const text = chunk.choices[0]?.delta?.content;
  if (text) process.stdout.write(text);
}
04

Make the integration observable and bounded.

  • Set an application timeout and abort requests that no longer have a caller.
  • Handle 400, 401, 403, 404, 429, and 5xx responses by category.
  • Use bounded exponential backoff only for retryable responses.
  • Validate JSON and tool arguments before they reach databases, shells, or external APIs.
  • Track latency, usage, and request IDs without logging credentials.

STRAIGHT ANSWERS

Frequently asked questions

01Does this work in browser JavaScript?

Do not expose a BrokenGPT key in browser code. Call BrokenGPT from a trusted server endpoint.

02Does the endpoint support streaming?

Yes. The chat-completions endpoint streams server-sent events when stream is true.

03Can I use max_completion_tokens?

The supported chat-completions surface accepts max_completion_tokens as an alias for max_tokens.

BUILD WITH THE LIVE CONTRACT

Test one real request end to end.

Create a scoped key, send a representative prompt, and verify output, usage, limits, and error handling before expanding the integration.

Create an account