Clatter LabsHelp Center

How do I call the Model API from an Express server?

How-toUpdated
import express from "express";
import OpenAI from "openai";

const app = express();
app.use(express.json());
const client = new OpenAI({
  baseURL: "https://api.gateway.clatterlabs.xyz/v1",
  apiKey: process.env.CLATTER_API_KEY,
  defaultHeaders: { "X-Clatter-Account": process.env.CLATTER_ACCOUNT_ID },
});

app.post("/ask", async (req, res) => {
  try {
    const r = await client.chat.completions.create({
      model: "supercosmos-flash",
      messages: [{ role: "user", content: req.body.question }],
    });
    res.json({ answer: r.choices[0].message.content, usage: r.usage });
  } catch (e) {
    res.status(e.status ?? 500).json({ error: e.code ?? "error" });
  }
});
app.listen(3000);

Good to know#

  • Keep keys on your server, never in a browser or mobile app. Why
  • If calls fail unexpectedly, check status.clatterlabs.com before debugging further.
  • Retry only 429 and 5xx errors, with backoff; honour Retry-After when it's sent. Which errors to retry

Still need help?#

We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Related articles

Still need help?

Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyz
For deleting your whole account or the data of one service.