Skip to content

feat(api): optional API keys for the API routes - #568

Merged
remsky merged 6 commits into
remsky:masterfrom
davidcoulson:feat/api-keys
Oct 31, 2026
Merged

remsky merged 6 commits into
remsky:masterfrom
davidcoulson:feat/api-keys

Conversation

@davidcoulson

Copy link
Copy Markdown
Contributor

The server has no authentication, and with CORS_ORIGINS defaulting to
"*" any client that can reach the port can run generation. Add
API_KEYS, a list of accepted keys. When it is non-empty, the /v1, /dev,
SSML, tune and debug routers require "Authorization: Bearer ",
which is how OpenAI-compatible clients send their API key; keys are
compared with secrets.compare_digest. /health, the docs and the web
player's static files stay open. Empty by default, so nothing changes
for existing deployments.

Testing

  • pytest: new tests, each confirmed to fail without the fix; full suite passes.
  • Unit tests cover the 401 and the open routes. Run on the 3070 with API_KEYS unset, where behaviour is unchanged.
  • These were run on an RTX 3070 (Unraid, nvidia runtime) using an image built from docker/gpu/Dockerfile.optimized with this and six sibling PRs merged.

Closes #373

One of seven PRs from the same review. Each is based on master on its own; where they overlap (CHANGELOG [Unreleased], and kokoro_v1.py across the OOM, voice-mix and event-loop PRs), the conflicts are mechanical.

The server has no authentication, and with CORS_ORIGINS defaulting to
"*" any client that can reach the port can run generation. Add
API_KEYS, a list of accepted keys. When it is non-empty, the /v1, /dev,
SSML, tune and debug routers require "Authorization: Bearer <key>",
which is how OpenAI-compatible clients send their API key; keys are
compared with secrets.compare_digest. /health, the docs and the web
player's static files stay open. Empty by default, so nothing changes
for existing deployments.

Closes remsky#373
@remsky

remsky commented Oct 31, 2026

Copy link
Copy Markdown
Owner

Thanks David, this is a good shape. Had websocket changes incoming (#576), so I'm resolving for the broader shape with a follow-up on your PR:

  • HTTPConnection dependency in place of HTTPBearer, so the same check covers the websocket routes
  • Accepts each client's header: Authorization: Bearer (OpenAI), xi-api-key (ElevenLabs), X-API-Key (Cartesia).

Give me a shout if you have any feedback

remsky and others added 2 commits October 30, 2026 20:35
test(api): check /v1/test stays open with API_KEYS set
API_KEYS containing "" matched any request without credentials. Skip empty
tokens and keys so such a config fails closed.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
davidcoulson added a commit to davidcoulson/Kokoro-FastAPI that referenced this pull request Oct 31, 2026
…-deploy

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

# Conflicts:
#	api/src/core/config.py
#	api/src/main.py
#	api/tests/test_kokoro_v1.py
#	api/tests/test_temp_manager.py
@remsky

remsky commented Oct 31, 2026

Copy link
Copy Markdown
Owner

Big catch, thanks @davidcoulson

@remsky
remsky merged commit 7719de0 into remsky:master Oct 31, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Authentication with api_key

2 participants