feat: add CONTEXT7_API_KEY environment variable support #743
+176
−6
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.
Summary
Adds support for
CONTEXT7_API_KEY
environment variable as a fallback when the--api-key
CLI flag is not provided.Motivation
Currently, the API key can only be passed via the
--api-key
CLI argument, which makes it difficult to:.env
files (best practice for secret management)Changes
Core Feature (Commit 1):
src/index.ts
:cliOptions.apiKey || process.env.CONTEXT7_API_KEY
--api-key
option description to document env var supportOptional Testing (Commits 2-3):
Behavior
--api-key
flag is provided → uses CLI value (existing behavior)--api-key
flag is NOT provided → falls back toCONTEXT7_API_KEY
env varUsage Example: Secure Key Management with dotenv-cli
For enhanced security, you can use
dotenv-cli
to load API keys from a local.env
file that's excluded from version control:.env.mcp.local
(add to.gitignore
):MCP Configuration (e.g.,
.mcp.json
for Claude Code):Testing
Manually tested with:
--api-key
flag only (existing behavior works)CONTEXT7_API_KEY
env var only (new fallback works)Optional: Integration Tests (commits 2-3)
Note: The last two commits add integration tests and CI workflow updates. These are optional and can be reverted before merge if the project maintainers prefer not to include them at this time.
Commit 2 (
1324ee1
):test: add integration tests for CONTEXT7_API_KEY env var
bun test
in package.json)src/__tests__/api-key-env-var.test.ts
with integration teststsconfig.json
to exclude test files from buildCommit 3 (
e2c6cbb
):ci: add test step to GitHub Actions workflow
bun test
step to.github/workflows/check.yaml
To use only the core feature (commit 1), you can:
# Cherry-pick only the feature commit git cherry-pick aa8f407
All tests pass locally (8/8) and validate the expected behavior.
Breaking Changes
None - this is purely additive. All existing configurations continue to work exactly as before.
🤖 Generated with Claude Code