DEV Community

Micheal Ojemoron
Micheal Ojemoron

Posted on • Updated on

The Environment Variables Pattern

An Introduction to Environment variables and how to use them

During software development, there are things we shouldn't share with our code. These are often configurations like secret keys, database credentials, AWS keys, API keys/tokens, or server-specific values.
According to 12 Factor methodology, it is wrong to store config as constants in the code because config varies substantially across deploys, code does not.

What Are Environment Variables?

When I started writing code it took quite some time to figure out what an environment variable is and how they can be set-up.
In a nutshell, an environment variable is a combination of values, called key/pair values. They hold information that other programs and applications can use.
One common environment variable described by the operating system is PATH which contains a set of directories where executable programs are located.
Using Git Bash shell:

echo $PATH
Enter fullscreen mode Exit fullscreen mode

How to Set-up Environment Variables

Aside from built-in environment variables defined by our operating system or third party programs, there are several ways to create environment variables:

#in bash shell
export API_KEY= #key=value
echo $API_KEY #echo key #value

#in windows cmd
set API_KEY=
echo %API_KEY%
Enter fullscreen mode Exit fullscreen mode
  • .env file: this is the most common way to create environment variables per project especially during development. To do this, you create a .env file in your project's root directory and set the various environment variables you need in your project and afterward, you use a library for your respective programming language to load the file which will dynamically define these variables. For example, using python-decouple in a Django project to load environment variables.
touch .env #create this file in the root directory

#add this to the .env file

#in your file, load the environment variables like so
    'default': {
        'ENGINE': 'django_postgres_extensions.backends.postgresql',
        'HOST': config('DB_HOST'),
        'NAME': config('DB_NAME'),
        'USER': config('DB_USER'),
        'PORT': config('DB_PORT'),
Enter fullscreen mode Exit fullscreen mode

There are several libraries that can help you achieve this:
- Node js
- Python
- Ruby
- Java

  • Cloud service providers like AWS, GCP, Heroku, Azure, and Digital Ocean recommend the use of environment variables for configuration and have built-in features for setting and managing them.


It's very important to note that separating application configurations such as credentials, API keys from code will remove unnecessary roadblocks/headaches when deploying your apps to multiple environments.

Finally, remember not to check in your .env file into a version control system by using git .ignore file.
You don't want to expose your environment variables to the world!

How do you configure environment variables for your projects?

Kindly follow me and turn on your notification.
Thank you! Happy coding! ✌

Top comments (4)

4r7d3c0 profile image

Hi thanks for the post, yes env variable is an absolute must!
There's some history behind them in the Early days of Unix video by the person who made sh and env vars too :D

Also please check out my package envariable that will read the .env file into your process.env object to be able to develop locally! I use it for every web server during local development. When deployed, the file won't be there and they'll be read from the actual environment (like heroku, etc)

mojemoron profile image
Micheal Ojemoron

Great, I will check it out

thisdotmedia_staff profile image
This Dot Media

Really thorough article. Thanks Michael πŸ˜„

mojemoron profile image
Micheal Ojemoron

You are welcome πŸ™Œ