Skip to main content
In our previous guide, we covered how to use the Ollama CLI. Ollama also exposes a REST API when you run ollama surf. This tutorial walks through each endpoint—showing request examples, sample responses, and best practices for integrating Ollama’s local LLMs into your applications.

1. Starting the Ollama REST Server

Launch the API server on your machine:
You should see logs like:
All endpoints are now accessible at http://localhost:11434.

2. Generating a Single Completion

Send a one-shot prompt to /api/generate:
Pipe the response through jq for pretty-printed JSON:
Example response:
Key fields:
  • response: Generated text.
  • done / done_reason: Completion status.
  • context: Token IDs consumed.
  • Timing metrics: Diagnose performance.

3. Streaming Tokens

To receive tokens as they’re generated, enable streaming:
The server emits incremental JSON chunks:
Use streaming for real-time UIs or chat interfaces.

4. Enforcing a JSON Schema

Require a structured output by defining a JSON schema in the format field:
Sample response:
Ideal for applications expecting strict data structures.

5. Multi-Turn Chat Conversations

Use /api/chat to maintain context across messages:
Example assistant reply:

6. Model Management Endpoints

You can list, copy, show, and delete models via REST: Copy a Model
Verify in the CLI:
Delete a Model
Pulling new models via the REST API is not supported. Use the CLI instead:

7. API Reference & Further Reading

For the full list of endpoints, request/response specifications, and example payloads, see the Ollama API Documentation on GitHub.
The image shows a GitHub repository page with a focus on an API documentation file, listing various endpoints such as "Generate a completion" and "Create a Model."
Links and References

Watch Video

Practice Lab