Environment Variables: Configuration Outside Code
Environment variables are settings passed to your app from the outside world, letting you change behavior without touching code. Use them for API keys or database URLs. The biggest footgun is committing secrets to version control instead of using variables.
Why it exists
Applications need to behave differently in different environments (development, staging, production) without code changes. Hardcoding configuration like database passwords or API endpoints for each environment would be unmanageable and insecure. Environment variables solve this by externalizing configuration from the code itself.
The mental model
An environment variable is a key-value pair that exists in the operating system's shell, outside of your application's source code. When your application process starts, it inherits a copy of these variables. It's like giving your program a set of instructions on a sticky note right before it runs, telling it which database to connect to or what API key to use. The program can then read these values at runtime.
How it works
The operating system maintains a list of variables for each user session. When you run a program, that new process gets a copy of the environment. In a shell, you might set one with a command like EXPORT API_KEY="12345". Your application code then uses a built-in library function (like os.getenv("API_KEY") in Python or process.env.API_KEY in Node.js) to read the value. The process can query any variable in its environment, like the TEMP variable to find a place for temporary files.
When to use it
Use environment variables for any configuration that changes between deployments or machines. This includes three main categories: first, secrets like API keys, database credentials, and encryption keys; second, environment-specific settings like a database host URL or a domain name; and third, toggling application behavior, such as setting a NODE_ENV to "production" to enable performance optimizations.
When not to use it
Avoid using environment variables for non-secret configuration that is static and shared across all environments, like a list of supported timezones. This type of configuration belongs in a regular config file committed to your repository. They are also not ideal for complex, nested configuration structures; for that, a dedicated config file (like YAML or JSON) is often clearer.
One canonical example
A web application needs to connect to a database. In development, it connects to a local database. In production, it connects to a different, managed database. Instead of hardcoding these connection strings, the code reads a single DATABASE_URL environment variable. The developer sets this variable on their local machine, and the deployment system sets a different value on the production servers. The code remains identical in both environments.
Interview question
Which scenario best illustrates the primary benefit of using environment variables for application configuration?
- a.A program needs to automatically encrypt all sensitive data like API keys before storing them on the operating system.
- b.An application requires a complex, nested configuration structure that needs to be easily parsed by various modules.
- c.A developer wants to store a list of all supported timezones in a central, easily accessible location for their application.
- d.An application needs to connect to different databases in development and production without altering its codebase.Correct
Why? this is the answer
The primary benefit of environment variables is to allow applications to adapt to different environments (like development vs. production) without requiring changes to the source code. Option D directly reflects this by showing how a database connection can change based on the environment variable. Option A is incorrect because environment variables do not automatically encrypt data; their security benefit comes from keeping sensitive information out of version control.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on configuration — each one lists the topics its interview covers.
See open roles