Skip to main content

API


Ollama API Usage & Integration

POST /v1/chat/completions

What: Main API endpoint to get chat completions.
Why: Allows programmatic interaction with Ollama models.
How: Send POST request with model, messages, and parameters.

Example:

POST /v1/chat/completions
{
"model": "llama2",
"messages": [{"role": "user", "content": "Hello Ollama!"}],
"temperature": 0.7,
"max_tokens": 150
}
Authorization Header

What: API key authentication.
Why: Secure access control.
How: Send Authorization: Bearer <API_KEY> header.

Example:

Authorization: Bearer your_api_key_here
Messages Format

What: Structure conversation history.
Why: Maintain context and roles.
How: Array of message objects with role and content.

Example:

[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a joke."}
]
Optional Parameters

What: Customize API responses.
Why: Control output style and length.
How: Include keys like temperature, max_tokens, stop.

Example:

{
"temperature": 0.6,
"max_tokens": 100,
"stop": ["\n"]
}
Example cURL Request

What: How to call API from terminal.
Why: Quick test and automation.
How: Use curl with headers and JSON body.

Example:

curl -X POST https://api.ollama.com/v1/chat/completions \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"model": "llama2",
"messages": [{"role": "user", "content": "Hello Ollama!"}],
"temperature": 0.7,
"max_tokens": 150
}'