April 28, 2026 • Parth Narkar
Protect Your API Keys (Beginner's Guide)
A beginner's guide to protecting API keys using .gitignore, with templates for Node.js, React, Next.js, Python, and full-stack applications.
The Common Mistake
Many beginners accidentally push their .env file to GitHub.
Why is this dangerous?
Because .env files usually contain:
- API Keys
- Database credentials
- Secret tokens
- Private configuration
If exposed, anyone can misuse your services, leading to:
- Account abuse
- Unexpected charges
- Security breaches
The Simple Fix: Use .gitignore
Step 1: Create a .gitignore file
touch .gitignore
Step 2: Add .env inside it
.env
This tells Git:
“Never track this file.”
Step 3: You’re Done
Now you can safely push your code without exposing secrets.
What if You Already Pushed .env?
Just deleting it is NOT enough.
Remove it from Git tracking:
git rm --cached .env
git commit -m "Removed .env from tracking"
git push
IMPORTANT: Regenerate Your Keys
If your .env was exposed:
- Regenerate API keys
- Change passwords
- Revoke old tokens
Best Practices
1. Use .env.example
API_KEY=your_api_key_here
DB_URI=your_database_url_here
2. Never Hardcode Secrets
Bad:
const API_KEY = "123abcSECRET";
Good:
const API_KEY = process.env.API_KEY;
3. Always Check Before Push
git status
Ready-to-Use .gitignore Templates
Node.js / Express
# dependencies
node_modules/
# environment variables
.env
.env.local
.env.*.local
# logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# build files
dist/
build/
# OS files
.DS_Store
# IDE
.vscode/
.idea/
React
# dependencies
node_modules/
# environment variables
.env
.env.local
.env.*.local
# build output
build/
# misc
.DS_Store
*.log
# IDE
.vscode/
.idea/
Next.js
# dependencies
node_modules/
# next build
.next/
out/
# environment
.env
.env.local
.env.*.local
# logs
*.log
# misc
.DS_Store
# IDE
.vscode/
.idea/
Python
# virtual environment
venv/
env/
# environment variables
.env
# python cache
__pycache__/
*.pyc
# logs
*.log
# OS
.DS_Store
# IDE
.vscode/
.idea/
Full Stack (MERN)
# dependencies
node_modules/
# env files
.env
.env.local
.env.*.local
# logs
*.log
# build
client/build/
dist/
# misc
.DS_Store
# IDE
.vscode/
.idea/
Quick Checklist
- .env added to .gitignore
- No secrets in code
- .env.example created
- Keys rotated if exposed
Final Thought
This one habit can save you from:
- Security issues
- Financial loss
- Embarrassing leaks
Smart developers don’t just build… they protect what they build.