Getting Ollama Into Your JavaScript Project Without the Friction
You've probably got a local model running through Ollama and now you want to actually use it from your JavaScript app. The REST API is there, but wiring up fetch calls, handling streaming chunks, and parsing responses gets tedious fast. The Ollama JavaScript library exists to take that tedium off your plate—it's a thin, official client that wraps the Ollama API in something you can call in a few lines.
What It Does
The Ollama JavaScript library is the easiest way to integrate your JavaScript project with Ollama. At its core, it's a client designed around the Ollama REST API, exposing methods like chat that take a request object and return a response. You pass in a model name and an array of messages (each with a role and content), and you get back the assistant's reply.
It works in Node and in the browser—for the browser you just import from ollama/browser instead of ollama. The chat method accepts optional parameters for things like structured JSON output (format: 'json'), model thinking levels (think: true or "high", "medium", "low"), log probabilities, and keep_alive to control how long a model stays loaded. Messages can also carry images as Uint8Array or base64 strings, and a tool_name field lets you report back which tool was executed so the model knows the result.
Why It's Cool
The streaming support is where this library earns its keep. Set stream: true and instead of waiting for a full response, you get back an AsyncGenerator—each iteration yields a part of the stream, so you can write chunks to stdout or push them to a client as they arrive. That's a small design decision with a big practical payoff: no manual SSE parsing, no chunk-boundary headaches.
-
Cloud models without leaving your workflow. This is the part that stands out. You can pull a larger model like
gpt-oss:120b-cloudand use it exactly as you would a local one—the library offloads automatically. Sign in once withollama signin, pull the model, and your existing code keeps working. You don't have to rewrite anything to reach bigger models. -
Direct cloud API access when you want it. If you'd rather skip the local daemon, point the client at
https://ollama.comwith an API key in theAuthorizationheader and talk to the cloud API directly. Samechatinterface, different host. -
Browser support is a one-line change. Importing
ollama/browserinstead ofollamais all it takes to move the same code into a frontend context. No separate SDK, no different mental model. -
The API mirrors the REST API deliberately. If you've read the Ollama REST docs, you already know how this library works. That's a good kind of boring—there's no abstraction layer hiding what's actually happening.
How to Try It
Getting started is about as short as it gets:
npm i ollama
Then make a call:
import ollama from 'ollama'
const response = await ollama.chat({
model: 'llama3.1',
messages: [{ role: 'user', content: 'Why is the sky blue?' }],
})
console.log(response.message.content)
Want streaming? Add stream: true and iterate:
const response = await ollama.chat({
model: 'llama3.1',
messages: [{ role: 'user', content: 'Why is the sky blue?' }],
stream: true,
})
for await (const part of response) {
process.stdout.write(part.message.content)
}
For cloud models, sign in and pull one first:
ollama signin
ollama pull gpt-oss:120b-cloud
Then use it like any other model—the offloading happens automatically. If you want to hit the cloud API directly, create an API key, export it as OLLAMA_API_KEY, and instantiate the client with host: 'https://ollama.com' and the appropriate Authorization header.
The full source and API reference live at github.com/ollama/ollama-js.
Final Thoughts
This library isn't trying to be clever—it's trying to get out of your way, and it mostly succeeds. If you're already running Ollama locally and writing JavaScript, there's little reason to hand-roll your own client. The streaming generator and the cloud model support are the two features that make it genuinely useful rather than just convenient, especially if you want to prototype locally and scale up to bigger models later without changing your code. It's a solid, focused tool for a specific job.