Add configurable API key authentication for secure web exposure #1
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.
Overview
This PR implements configurable API client key authentication to enable safe exposure of the copilot-api server to the open web. The feature is fully backward compatible—when no API keys are configured, the server operates without authentication as before.
Problem
The server currently has no authentication mechanism, making it unsafe to expose to the public internet. Users wanting to deploy this as a publicly accessible service or share it across multiple clients need a way to control access.
Solution
Added a simple yet effective API key authentication system that:
Authorizationheader in standard OpenAI format (Bearer <key>or just<key>)/v1/chat/completions,/v1/models,/v1/embeddings,/v1/messages) require authentication when keys are configured/,/usage,/token) remain accessible without authentication--api-keysis not provided, the server operates without authentication (no breaking changes)Usage
Start the server with API keys:
Make authenticated requests:
Configure Claude Code with authentication:
{ "env": { "ANTHROPIC_BASE_URL": "http://localhost:4141", "ANTHROPIC_AUTH_TOKEN": "mykey123", "ANTHROPIC_MODEL": "gpt-4.1", "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1" } }Implementation Details
Testing
Added comprehensive test coverage:
Files Changed
src/lib/auth-middleware.ts- New authentication middlewaresrc/lib/state.ts- AddedapiKeysstate fieldsrc/server.ts- Applied middleware to protected routessrc/start.ts- Added--api-keysCLI optionREADME.md- Comprehensive documentation with examplestests/auth-middleware.test.ts- Unit teststests/api-key-integration.test.ts- Integration testsSecurity Considerations
Example: Production Deployment
This implementation enables safe deployment to public infrastructure while maintaining the simplicity and ease-of-use that makes copilot-api great for local development.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
aur.archlinux.orgnode ./dist/main.js start --help(dns block)bun test tests/api-key-integration.test.ts(dns block)bun test(dns block)https://api.github.com/copilot_internal/userbun test tests/api-key-integration.test.ts(http block)bun test(http block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.