# Starter examples

Three small programs you can copy and run. Each one uses the official OpenAI library with Zalmir's address. Create a key in [API keys](https://zalmir.ai/account/keys), add credits, then set the key in your terminal: `export ZALMIR_API_KEY="your key"`. Swap the model for any ID from [Models](https://zalmir.ai/models).

## Stream an answer in Node

Needs Node 18 or newer. Run `npm install openai`, save this as `chat.mjs`, then run `node chat.mjs`.

```chat.mjs
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://zalmir.ai/api/v1",
  apiKey: process.env.ZALMIR_API_KEY,
});

const stream = await client.chat.completions.create({
  model: "deepseek/deepseek-v4.1-flash-abliterated",
  messages: [{ role: "user", content: "Write a haiku about the sea." }],
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
process.stdout.write("\n");
```

## Stream an answer in Python

Run `pip install openai`, save this as `chat.py`, then run `python chat.py`.

```chat.py
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://zalmir.ai/api/v1",
    api_key=os.environ["ZALMIR_API_KEY"],
)

stream = client.chat.completions.create(
    model="deepseek/deepseek-v4.1-flash-abliterated",
    messages=[{"role": "user", "content": "Write a haiku about the sea."}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="", flush=True)
print()
```

## Next.js: keep your key on the server

Never put your API key in browser code. Add this route to a Next.js app as `app/api/chat/route.js` and set `ZALMIR_API_KEY` in the server's environment. Your pages call `/api/chat`; only your server talks to Zalmir. Add your own sign-in or limits before opening it to the public, because anyone who can reach the route spends your credits.

```app/api/chat/route.js
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://zalmir.ai/api/v1",
  apiKey: process.env.ZALMIR_API_KEY,
});

export async function POST(request) {
  const { message } = await request.json().catch(() => ({}));
  if (typeof message !== "string" || message.length === 0 || message.length > 4000) {
    return Response.json({ error: "Send a message of 1 to 4,000 characters." }, { status: 400 });
  }
  const completion = await client.chat.completions.create({
    model: "deepseek/deepseek-v4.1-flash-abliterated",
    messages: [{ role: "user", content: message }],
  });
  return Response.json({ reply: completion.choices[0].message.content });
}
```

From a page or component:

```page code
const response = await fetch("/api/chat", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ message: "Hello" }),
});
const { reply, error } = await response.json();
```

More request options are in [Requests and outputs](https://zalmir.ai/docs/guides/requests), and every error code is in [Errors and headers](https://zalmir.ai/docs/guides/errors).

Source page: https://zalmir.ai/docs/guides/examples
