The Environment Variables Pattern
Tags: javascript, tutorial, python, beginners
Published: 2020-05-17 18:39:12

#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](https://12factor.net/config), 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](https://gitforwindows.org/) shell:
```bash
echo $PATH
/c/Users/MICHAEL/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/MICHAEL/bin:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:
```

##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:
- [the Windows Environment Variables setup](https://helpdeskgeek.com/how-to/create-custom-environment-variables-in-windows/): you can use this to configure global variables
- [.bashrc file via Bash shell](https://dev.to/mojemoron/productivity-101-git-aliases-for-lazy-developers-4hip)
- the **export** command in Bash like environment or **set** command in windows command-line:
```bash
#in bash shell
export API_KEY=https://dev.to/mojemoron #key=value
echo $API_KEY #echo key
https://dev.to/mojemoron #value

#in windows cmd
set API_KEY=https://dev.to/mojemoron
echo %API_KEY%
```
- `.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](https://pypi.org/project/python-decouple/) in a Django project to load environment variables.

```bash
touch .env #create this file in the root directory

#add this to the .env file
DB_HOST=localhost
DB_NAME=root
DB_USER=root
DB_PWD=root
DB_PORT=5432

#in your settings.py file, load the environment variables like so
DATABASES = {
    'default': {
        'ENGINE': 'django_postgres_extensions.backends.postgresql',
        'HOST': config('DB_HOST'),
        'NAME': config('DB_NAME'),
        'USER': config('DB_USER'),
        'PASSWORD':config('DB_PWD'),
        'PORT': config('DB_PORT'),
    }
}
```
There are several libraries that can help you achieve this:
     - [Node js](https://www.npmjs.com/package/dotenv)
     - [Python](https://pypi.org/project/python-dotenv/)
     - [Ruby](https://github.com/bkeepers/dotenv)
     - [Java](https://github.com/cdimascio/java-dotenv)
- 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.

###Summary
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](https://www.freecodecamp.org/news/gitignore-what-is-it-and-how-to-add-to-repo/). 
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! ✌