JavaScript

Plain fetch from any JavaScript runtime, server side.

Any JavaScript runtime with fetch can call Top Billing directly — no package needed. Keep the key on the server: never ship a tb_ key to a browser.

Chat

javascript
const res = await fetch("https://api.topbilling.in/v1/chat/completions", {
  method: "POST",
  headers: { "Authorization": "Bearer " + process.env.TB_API_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({ model: "<model-id>", messages: [{ role: "user", content: "Hello" }] }),
});
if (!res.ok) throw new Error((await res.json()).error?.message);
const data = await res.json();
console.log(data.choices[0].message.content, res.headers.get("x-request-id"));

Streaming

javascript
const res = await fetch("https://api.topbilling.in/v1/chat/completions", {
  method: "POST",
  headers: { "Authorization": "Bearer " + process.env.TB_API_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({ model: "<model-id>", stream: true, stream_options: { include_usage: true }, messages }),
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
for (;;) {
  const { value, done } = await reader.read();
  if (done) break;
  process.stdout.write(decoder.decode(value)); // data: {...} lines, ending with data: [DONE]
}

For typed clients, use the official SDKs from Node.js.