Prompt
building backend with chat completions api
Latest observation
Oct 8, 2026
OpenAI APIWeb search: off
If you’re building a backend around the Chat Completions API, a good setup usually looks like this:
Basic flow
- Frontend sends user message to your backend.
- Backend validates/authenticates the request.
- Backend calls the Chat Completions API with:
- a system message
- conversation history
- the latest user message
- Backend returns the assistant response to the frontend.
- Optionally store the conversation in your database.
Minimal Node.js example
import OpenAI from "openai";
import express from "express";
const app = express();
app.use(express.json());
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
app.post("/chat", async (req, res) => {
try {
const { messages } = req.body; // [{ role: "user", content: "Hello" }]
const response = await client.chat.completions.create({
model: "gpt-4o-mini",
messages,
});
res.json({
reply: response.choices[0].message.content,
});
} catch (err) {
console.error(err);
res.status(500).json({ error: "Chat request failed" });
}
});
app.listen(3000, () => console.log("Server running on port 3000"));
Example request body
{
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "Write a haiku about rain." }
]
}
Recommended backend practices
- Never expose your API key to the client.
- Keep chat history on the server if you need persistence.
- Trim old messages to avoid token bloat.
- Add rate limiting and auth.
- Handle timeouts/retries gracefully.
- If you want token-by-token UX, use streaming.
Streaming example
const stream = await client.chat.completions.create({
model: "gpt-4o-mini",
messages,
stream: true,
});
for await (const chunk of stream) {
const token = chunk.choices[0]?.delta?.content || "";
process.stdout.write(token);
}
Common backend architecture
- Routes:
/chat,/conversations/:id - DB tables:
- users
- conversations
- messages
- Services:
openaiServiceconversationServiceauthService
If you want, I can give you:
- a full Express backend template
- a Python/FastAPI version
- a production-ready architecture with DB + auth + streaming