Clatter LabsHelp Center

How do I read response headers like X-Request-Id in JavaScript?

How-toUpdated

withResponse() gives you the parsed data and the HTTP response with its headers.

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

const { data, response } = await client.chat.completions
  .create({ model: "supercosmos-flash", messages: [{ role: "user", content: "Hi" }] })
  .withResponse();

console.log(response.headers.get("x-request-id"));
console.log(response.headers.get("x-ratelimit-remaining"), "/", response.headers.get("x-ratelimit-limit"));
const notice = response.headers.get("x-clatter-notice"); // "abuse_warning" when a warning was issued
if (notice) console.warn(decodeURIComponent(response.headers.get("x-clatter-notice-text") ?? ""));

These headers are exposed for CORS, so browser code can read them too.

Every JavaScript example uses the same client (base URL, key and the X-Clatter-Account header); see Set up the JavaScript SDK. Full parameter list: Create a chat completion.

Good to know#

  • Every call shows in Console › Logs and Usage as Model API, with its price. See what you spent
  • Keep keys on your server, never in a browser or mobile app. Why
  • New accounts get $5 of free credit; top up from $1 to $800 per purchase in Console › Billing › Credits. Billing

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.