Environmental variables

Environment variables, Environment variable, Environment vars, Env vars, Configuration variables, System variables
Environment variables are configuration settings stored outside the code, such as API keys and database credentials. They make code more secure and flexible.

What are environmental variables?

Environment variables are configuration settings that you store outside your source code, such as database passwords, API keys and server addresses. They act as a separate file or system setting where you place sensitive data or environment-dependent values. This allows the same code to run on different environments (development, test, production) without having to modify code each time. For an SMB with an ecommerce store or custom application, this prevents passwords from ending up in your GitHub repository or a developer from accidentally overwriting production data during a test.

How environmental variables work in practice

You define environment variables in a separate configuration file (often called .env) or directly in your hosting environment. Your application reads these variables at startup. Suppose you have an ecommerce store that connects to Mollie for payments. Instead of putting your Mollie API key hard-coded into your PHP or JavaScript file, you put it into an environment variable such as MOLLIE_API_KEY. Your code retrieves that value with a function like getenv() or process.env. Switching from test key to live key? Then you only modify the value in your .env file, not your entire codebase. That saves errors and makes deployment to new servers much faster.

Why environmental variables arose and why they are essential today

In the past, developers often placed configuration directly in the code. That became a problem as teams got bigger and code was shared through platforms like GitHub. Passwords and keys became publicly available online, resulting in data breaches and security incidents. The Twelve-Factor App methodology made environment variables standard. Today, hosting platforms like Kinsta, Cloudways and Vercel require you to configure sensitive data via environment variables. It also makes sense for AVG compliance: you separate configuration from code, making audits easier and reducing your risk of inadvertent disclosure of personal data.

What environmental variables bring to web projects

For an SME with a WordPress site, ecommerce store or custom application, environment variables offer three concrete benefits. First, you increase security: API keys and passwords are not in your code repository. Second, you make deployment more flexible: the same codebase runs on local development environment, staging server and production, each with its own database credentials. Third, you simplify collaboration: a new developer clicks his own .env file and can get started right away without production access. With web development by Monkey Vision, we set up default environment variables for all custom projects, so your application remains scalable and secure as your team or infrastructure grows.

Applications of environmental variables

Environmental variables appear in almost every Web project, but how you deploy them varies from situation to situation. Here are four concrete applications you might encounter as an SME entrepreneur, plus a decision criterion for when environmental variables are or are not the right choice.

Securely manage database credentials and API keys

The most common application: you store database username, password and hostname in environment variables such as DB_HOST, DB_USER and DB_PASSWORD. The same goes for API keys from services like Mailchimp, Google Maps, Stripe or Mollie. This keeps sensitive data out of your Git repository and allows team members with different access levels to work without everyone seeing production passwords. An ecommerce store with 500 products and integrations with an ERP system often has ten to fifteen API keys. By placing them centrally in environment variables, you change an expired key in one file instead of searching through dozens of PHP or JavaScript files.

Environment-specific configuration for development, test and production

You want your application to run on your local laptop against a test database, on the staging server against a copy of live data, and in production against the real database. With environment variables, you set an APP_ENV for each environment (for example, local, staging, production). Your code reads that variable and adjusts behavior: debug notifications only in local, caching only in production, email notifications only to real customers in production. So a B2B service provider building a customer portal can safely test new features without customers seeing error messages or receiving test emails. This prevents you from accidentally overwriting production data during development.

Feature flags and A/B test configuration

Want to test a new checkout flow without immediately showing all visitors the new version? Place an environment variable such as FEATURE_NEW_CHECKOUT=true or false. Your code checks that variable and shows the old or new flow. This way you switch functions on or off without deploying code. This is useful for gradual rollouts: first test internally, then 10% of visitors, then everyone. An ecommerce store running A/B testing on product pages can use environment variables to quickly switch between variants without pushing code each time. Note: for complex feature management, dedicated tools like LaunchDarkly are more powerful, but for simple on/off switches, environment variables suffice.

Centralize external service endpoints and CDN URLs.

Your application retrieves images from a CDN, sends notifications via a webhook or synchronizes data with an external API. Those URLs sometimes change (migration to new CDN, new API version). By putting them in environment variables (CDN_URL, WEBHOOK_ENDPOINT), you modify one line instead of dozens of references through your code. A content-heavy site with thousands of images on a CDN can thus switch from cloudflare.com to its own domain in minutes without code modifications. This reduces downtime on infrastructure changes from hours to minutes.

When environmental variables are the right choice and when they are not

Use environment variables for data that varies by environment or must be kept secret: passwords, API keys, database credentials, service endpoints. Don't use them for business logic or settings that are part of your application configuration (such as product categories or rates). Those belong in a database or configuration file within your code. Also not useful: environment variables for data that marketers or editors need to modify. They don't have access to server environments. For those kinds of settings, a CMS or admin interface is better. A common mistake: creating too many variables for things that are actually constants, such as the name of your company or default language. That makes configuration unnecessarily complex.

Want to apply this to your business? Monkey Vision helps SME entrepreneurs with web design, SEO and smart digital solutions. Schedule a no-obligation meeting and find out what's possible for you.

Schedule an introduction

Frequently Asked Questions

No, although both store configuration, they work differently. Environment variables are at the system level or in a separate .env file that is not in version control. Configuration files such as config.php or settings.json do reside in your code repository and often contain non-sensitive settings such as language, time zone or default page size. The difference: environment variables are environment-dependent and secret, configuration files are code-specific and public within your team. In practice, you combine both: configuration files for application logic, environment variables for credentials and environment-specific values. That's how you keep your repository clean and secure.

The biggest mistake: accidentally committing your .env file to Git. Therefore, always add .env to your .gitignore file. Second mistake: not keeping an example file. Create an .env.example with dummy values so new team members know what variables to set. Third mistake: using environment variables for data that actually belongs in a database, such as product prices or user roles. That data should be manageable through your application, not server configuration. Fourth mistake: not setting fallback values. If a variable is missing, your application crashes. Always check if a variable exists and provide a clear error message or default value. These errors lead to deployment problems and security risks.

Start with an inventory: what sensitive data is hard-coded in your code now? Consider database passwords, API keys and service URLs. Create an .env file in the root of your project and move those values there. Use a library such as phpdotenv (PHP), dotenv (Node.js) or python-dotenv (Python) to load variables. Customize your code: replace hard-coded values with getenv('VARIABLE_NAAM') or process.env.VARIABLE_NAAM. Test thoroughly on your local environment. Add .env to .gitignore and commit an .env.example with dummy values. Finally, configure your hosting platform (Kinsta, Cloudways, Vercel) to set the same variables in production. At web development by Monkey Vision, we set this up by default, including documentation for your team.

About the author

Monkey Vision

Monkey Vision is a full-service digital agency in Remote, specializing in web design, SEO and AI automation for SMEs. The knowledge base is compiled by our team of online strategists and continuously updated based on current insights.

Publication date: 26-04-2026
Last update: 27-04-2026