feat: add optional API key authentication to REST API server#4442
Merged
kartik-mem0 merged 4 commits intomainfrom Mar 20, 2026
Merged
feat: add optional API key authentication to REST API server#4442kartik-mem0 merged 4 commits intomainfrom
kartik-mem0 merged 4 commits intomainfrom
Conversation
Add token-based authentication to the FastAPI server, securing all endpoints when the ADMIN_API_KEY environment variable is set. Uses secrets.compare_digest for timing-safe key comparison. Fixes #3311 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
kartik-mem0
requested changes
Mar 20, 2026
Contributor
kartik-mem0
left a comment
There was a problem hiding this comment.
Looks good overall. Clean implementation that directly fixes #3311.
Few suggestions:
- The docs say auth is required for "every endpoint except the / redirect" — but /docs and /openapi.json are also excluded. Update the docs to mention all three.
- Consider adding a warning at startup if ADMIN_API_KEY is set but very short (e.g. under 16 chars). Easy for someone to accidentally set ADMIN_API_KEY=test in production.
- Minor type nit: _api_key: str = Depends(verify_api_key) should be Optional[str] since it returns None when auth is disabled.
Tests are thorough, secrets.compare_digest is the right choice, backward compat is preserved. LGTM with the docs fix.
- Update docs to mention /docs and /openapi.json are also excluded from auth - Add startup warning when ADMIN_API_KEY is shorter than 16 characters - Fix type annotation: _api_key is Optional[str] since it returns None when auth is disabled Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
13 tasks
kartik-mem0
approved these changes
Mar 20, 2026
jamebobob
pushed a commit
to jamebobob/mem0-vigil-recall
that referenced
this pull request
Mar 29, 2026
…4442) Co-authored-by: utkarsh240799 <utkarsh240799@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add optional token-based authentication (API key) to the FastAPI REST API server. When the
ADMIN_API_KEYenvironment variable is set, all endpoints (except/redirect,/docs, and/openapi.json) require a validX-API-Keyheader. When unset, the server behaves as before with no auth.This addresses the security concern that deployed instances accept requests from anyone without authentication.
Fixes #3311
Type of change
How Has This Been Tested?
Test suite:
tests/test_server_auth.py— 73 testsTests run against the actual
server/main.pyapp via FastAPI'sTestClient(full ASGI round-trip).TestAuthDisabled (22 tests)
/redirects to/docsTestAuthEnabled (32 tests)
X-API-KeyWWW-Authenticate: ApiKeyheader present on 401 responses/always accessible without keyTestAuthenticatedCRUDFlow (2 tests)
TestAuthEdgeCases (6 tests)
ADMIN_API_KEYenv var completely absent → auth disabled/openapi.jsonand/docsaccessible without key when auth enabledTestStartupLogging (2 tests)
ADMIN_API_KEYis not setADMIN_API_KEYis set$ python -m pytest tests/test_server_auth.py -v # 73 passed in 1.74sChecklist:
Maintainer Checklist
🤖 Generated with Claude Code