Nirmos
Examples and recipes

Build a basic AI chatbot

Run an interactive TypeScript chatbot with explicit conversation history.

Build a terminal chatbot that sends the current conversation to Gateway and prints each reply. History stays in this process; it is not persistent Memory.

Prerequisites and Dashboard setup

Generate a service or personal API key with gateway:execute in Dashboard settings. Configure a provider credential in the same organization and choose an accessible chat model. Use its provider-qualified identifier as NIRMOS_CHAT_MODEL; model discovery explains the identifier.

Install the published package and a TypeScript runner in a new project:

npm install @nirmos/sdk
npm install --save-dev tsx typescript @types/node

Use a trusted Node.js process with Fetch and Web Crypto. Read secrets from its environment or secret store. Optional NIRMOS_API_URL and NIRMOS_RUNTIME_URL select another deployment; leave them unset for hosted defaults. The SDK does not load .env files automatically.

Complete code

Save as chatbot.ts.

chatbot.ts
import { Nirmos, NirmosError } from "@nirmos/sdk";

function requiredEnv(name: string): string {
  const value = process.env[name];
  if (!value) throw new Error(`Set ${name} before running this recipe`);
  return value;
}
function configuredClient(): Nirmos {
  return new Nirmos({
    auth: { type: "api-key", key: requiredEnv("NIRMOS_API_KEY") },
    apiBaseUrl: process.env.NIRMOS_API_URL,
    runtimeBaseUrl: process.env.NIRMOS_RUNTIME_URL,
    maxRetries: 0,
  });
}
function report(error: unknown): never {
  if (error instanceof NirmosError) {
    console.error({ code: error.code, status: error.status, requestId: error.requestId });
  }
  throw error;
}

import { createInterface } from "node:readline/promises";
import type { ChatMessage } from "@nirmos/sdk";

export async function chatbot(
  client: Nirmos,
  model: string,
  questions: AsyncIterable<string>,
  write: (text: string) => void = console.log,
) {
  const messages: ChatMessage[] = [{ role: "system", content: "Answer briefly and accurately." }];
  for await (const line of questions) {
    const question = line.trim();
    if (!question) continue;
    if (question === "/exit") break;
    messages.push({ role: "user", content: question });
    const completion = await client.gateway.chat.create({
      model, messages, maxOutputTokens: 300,
    }, { timeoutMs: 30_000 });
    const reply = completion.choices[0]?.message;
    if (!reply || !completion.outputText) throw new Error("No text reply was returned");
    messages.push(reply);
    write(completion.outputText);
    // Keep the system message and the latest six turns for this small demo.
    if (messages.length > 13) messages.splice(1, messages.length - 13);
  }
}

if (process.env.NIRMOS_RUN_RECIPE === "1") {
  const input = createInterface({ input: process.stdin, output: process.stdout });
  try {
    console.log("Enter a question; use /exit to finish.");
    await chatbot(configuredClient(), requiredEnv("NIRMOS_CHAT_MODEL"), input);
  } catch (error) { report(error); }
  finally { input.close(); }
}

Run it

Set NIRMOS_API_KEY and NIRMOS_CHAT_MODEL in your server environment, then run:

NIRMOS_RUN_RECIPE=1 npx tsx chatbot.ts

Ask a question, then ask a follow-up about the reply. The second request includes the earlier messages. /exit stops the program.

Behavior and limits

Every turn can incur provider charges. The demo retains six recent turns, which is a message-count limit rather than a token budget. Production applications should budget tokens for their selected model, authorize their users, and propagate cancellation when a caller disconnects.

A request failure stops this program and reports safe SDK identifiers. Responses depend on your provider/model; no fixed generated wording is guaranteed. No persistent Nirmos resources are created.

Chat guide · Streaming · Gateway methods

On this page