Cloud inference runs on hardware outside your computer, so the service must identify the account paying for a request. An Ollama API key authenticates requests sent directly to the hosted API.
The local Ollama API does not require a token. Direct hosted access is separate from CLI sign-in and needs no local Ollama installation or model download.
You need Bash, curl 7.76 or later, an existing Ollama API key, and enough usage credits for a short model reply. Prompts leave your computer, and anyone holding the key can consume the account's credits.
Steps to authenticate Ollama API requests:
- Check that the hosted chat endpoint rejects a request without an API key.
$ curl --fail-with-body --silent --show-error https://ollama.com/api/chat \ -H 'Content-Type: application/json' \ -d '{ "model": "gpt-oss:20b", "messages": [{"role": "user", "content": "What is 17 plus 25? Answer with only the number."}], "stream": false, "think": "low", "options": {"num_predict": 64} }' \ --write-out '\nHTTP %{http_code}\n' {"error":"Unauthorized"} HTTP 401
HTTP 401 and Unauthorized indicate missing authentication; curl also reports the HTTP error and exits with code 22. A public model listing or a local server response does not prove that a key works.
- Create a private header file from a hidden API-key prompt in Bash.
The current directory must be private and outside source control, with no existing ollama-header.txt to overwrite. The prompt does not echo the pasted key; Enter completes the input. The file contains the secret in plain text, with owner-only read and write permissions. Shell tracing and curl verbose output can expose it. If the key leaks, revocation in API key settings prevents further use; deleting a file or clearing a shell variable does not revoke it.
$ ( umask 077 read -r -s -p 'Ollama API key: ' key printf '\n' printf 'Authorization: Bearer %s\n' "$key" > ollama-header.txt )
- Send the hosted chat request with the protected bearer header.
$ curl --fail-with-body --silent --show-error https://ollama.com/api/chat \ -H @ollama-header.txt \ -H 'Content-Type: application/json' \ -d '{ "model": "gpt-oss:20b", "messages": [{"role": "user", "content": "What is 17 plus 25? Answer with only the number."}], "stream": false, "think": "low", "options": {"num_predict": 64} }' \ --write-out '\nHTTP %{http_code}\n' {"model":"gpt-oss:20b","created_at":"2026-11-04T02:20:35.78329578Z","message":{"role":"assistant","content":"42","thinking":"Just compute 17+25=42."},"done":true,"done_reason":"stop","total_duration":377297745,"prompt_eval_count":81,"prompt_eval_cached_count":64,"eval_count":20} HTTP 200
The -H @ollama-header.txt option reads the Authorization: Bearer header from the file instead of placing the key in curl's process arguments. stream: false returns one JSON response, think: low reduces this model's reasoning effort, and num_predict bounds generated tokens. HTTP 200, message.content containing 42, and done: true with done_reason: stop show that the authenticated request completed. An HTTP 402 response indicates insufficient usage credits. The private header file remains available for later requests.