Add support for ANTHROPIC_API_KEY environment variable authentication detection#346
Add support for ANTHROPIC_API_KEY environment variable authentication detection#346menny wants to merge 3 commits intositeboon:mainfrom
Conversation
… detection
This commit enhances Claude authentication detection to support both the
ANTHROPIC_API_KEY environment variable and the OAuth credentials file,
matching the authentication priority order used by the Claude Agent SDK.
## Changes
- Updated checkClaudeCredentials() function in server/routes/cli-auth.js
to check ANTHROPIC_API_KEY environment variable first, then fall back
to ~/.claude/.credentials.json OAuth tokens
- Modified /api/cli-auth/claude/status endpoint to return authentication
method indicator ('api_key' or 'credentials_file')
- Added comprehensive JSDoc documentation with priority order explanation
and official Claude documentation citations
## Authentication Priority Order
1. ANTHROPIC_API_KEY environment variable (highest priority)
2. ~/.claude/.credentials.json OAuth tokens (fallback)
This priority order matches the Claude Agent SDK's authentication behavior,
ensuring consistency between how we detect authentication and how the SDK
actually authenticates.
## API Response Changes
The /api/cli-auth/claude/status endpoint now returns:
- method: 'api_key' when using ANTHROPIC_API_KEY environment variable
- method: 'credentials_file' when using OAuth credentials file
- method: null when not authenticated
This is backward compatible as existing code checking the 'authenticated'
field will continue to work.
## References
- https://support.claude.com/en/articles/12304248-managing-api-key-environment-variables-in-claude-code
Claude Agent SDK prioritizes environment variables over subscriptions
- https://platform.claude.com/docs/en/agent-sdk/overview
Official Claude Agent SDK authentication documentation
## Important Note
When ANTHROPIC_API_KEY is set, API calls are charged via pay-as-you-go
rates instead of subscription rates, even if the user is logged in with
a claude.ai subscription.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
WalkthroughThe Claude authentication flow is extended to check the ANTHROPIC_API_KEY environment variable as the primary credential source, falling back to OAuth tokens from ~/.claude/.credentials.json. All Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |

Summary
This PR enhances Claude authentication detection to support both the
ANTHROPIC_API_KEYenvironment variable and the OAuth credentials file, matching the authentication priority order used by the Claude Agent SDK.Changes
Modified Files
server/routes/cli-auth.jsKey Updates
Updated
checkClaudeCredentials()function to checkANTHROPIC_API_KEYenvironment variable first, then fall back to~/.claude/.credentials.jsonOAuth tokensModified
/api/cli-auth/claude/statusendpoint to return authentication method indicator:method: 'api_key'when usingANTHROPIC_API_KEYenvironment variablemethod: 'credentials_file'when using OAuth credentials filemethod: nullwhen not authenticatedAdded comprehensive documentation with JSDoc comments explaining:
Authentication Priority Order
ANTHROPIC_API_KEYenvironment variable (highest priority)~/.claude/.credentials.jsonOAuth tokens (fallback)This priority order matches the Claude Agent SDK's authentication behavior, ensuring consistency between how we detect authentication and how the SDK actually authenticates.
API Response Format
The
/api/cli-auth/claude/statusendpoint now returns:{ "authenticated": true, "email": "user@example.com", "method": "credentials_file" }Or when using API key:
{ "authenticated": true, "email": "API Key Auth", "method": "api_key" }Backward Compatibility
✅ Fully backward compatible - existing code checking the
authenticatedfield will continue to work as expected.Important Note
ANTHROPIC_API_KEYis set, API calls are charged via pay-as-you-go rates instead of subscription rates, even if the user is logged in with a claude.ai subscription.References
Managing API key environment variables in Claude Code
Agent SDK Overview
Testing
The implementation has been verified to:
ANTHROPIC_API_KEYenvironment variable first🤖 Generated with Claude Code
Summary by CodeRabbit
ANTHROPIC_API_KEYenvironment variable for authentication, prioritized over stored OAuth tokens✏️ Tip: You can customize this high-level summary in your review settings.