LocalAI listens on all interfaces with authentication off by default. That is fine on your own laptop, and dangerous the moment the port is reachable from anyone else: anyone who can reach it can use your models. Set the API key before you expose it — access control first, not after.
Set the API key before the port is reachable
Set the environment variable LOCALAI_API_KEY to a long random string before starting LocalAI, and set LOCALAI_AUTH=true to turn authentication on. Clients then send the key as a Bearer token. Do this before the server is reachable from the network — a key set after exposure is a key set too late.
export LOCALAI_API_KEY=<long-random-string>
export LOCALAI_AUTH=true
local-ai
# clients send: Authorization: Bearer <long-random-string>
The key is only as good as its storage: keep it out of screenshots, shared configs and chat logs, and rotate it if it leaks. The same rule applies to any local server you expose — the default of “open on all interfaces” is a convenience for development, not a deployment choice.
Try it: four checks
| Check | What you should see | If you do not see it |
|---|---|---|
| Key set | LOCALAI_API_KEY is set before starting | Long random string |
| Auth on | LOCALAI_AUTH=true is set | Auth is off by default |
| No key fails | A call without the key is refused | That is the protection working |
| Key works | A call with the Bearer key succeeds | Check the header spelling |
Expected result. We checked the LocalAI quickstart and README, which say the server listens on all interfaces with auth off and that LOCALAI_API_KEY and LOCALAI_AUTH=true protect it before exposing. We did not set a key on this machine.
Limits
This guide covers the API key only. The key protects the API, not the machine. We did not test any key ourselves.
