Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/ollama/llms.txt

Use this file to discover all available pages before exploring further.

A local Ollama server does not require authentication — you can start generating text immediately without any API key. Authentication is only needed when connecting to a remote Ollama server or one that has been explicitly configured to require a bearer token.

Environment variable

The recommended approach for production deployments is to set the key as an environment variable. It is loaded automatically via Env::loadOptionalSetting() and requires no changes to your application code.
OLLAMA_API_KEY=your-bearer-token

Programmatic configuration

Pass the key directly when calling Ollama::create():
use AiSdk\Ollama;

Ollama::create([
    'apiKey' => 'your-bearer-token',
]);

How it works

When apiKey is set, OllamaOptions::authHeaders() prepends an Authorization: Bearer <token> header to the headers array before every outbound request. The resulting headers are passed to all request types — text generation, embeddings, image generation, model listing (/api/tags), and model inspection (/api/show).
// OllamaOptions::authHeaders() produces:
[
    'Authorization' => 'Bearer your-bearer-token',
    // ...any additional headers from the 'headers' option
]
This merged headers array is forwarded unchanged to the underlying HTTP runner on every call.

Custom headers

For servers that require non-standard authentication schemes or additional request headers, use the headers option:
Ollama::create([
    'headers' => [
        'X-Api-Key' => 'your-key',
        'X-Tenant'  => 'my-org',
    ],
]);
When both apiKey and a custom Authorization entry in headers are provided, authHeaders() calls array_merge(['Authorization' => 'Bearer '.$apiKey], $headers). Because PHP array_merge lets later string keys overwrite earlier ones, a conflicting Authorization key in headers will override the apiKey value. To rely on apiKey for authentication, omit Authorization from the headers array.
For local development, omit apiKey entirely. The default base URL http://localhost:11434/v1 does not require authentication, and the provider initialises without credentials when neither apiKey nor OLLAMA_API_KEY is present.

Build docs developers (and LLMs) love