TypeScript SDK
Quickstart
Send chat, stream output, and use managed prompts with Nirmos.
Initialize the SDK
import { Nirmos } from "@nirmos/sdk";
const nirmos = new Nirmos({
apiKey: process.env.NIRMOS_API_KEY!,
});Send a chat request
const completion = await nirmos.gateway.chat.create({
model: "openai/gpt-4.1-mini",
messages: [
{
role: "user",
content: "Explain semantic caching in two sentences.",
},
],
temperature: 0.3,
maxOutputTokens: 200,
});
console.log(completion.outputText);
console.log(completion.usage.totalTokens);
console.log(completion.metadata.requestId);outputText joins text from all returned choices for simple applications. Use completion.choices when handling multiple choices, tool calls, or finish reasons.
Stream a response
const stream = await nirmos.gateway.chat.stream({
model: "anthropic/claude-sonnet-4",
messages: [{ role: "user", content: "Write a short launch announcement." }],
});
for await (const event of stream) {
if (event.type === "content.delta") {
process.stdout.write(event.delta ?? "");
}
}
const completion = await stream.finalResponse();
console.log(completion.usage);The stream emits normalized events and assembles the same ChatCompletion shape returned by chat.create.
Use a managed prompt
Create and activate prompts through the dashboard or SDK. Reference the prompt by ID or slug from a gateway call:
const completion = await nirmos.gateway.chat.create({
model: "openai/gpt-4.1-mini",
prompt: {
id: "support-reply",
variables: {
customerName: "Asha",
tone: "calm",
},
},
messages: [
{
role: "user",
content: "My order has not arrived.",
},
],
});The SDK retrieves the active prompt, caches it, renders its variables, inserts its messages before the request messages, and sends the combined request through Nirmos Gateway.
Handle errors
import { APIError, RateLimitError } from "@nirmos/sdk";
try {
await nirmos.gateway.chat.create({
model: "openai/gpt-4.1-mini",
messages: [{ role: "user", content: "Hello" }],
});
} catch (error) {
if (error instanceof RateLimitError) {
console.error("Retry after", error.retryAfterMs);
} else if (error instanceof APIError) {
console.error(error.code, error.requestId, error.message);
}
throw error;
}Read error handling before deploying.