Express
Express.js with an OpenAI-compatible uncensored API
Build an Express 5 API on Abliterator with the openai package: a JSON route, an SSE relay that aborts when the client leaves, and error middleware for Abliterator errors.
Create one OpenAI client with baseURL "https://abliterator.ai/v1" and apiKey process.env.ABL_API_KEY at startup, call client.chat.completions.create in your routes, and relay streams to the browser as Server-Sent Events.
Express has no LLM integration of its own, so the clean approach is the official openai package pointed at Abliterator, wrapped in ordinary routes. These examples use Express 5 with ES modules on Node 22 and were run end to end against a stand-in server.
Install
npm install express openai
export ABL_API_KEY="sk-abl-v1-..."
A JSON route
Create the client once at startup and reuse it; it keeps HTTP connections alive between requests.
import express from "express";
import OpenAI from "openai";
const abliterator = new OpenAI({ baseURL: "https://abliterator.ai/v1", apiKey: process.env.ABL_API_KEY });
const app = express();
app.use(express.json({ limit: "1mb" }));
app.post("/api/explain-log", async (req, res) => {
const completion = await abliterator.chat.completions.create({
model: "deepseek-v4.1-flash-abliterated",
messages: [
{ role: "system", content: "Explain log lines for a junior SOC analyst. Say what is normal and what is suspicious." },
{ role: "user", content: String(req.body.line ?? "") },
],
max_tokens: 500,
});
res.json({ explanation: completion.choices[0].message.content, cost: completion.usage.cost });
});
app.listen(3000, () => console.log("listening on :3000"));
curl -s localhost:3000/api/explain-log -H 'Content-Type: application/json' \
-d '{"line":"sshd[811]: Accepted publickey for deploy from 203.0.113.9 port 50122"}'
Relay a stream as Server-Sent Events
The route reads Abliterator's stream and writes each text delta as an SSE event. If the client disconnects, controller.abort() cancels the upstream request:
import express from "express";
import OpenAI from "openai";
const abliterator = new OpenAI({ baseURL: "https://abliterator.ai/v1", apiKey: process.env.ABL_API_KEY });
const app = express();
app.use(express.json());
app.post("/api/chat", async (req, res) => {
const stream = await abliterator.chat.completions.create({
model: "kimi-k3.1-pro-abliterated",
messages: req.body.messages,
stream: true,
});
res.set({ "Content-Type": "text/event-stream", "Cache-Control": "no-cache", Connection: "keep-alive" });
res.flushHeaders();
res.on("close", () => stream.controller.abort());
try {
for await (const chunk of stream) {
const text = chunk.choices[0]?.delta?.content;
if (text) res.write(`data: ${JSON.stringify({ text })}\n\n`);
if (chunk.usage) res.write(`event: usage\ndata: ${JSON.stringify(chunk.usage)}\n\n`);
}
res.write("data: [DONE]\n\n");
} catch (err) {
if (!(err instanceof OpenAI.APIUserAbortError)) throw err;
} finally {
res.end();
}
});
app.listen(3000);
On the client, read it with fetch and a stream reader; EventSource only supports GET. Disable proxy buffering (for nginx, X-Accel-Buffering: no) or the events arrive all at once.
Error middleware for Abliterator errors
The SDK throws OpenAI.APIError with Abliterator's HTTP status. Express 5 forwards rejected promises to error middleware, so one handler can map them:
import express from "express";
import OpenAI from "openai";
const abliterator = new OpenAI({ baseURL: "https://abliterator.ai/v1", apiKey: process.env.ABL_API_KEY, maxRetries: 1 });
const app = express();
app.use(express.json());
app.post("/api/ask", async (req, res) => {
const r = await abliterator.chat.completions.create({
model: req.body.fast ? "deepseek-v4.1-flash-abliterated" : "kimi-k3.1-pro-abliterated",
messages: [{ role: "user", content: String(req.body.question ?? "") }],
});
res.json({ answer: r.choices[0].message.content });
});
app.use((err, req, res, next) => {
if (err instanceof OpenAI.APIError) {
const retryAfter = err.headers?.get("retry-after");
if (retryAfter) res.set("Retry-After", retryAfter);
const status = err.status === 401 || err.status === 402 ? 503 : err.status ?? 502;
return res.status(status).json({ error: err.message });
}
next(err);
});
app.listen(3000);
Mapping 401 and 402 to 503 keeps "our key is out of credit" from looking like the end user's auth problem. The codes are listed in /docs/errors.
Troubleshooting
req.bodyis undefined.express.json()is missing or registered after the route.- Events arrive in one burst. Compression middleware or a reverse proxy is buffering. Exclude the SSE route from
compression()and turn off proxy buffering. APIUserAbortErrorin logs. Expected when a client disconnects mid-stream; the example swallows it.- Requests hang when many users stream at once. Each stream holds one of your plan's concurrent-request slots until it ends (/pricing). Queue or reject extra requests instead of letting them pile up.
- Express 4. Async errors are not forwarded there; wrap handlers in
try/catchand callnext(err).
Frequently asked questions
Do I need express-async-handler or try/catch in every route?
Not in Express 5. A rejected promise from an async route is passed to your error middleware automatically, so one handler can turn Abliterator's API errors into responses.
How do I stop paying for tokens after the user closes the tab?
Listen for the response's close event and call stream.controller.abort(). The upstream request to Abliterator is cancelled and the loop exits, as in the SSE example on this page.
Should the browser call Abliterator directly?
No. The key would be visible to anyone. Keep ABL_API_KEY on the server and expose only the routes you choose, with your own auth and rate limits in front.