TypeScript SDK
Getting started
Install the SDK, configure a client, and make your first chat completion call in under a minute.
1. Install
npm install @qubittron/bastion-sdk
# or
pnpm add @qubittron/bastion-sdk
# or
bun add @qubittron/bastion-sdk
# or
yarn add @qubittron/bastion-sdkRequirements:
- Node ≥ 20 (uses global
fetchandReadableStream) - Bun, Deno (with Node compatibility), or any edge runtime exposing
fetch - Browsers: any with global
fetch— but see Environments before shipping a browser bundle (your API key would leak).
2. Get an API key
- Sign up at qubittron.ai and verify your phone number.
- Open the dashboard → API keys → Create key. Keys start with
qbt_and are shown only once. - Set the key in your environment:
export BASTION_API_KEY=qbt_your_key_hereThe SDK reads BASTION_API_KEY automatically when you don't pass apiKey to the constructor.
3. Construct a client
import { Bastion } from "@qubittron/bastion-sdk";
const client = new Bastion(); // reads BASTION_API_KEY from env
// or, explicitly:
const client = new Bastion({
apiKey: process.env.BASTION_API_KEY,
});If apiKey is missing and BASTION_API_KEY is unset, the constructor throws synchronously. See Bastion API for all options.
4. Make your first call
const res = await client.chat.completions.create({
model: "gpt-oss-120b",
messages: [{ role: "user", content: "Reply with exactly: ok" }],
max_tokens: 16,
});
console.log(res.choices[0]?.message.content);
console.log(res.usage); // { prompt_tokens, completion_tokens, total_tokens }5. Stream tokens
Pass stream: true to get an AsyncIterable over ChatCompletionChunks:
const stream = await client.chat.completions.create({
model: "gpt-oss-120b",
messages: [{ role: "user", content: "Write a haiku about Canada." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta.content ?? "");
}See the streaming guide for cancellation, partial-tool-call handling, and reasoning tokens.
6. Handle errors
import {
AuthenticationError,
RateLimitError,
} from "@qubittron/bastion-sdk";
try {
await client.chat.completions.create({
model: "gpt-oss-120b",
messages: [{ role: "user", content: "hi" }],
});
} catch (err) {
if (err instanceof RateLimitError) {
// back off
} else if (err instanceof AuthenticationError) {
// refresh / rotate the key
} else {
throw err;
}
}Every API error is a subclass of BastionError and carries status, code, type, and the raw body. See error handling.