Managing Environment Variables Securely
0 140
๐ What Are Environment Variables?
Environment variables are key-value pairs used to configure apps without hardcoding sensitive or environment-specific information directly into your code. Examples include API keys, database URLs, tokens, and secrets. ๐
Proper use of environment variables helps separate code from configuration, which is crucial for security, scalability, and maintainability. ๐ก
๐งช Why Manage Them Securely?
- ๐ซ Prevent exposing sensitive keys in public repositories
- โ Easily switch between development, staging, and production
- ๐ Enable dynamic configuration without changing code
๐ ๏ธ Defining Env Variables in React
React (via Create React App) only allows environment variables that start with REACT_APP_
. You can define them in a file named .env
at the root of your project:
REACT_APP_API_URL=https://api.example.com
REACT_APP_GOOGLE_MAPS_KEY=your-secret-key
Then, access them in your app:
const apiUrl = process.env.REACT_APP_API_URL;
โ ๏ธ Never commit the .env
file. Add it to .gitignore
.
๐ Accessing Env Vars in Next.js
Next.js allows both public and server-only env variables:
NEXT_PUBLIC_*
โ Accessible in the browser- Without prefix โ Server-only (e.g., API keys, DB secrets)
Example .env.local
file:
NEXT_PUBLIC_API_URL=https://api.mysite.com
JWT_SECRET=super_secret_jwt_token
Add .env.local
to .gitignore
to keep secrets private.
๐ Setting Env Variables on Hosting Platforms
โถ๏ธ Vercel
- Go to your project โ Settings โ Environment Variables
- Add
NEXT_PUBLIC_
orREACT_APP_
variables - Re-deploy for changes to take effect
๐ข Netlify
- Go to Site โ Settings โ Environment
- Add build-time env vars
- Netlify injects them during the build process
๐ฅ Firebase Hosting
Firebase doesnโt support runtime env vars directly. Instead:
- Use
.env
during build time - Consider deploying functions for secure server-side logic
๐ก GitHub Actions Secrets
For CI/CD pipelines using GitHub Actions:
- Go to GitHub repo โ Settings โ Secrets and variables โ Actions
- Add secrets like
NETLIFY_AUTH_TOKEN
,VERCEL_TOKEN
env:
REACT_APP_API_URL: ${{ secrets.REACT_APP_API_URL }}
๐งน Best Practices
- โ
Never commit secrets to Git (use
.gitignore
) - โ
Use separate
.env
files per environment (.env.development
,.env.production
) - โ
Use
dotenv
in Node.js backend for loading env files - โ Encrypt secrets for internal docs or handoffs
- โ Rotate secrets periodically
๐ Debugging Env Variables
If a variable isnโt showing up:
- Make sure you restarted the dev server after updating
.env
- Ensure the variable has the correct prefix (
REACT_APP_
orNEXT_PUBLIC_
) - Check hosting platform's dashboard for typos
โ Final Thoughts
Managing environment variables securely is a must-have skill for any serious developer. It helps keep your secrets safe, your apps flexible, and your deployments clean. ๐ง ๐
Stick to the principles of separation of config and code, use secrets managers where needed, and avoid committing anything you wouldnโt want exposed.
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments