New Tool Radar

Local AI

How to Require an API Key on llama-server

Start llama-server with --api-key before you expose it on a network, so only clients with the key can generate text.

Schematic diagram with three steps: start llama-server with --api-key, send a request with the Authorization header, and confirm a request without the key is refused.

By default llama-server has no authentication at all: anyone who can reach the port can generate text on your hardware. That is fine on your own laptop at 127.0.0.1, and a real problem the moment the port is reachable from a network. Set the key first, before you expose the server — the standing review rule for public entries is access control first.

Related guide: How to Serve a Local Model with an OpenAI-Compatible API Using llama-server

Start the server with a key

The server README documents --api-key KEY, with multiple keys allowed as a comma-separated list, and --api-key-file FNAME for a file with one key per line. Use the file form so the key does not sit in your shell history:

llama-server -m model.gguf --api-key-file ./llama_keys.txt --host 127.0.0.1 --port 8080

Clients then send the key as a bearer token:

curl http://localhost:8080/v1/chat/completions -H "Authorization: Bearer YOUR_KEY" -H "Content-Type: application/json" -d '{"model": "model", "messages": [{"role": "user", "content": "Why is the sky blue?"}]}'

The API reference says that when a key is configured, all non-public endpoints require that header — while /health and /models stay public.

Verify the lock works

Send the same request without the header and expect a 401. A test of llama-server in Docker did exactly this: it checked the 401 without the key and got a real chat answer with it. If a request without a key still succeeds, you are talking to a different server process — one started without the flag — so check which process owns the port.

Expected result. We checked the llama.cpp server README for the –api-key and –api-key-file flags, the HTTP API reference for which endpoints require the key, and the linked test for the 401 check. We did not run this software ourselves for this guide, so no output here is a recorded result; steps marked UNTESTED were checked against the sources below, not run.

Limits

An API key is a shared secret, not encryption: on an untrusted network, use it together with HTTPS (the server supports –ssl-key-file and –ssl-cert-file) or a private network, not instead of them. Rotate the key if it ever appears in a log or a shared file. We did not run llama-server, so no 401 here is a recorded result.

Sources

Checked 2026-11-28.

See all