Contents
Ollama JavaScript / Node.js Tutorial: Chat, Streaming, and REST API Calls
Install
npm install ollama
Basic chat
import ollama from "ollama";
const response = await ollama.chat({
model: "qwen3:8b",
messages: [
{ role: "system", content: "Please answer in Traditional Chinese." },
{ role: "user", content: "What is the difference between JavaScript and TypeScript?" },
],
});
console.log(response.message.content);
(Using ES modules: use the .mjs file extension or set "type": "module" in package.json.)
Stream output
import ollama from "ollama";
const stream = await ollama.chat({
model: "qwen3:8b",
messages: [{ role: "user", content: "Write a four-line poem" }],
stream: true,
});
for await (const part of stream) {
process.stdout.write(part.message.content);
}
Stop generation
When the user clicks “Stop”:
const stream = await ollama.chat({ model: "qwen3:8b", messages, stream: true });
setTimeout(() => stream.abort(), 3000); // Abort after 3 seconds
try {
for await (const part of stream) process.stdout.write(part.message.content);
} catch (err) {
if (err.name === "AbortError") console.log("\nStopped");
}
Connect to another host
import { Ollama } from "ollama";
const client = new Ollama({ host: "http://192.168.1.50:11434" });
const res = await client.chat({ model: "qwen3:8b", messages: [{ role: "user", content: "Hello" }] });
Use in a browser
import ollama from "ollama/browser";
To call local Ollama directly from a web page, set OLLAMA_ORIGINS to allow the page's origin. See Environment variables and configuration. In production, requests are usually forwarded by your own backend so the browser does not connect directly to Ollama.
Without installing a package: use fetch directly
const res = await fetch("http://localhost:11434/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "qwen3:8b",
messages: [{ role: "user", content: "Hello" }],
stream: false,
}),
});
const data = await res.json();
console.log(data.message.content);
Handle streaming with fetch
A streaming response contains one JSON object per line (NDJSON):
const res = await fetch("http://localhost:11434/api/chat", {
method: "POST",
body: JSON.stringify({ model: "qwen3:8b", messages: [{ role: "user", content: "Count from 1 to 10" }] }),
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop(); // The last line may be incomplete; keep it for the next read
for (const line of lines) {
if (!line.trim()) continue;
const chunk = JSON.parse(line);
process.stdout.write(chunk.message?.content ?? "");
}
}
Use the OpenAI SDK
npm install openai
import OpenAI from "openai";
const openai = new OpenAI({ baseURL: "http://localhost:11434/v1", apiKey: "ollama" });
const completion = await openai.chat.completions.create({
model: "qwen3:8b",
messages: [{ role: "user", content: "Hello" }],
});
console.log(completion.choices[0].message.content);
A simple Express API
Wrap Ollama in your own API for the frontend to call:
import express from "express";
import ollama from "ollama";
const app = express();
app.use(express.json());
app.post("/api/ask", async (req, res) => {
const { question } = req.body;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
const stream = await ollama.chat({
model: "qwen3:8b",
messages: [
{ role: "system", content: "Please answer concisely in Traditional Chinese." },
{ role: "user", content: question },
],
stream: true,
});
for await (const part of stream) res.write(part.message.content);
res.end();
});
app.listen(3000, () => console.log("http://localhost:3000"));
curl -N -X POST http://localhost:3000/api/ask \
-H "Content-Type: application/json" \
-d '{"question":"What is an API?"}' Updated October 12, 2026