Clatter LabsHelp Center

How do I call the Model API from a Next.js route handler?

How-toUpdated
// app/api/chat/route.js  (server-side only)
import OpenAI from "openai";

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 },
});

export async function POST(req) {
  const { message } = await req.json();
  const stream = await client.chat.completions.create({
    model: "supercosmos-flash",
    messages: [{ role: "user", content: message }],
    stream: true,
  });
  const body = new ReadableStream({
    async start(controller) {
      const enc = new TextEncoder();
      for await (const c of stream) {
        const t = c.choices[0]?.delta?.content;
        if (t) controller.enqueue(enc.encode(t));
      }
      controller.close();
    },
  });
  return new Response(body, { headers: { "Content-Type": "text/plain; charset=utf-8" } });
}

Keep CLATTER_API_KEY in server environment variables (never a NEXT_PUBLIC_ variable).

Good to know#

  • Retry only 429 and 5xx errors, with backoff; honour Retry-After when it's sent. Which errors to retry
  • Every response has an X-Request-Id; log it so a request can be found later. Why log it
  • Your key's account is responsible for your end users' text under the Abuse policy. What it means for apps

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.