Node JS AI API: Streaming, Tools and Errors in Express
This node js ai api tutorial takes you from an empty folder to an Express route that streams chat replies, calls a tool and handles rate-limit errors. You only change the base URL and key in the official OpenAI package, then send model uncensored.
UpdatedUnfiltered AI

Key points
- Install the openai package and set baseURL to https://api.unfilteredaiapi.com/v1.
- Stream with stream: true and forward chunks to the browser as server-sent events.
- Tool calls arrive as tool_calls; return results with role tool.
- 429 means rate_limited, concurrency or temporary overload; retry after the Retry-After delay.
Setup: install and send the first request
You need Node 18 or newer. Create a project, install the SDK and Express, and switch to ES modules.
mkdir ai-node && cd ai-node
npm init -y
npm pkg set type=module
npm install openai express
# base URL: https://api.unfilteredaiapi.com/v1 model: uncensored
export AI_API_KEY="YOUR_KEY"
Get a key from the API key page. New accounts can claim $0.50 of trial credit with one click, valid for 7 days. Then create client.js:
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.unfilteredaiapi.com/v1",
apiKey: process.env.AI_API_KEY,
});
const res = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Give me three names for a tavern keeper." }],
});
console.log(res.choices[0].message.content);
Run it with node client.js. Each snippet below defines the client again so that it runs on its own. That is the whole migration if you already use the OpenAI package: a different baseURL, your key and the model ID. The service is OpenAI-compatible and text only, with a 100,000 token context window. See the docs for every supported method.
Stream the reply
Streaming sends tokens as they are produced, so users see text immediately. Set stream: true and iterate over the chunks:
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.unfilteredaiapi.com/v1",
apiKey: process.env.AI_API_KEY,
});
const stream = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Describe a rainy harbor town." }],
stream: true,
stream_options: { include_usage: true },
});
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content;
if (delta) process.stdout.write(delta);
if (chunk.usage) console.log("\nusage:", chunk.usage);
}
Usage arrives in the last chunk, which lets you log real token counts per request. Keep one fact in mind: a streamed request occupies its concurrency slot until the stream ends. If the client disconnects, abort the upstream call so the slot frees quickly.
An Express route that streams to the browser
Here is a small server with a POST /chat route. It forwards the stream as server-sent events and cancels the upstream request when the browser closes the connection.
import express from "express";
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.unfilteredaiapi.com/v1",
apiKey: process.env.AI_API_KEY,
});
const app = express();
app.use(express.json({ limit: "1mb" }));
app.post("/chat", async (req, res) => {
const { messages } = req.body;
if (Array.isArray(messages) === false) return res.status(400).json({ error: "messages required" });
res.set({
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
});
const controller = new AbortController();
req.on("close", () => controller.abort());
try {
const stream = await client.chat.completions.create(
{ model: "uncensored", messages, stream: true },
{ signal: controller.signal }
);
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content;
if (delta) res.write("data: " + JSON.stringify({ delta }) + "\n\n");
}
res.write("data: [DONE]\n\n");
} catch (err) {
res.write("data: " + JSON.stringify({ error: err.status || 500 }) + "\n\n");
} finally {
res.end();
}
});
app.listen(3000, () => console.log("listening on 3000"));
Never put the key in browser code. The route above keeps it on the server, where it belongs. The same pattern is shown in Python in the FastAPI chatbot backend article.
Call a tool
Tool calling follows the OpenAI format: you describe functions in tools, the model replies with tool_calls, you run the function and send the result back as a message with role: "tool".
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.unfilteredaiapi.com/v1",
apiKey: process.env.AI_API_KEY,
});
const tools = [{
type: "function",
function: {
name: "roll_dice",
description: "Roll a die with the given number of sides",
parameters: {
type: "object",
properties: { sides: { type: "integer" } },
required: ["sides"],
},
},
}];
const messages = [{ role: "user", content: "Roll a d20 for my attack." }];
const first = await client.chat.completions.create({
model: "uncensored", messages, tools, tool_choice: "auto",
});
const msg = first.choices[0].message;
if (msg.tool_calls) {
messages.push(msg);
for (const call of msg.tool_calls) {
const { sides } = JSON.parse(call.function.arguments);
const result = 1 + Math.floor(Math.random() * sides);
messages.push({ role: "tool", tool_call_id: call.id, content: String(result) });
}
const second = await client.chat.completions.create({ model: "uncensored", messages, tools });
console.log(second.choices[0].message.content);
}
Always parse arguments inside a try block in real code, since models can return malformed JSON now and then. For structured output without tools, set response_format: { type: "json_object" } and mention JSON in your prompt. Game scenes that call dice or inventory functions are covered in building game NPCs and interactive fiction.
Handle errors and rate limits
The API returns HTTP 429 in three situations. Error code rate_limited means too many requests per minute: 300 for a full key, 60 for a trial key. Code concurrency means too many at once: 8 for a full key, 2 for a trial key. On temporary overload you also get 429 with a Retry-After: 5 header. The SDK exposes the status on the thrown error, so a small wrapper handles all three:
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.unfilteredaiapi.com/v1",
apiKey: process.env.AI_API_KEY,
});
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
export async function chatWithRetry(params, tries = 4) {
for (let i = 0; i < tries; i++) {
try {
return await client.chat.completions.create({ model: "uncensored", ...params });
} catch (err) {
if (err.status === 429 && i < tries - 1) {
const wait = Number(err.headers?.["retry-after"]) || 2 ** i;
await sleep(wait * 1000);
continue;
}
throw err;
}
}
}
The wrapper waits for the delay the server asks for and falls back to exponential backoff. Note that the OpenAI package also retries some errors by itself; set maxRetries on the client if you want to control this. Other statuses, such as 401 for a bad key, should not be retried. A deeper look at limits is in the FAQ.
Run and test the route
Start the server with node server.js and test it from a second terminal. The -N flag turns off curl buffering so you see events arrive one by one:
# the server calls https://api.unfilteredaiapi.com/v1 with model uncensored
curl -N http://localhost:3000/chat -H "Content-Type: application/json" -d '{"messages":[{"role":"user","content":"Write a two-line riddle."}]}'
In the browser, read the stream with fetch and a reader, or with a small SSE helper library. Because the route is a POST, the native EventSource object does not fit, so use fetch and decode the chunks yourself.
Production habits
- Keep the key in an environment variable or a secret store, never in the repository.
- Cap the request body. The API accepts up to 8 MB, but your own route should accept far less.
- Limit the history you send. Trim old turns before they cost you money or fill the 100,000 token window.
- Queue or reject extra work when you already have 8 streams open, instead of letting the API answer 429 to your users.
- Log the
X-Request-Idresponse header, so support can trace a single request.
Check your balance and cost
Every response carries an X-Balance-USD header, and GET /v1/balance returns the balance directly. Pricing is $0.15 per 1M input tokens and $0.50 per 1M output tokens, charged from prepaid credit, and failed or refused requests are free. A chat turn of 800 input and 250 output tokens costs $0.000245, so 10,000 turns cost $2.45. Log usage from each response and you can predict your bill within a few cents. The pricing page lists top-ups from $20 to $500.
More guides
Questions and answers
Which npm package should I use?
The official openai package works. Set baseURL to https://api.unfilteredaiapi.com/v1, pass your key and use the model ID uncensored.
Can I stream responses from Node?
Yes. Pass stream: true and iterate with for await. Add stream_options with include_usage to get token counts in the last chunk.
Does the API support function calling?
Yes. Send tools and tool_choice, read tool_calls from the reply and return results as messages with role tool.
What should my server do on HTTP 429?
Wait for the Retry-After value if present, otherwise back off exponentially, then retry a few times. Reduce parallel requests if the error code is concurrency.
Your key is one form away
Create an account, copy the key, change the base URL. That is the whole setup.