NEVER commit API keys, secrets, or sensitive data to this repository!
The following files are automatically excluded from git:
- β
.env- All environment files - β
.env.*- Any environment variant - β
*secret*- Any file with "secret" in the name - β
*key*.json- Any JSON file with "key" in the name - β
node_modules/- Dependencies
Create a .env file in the root or examples directory:
# .env (THIS FILE IS IN .gitignore)
OPENAI_API_KEY=your-openai-api-key-here
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_API_KEY=AIza...Load in your code:
import 'dotenv/config'; // or require('dotenv').config();
const agent = defineAgent({
provider: openai('gpt-4o'), // API key loaded from process.env
});β NEVER DO THIS:
const agent = defineAgent({
provider: openai('gpt-4o', {
apiKey: 'your-openai-api-key-here' // NEVER!
}),
});β DO THIS INSTEAD:
const agent = defineAgent({
provider: openai('gpt-4o', {
apiKey: process.env.OPENAI_API_KEY
}),
});Run this checklist BEFORE every commit:
# 1. Check for accidentally committed secrets
git grep -i "sk-proj" # OpenAI keys
git grep -i "sk-ant" # Anthropic keys
git grep -i "AIza" # Google keys
# 2. Check .env files
git status | grep ".env" # Should show nothing
# 3. Review staged files
git diff --staged
# 4. Use git-secrets (optional but recommended)
git secrets --scanAct immediately:
- Revoke the key at the provider's dashboard
- Remove from git history:
git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH/TO/FILE" \ --prune-empty --tag-name-filter cat -- --all - Force push (if already pushed):
git push origin --force --all
- Generate new keys and update your
.envfile
Prevent committing secrets:
# Install
brew install git-secrets # macOS
# or
git clone https://github.com/awslabs/git-secrets
# Setup
git secrets --install
git secrets --register-aws # If using AWS
git secrets --add 'sk-[Pp]roj-[A-Za-z0-9]{32,}' # OpenAI pattern
git secrets --add 'sk-ant-[A-Za-z0-9]{32,}' # Anthropic
git secrets --add 'AIza[A-Za-z0-9_-]{35}' # GoogleCreate .git/hooks/pre-commit:
#!/bin/bash
# Check for common secret patterns
if git grep -qE '(sk-[Pp]roj-|sk-[Aa]nt-|AIza)' $(git diff --cached --name-only); then
echo "β ERROR: Potential API key found in staged files!"
echo "Please remove API keys before committing."
exit 1
fi
# Check for .env files (except .env.example)
if git diff --cached --name-only | grep -qE '^\.env($|\.(?!example))'; then
echo "β ERROR: .env file in staged changes!"
echo "Make sure .env is in .gitignore"
exit 1
fi
echo "β
Pre-commit checks passed"
exit 0Make it executable:
chmod +x .git/hooks/pre-commit# Local development - NEVER COMMIT THIS FILE
OPENAI_API_KEY=your-openai-api-key-here
ANTHROPIC_API_KEY=sk-ant-your-real-key-here
GOOGLE_API_KEY=AIza-your-real-key-here# Template - SAFE TO COMMIT
OPENAI_API_KEY=your_openai_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
GOOGLE_API_KEY=your_google_api_key_hereConfigure API key restrictions at provider dashboards:
- OpenAI: Set rate limits, allowed models
- Anthropic: Set usage limits
- Google: Restrict by IP, HTTP referrer
- Change API keys every 90 days
- Revoke old keys immediately after rotation
- Set up billing alerts
- Monitor for unusual activity
- Review API usage regularly
- Use separate keys for dev and prod
- Set lower limits on dev keys
- Never use production keys in examples
- Use a secrets manager (1Password, LastPass, AWS Secrets Manager)
- Never share keys via Slack, email, or chat
- Use environment-specific keys
For GitHub Actions or other CI/CD:
# .github/workflows/test.yml
name: Test
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
# Store secrets in GitHub Secrets
- name: Run tests
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: npm testAdd secrets in GitHub: Repository Settings β Secrets and variables β Actions β New repository secret
Run this command to audit your repo:
# Check for any potential secrets
git log --all --full-history --source --remotes --all -- '*.env'
git log -p | grep -i "api.key\|apikey\|api_key"
# Check current files
find . -name "*.env" -not -path "./node_modules/*"
grep -r "sk-proj" . --exclude-dir=node_modules
grep -r "sk-ant" . --exclude-dir=node_modulesIf a key is exposed:
- Revoke immediately at provider dashboard
- Remove from git history (see above)
- Generate new key
- Check billing for unauthorized usage
- Update .env with new key
- Document the incident
Remember: Security is everyone's responsibility! π
If you're unsure about something, DON'T commit it. Ask first!