Prompt

building backend with chat completions api

Artificial Intelligence / AI Platforms1 observationLast seen Oct 8, 2026

Latest observation

Oct 8, 2026OpenAI APIWeb search: off

If you’re building a backend around the Chat Completions API, a good setup usually looks like this:

Basic flow

  1. Frontend sends user message to your backend.
  2. Backend validates/authenticates the request.
  3. Backend calls the Chat Completions API with:
    • a system message
    • conversation history
    • the latest user message
  4. Backend returns the assistant response to the frontend.
  5. 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:
    • openaiService
    • conversationService
    • authService

If you want, I can give you:

  1. a full Express backend template
  2. a Python/FastAPI version
  3. a production-ready architecture with DB + auth + streaming

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.