How to implment access token or other form of authorization #99

Open
opened 2026-02-15 16:27:58 -05:00 by yindo · 3 comments
Owner

Originally created by @PhilipAmadasun on GitHub (Jun 11, 2024).

Is it possible to implement access tokens with this library? I'm basically looking for a way to implement authorization.

Originally created by @PhilipAmadasun on GitHub (Jun 11, 2024). Is it possible to implement access tokens with this library? I'm basically looking for a way to implement authorization.
Author
Owner

@ajrodrigues commented on GitHub (Sep 26, 2024):

I was trying to do that using headers to pass the API Key:
client = Client(host='https://localhost:11434',headers=["x-token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"])

But after that I get a different error:
File "C:\Python312\Lib\site-packages\ollama_client.py", line 67, in init
super().init(httpx.Client, host, **kwargs)
File "C:\Python312\Lib\site-packages\ollama_client.py", line 52, in init
headers['Content-Type'] = 'application/json'
~~~~~~~^^^^^^^^^^^^^^^^
TypeError: list indices must be integers or slices, not str

@ajrodrigues commented on GitHub (Sep 26, 2024): I was trying to do that using headers to pass the API Key: **_client = Client(host='https://localhost:11434',headers=["x-token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"])_** But after that I get a different error: **_File "C:\Python312\Lib\site-packages\ollama\_client.py", line 67, in __init__ super().__init__(httpx.Client, host, **kwargs) File "C:\Python312\Lib\site-packages\ollama\_client.py", line 52, in __init__ headers['Content-Type'] = 'application/json' ~~~~~~~^^^^^^^^^^^^^^^^ TypeError: list indices must be integers or slices, not str_**
Author
Owner

@westbrook-ai commented on GitHub (Feb 24, 2025):

I was wondering the same thing and was able to get it work using the example below:

import ollama
import os

url = "https://<Ollama URL>"
api_key = os.getenv("OLLAMA_API_KEY")
model_name = "llama3.2"
headers = {
    "Authorization": f"Bearer {api_key}"
}

client = ollama.Client(host=url, headers=headers)

response = client.generate(model=model_name, prompt="Why is the sky blue?")

print(response)

I hope this helps someone in the future!

@westbrook-ai commented on GitHub (Feb 24, 2025): I was wondering the same thing and was able to get it work using the example below: ``` import ollama import os url = "https://<Ollama URL>" api_key = os.getenv("OLLAMA_API_KEY") model_name = "llama3.2" headers = { "Authorization": f"Bearer {api_key}" } client = ollama.Client(host=url, headers=headers) response = client.generate(model=model_name, prompt="Why is the sky blue?") print(response) ``` I hope this helps someone in the future!
Author
Owner

@ramibch commented on GitHub (Nov 5, 2025):

Many thanks to @westbrook-ai for sharing that code snippet! 🙏

If someone wants to implement this Authorization feature with a Cloudflare Tunnel, you can point it to a local reverse proxy. For example, you can use something like this reverse-proxy

# .env file
LISTEN_PORT=8011        # Port for the reverse proxy application (Python script)
BACKEND_URL="http://localhost:11434"  # Your Ollama server
BEARER_TOKEN="my-secret-token"        # Authorization token
TIMEOUT=60             # Response timeout in seconds

Image

With this setup, you can secure your Ollama server over the internet using an authorization token.

@ramibch commented on GitHub (Nov 5, 2025): Many thanks to @westbrook-ai for sharing that code snippet! 🙏 If someone wants to implement this Authorization feature with a Cloudflare Tunnel, you can point it to a local reverse proxy. For example, you can use something like [this reverse-proxy](https://github.com/ramibch/reverse-proxy) ``` # .env file LISTEN_PORT=8011 # Port for the reverse proxy application (Python script) BACKEND_URL="http://localhost:11434" # Your Ollama server BEARER_TOKEN="my-secret-token" # Authorization token TIMEOUT=60 # Response timeout in seconds ``` <img width="2189" height="524" alt="Image" src="https://github.com/user-attachments/assets/3c88d271-849d-4a68-abc4-45eb0be796f2" /> With this setup, you can secure your Ollama server over the internet using an authorization token.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#99