Skip to main content

Command Palette

Search for a command to run...

Understanding the .env file: A Beginner's Guide to Configuration Management.

Environmental variable's role in data encryption and security.

Published
โ€ข7 min readโ€ขView as Markdown
Understanding the .env file: A Beginner's Guide to Configuration Management.
J
Software engineer with years of experience in software development

In the ever-growing world of software development, software deployment, and open source, the concepts of configuration management and data encryption remain an important part of the smooth execution of applications in various environments. One of the tools used to achieve this, and the tool that we will be talking about today, is the '.env' file.

Configuration management is crucial, whether you are working on a website, a mobile app, or any other software project. In this article, we are going to talk about the '.env' file, how to utilise it, and how it helps secure data from potential vulnerabilities.

Prerequisites

Before you begin this tutorial, you will need the following:

  • Basic programming knowledge

  • Development environment

  • Willingness to learn ๐Ÿ˜Ž

"The great aim of education is not knowledge, but action" - Herbert Spencer

What is .env?

The '.env' file is a plain text configuration file which stands for "environmental variable." It is widely used by developers to control environment-specific parameters for their programs. It is an easy and efficient solution to store private data such as API keys, database credentials, and other configuration variables without hardcoding it into your codebase.

Between development, testing, and production environments, these settings frequently vary, and the '.env' file maintains flexibility while protecting sensitive data. thereby reducing the rate of data breaches and unauthorised access to sensitive information such as API keys, database passwords, etc. A '.env' file is often located in the project's root directory and is excluded from version control repositories. It also performs the task of taking the values specified in it to the application environment.

Why use the .env file?

There are several advantages of using the '.env' file, including scalability and security. Here are just some of the many advantages of using the '.env' file:

  • Data security and encryption: hardcoding important data into your codebase can increase the security risk of your application.

    Imagine you have a secret code, like the password to your favourite game. If you write that password directly into the game and share it with others, anyone who sees your code can easily find and use your password. This makes your game less secure because anyone with access to the code knows your secret. Information like passwords, API keys, or other sensitive data is like your secret game password. If you put this sensitive information directly into your code, it becomes visible to anyone who can access the code. This is a security risk because if a malicious person gets hold of your code, they can also get access to your sensitive data.

  • Version control compatibility: If your codebase is open source or shared, you do not need to worry about your information being exposed because, by using a version control system with the '.env' file, you can easily prevent the risk of your information getting exposed, ensuring your secrets remain secure. The '.env' file is also compactable with systems like git.

There are so many reasons to use the '.env' file in your project, some of which are enhancing security, efficient deployment, easy rotation and update, scalable code maintenance, etc.

How to create the env file in your project

NB: If you already have a '.env' file in your project, consider skipping this step.

Creating a '.env' file is quite simple if you are using a code editor such as Visual Studio Code. Follow these steps to get it done.

  1. Open the terminal: You can do this by pressing Ctrl + ` on your keyboard or through the VS Code navigation menu.

  2. Run this command to create a '.env' file for your project:

     touch .env
    

    If you encounter any error while executing the command above, there is also a manual approach:

  1. Create a blank file in your project's root directory.

  2. Set the name of the blank file to ".env"

Congratulations! ๐Ÿพ You have successfully created the '.env' file for your project in either case.

layout of the env file (How does it even look like? ๐Ÿคท๐Ÿฝ)

A typical '.env' file layout consists of "KEY = VALUE" pairs, where the key is the configuration variable that holds the secret value. e.g.

THE_NAME_OF_WHAT_IS_HIDDEN = SECRET_KEY_THAT_IS_HIDDEN
API_KEY = input_api_key_here
DATABASE_URL = mongodb+srv://username:<password>@database

The code block above explains the anatomy of the '.env' file. To use the value assigned in the '.env' file, call process.env.KEY. e.g.

// Assuming your .env file contains a key named API_KEY with a value of "input_api_key_here"
const API_KEY_FROM_ENV = process.env.API_KEY;

// Now, API_KEY_FROM_ENV contains the value of API_KEY from the env file.
console.log(API_KEY_FROM_ENV);
// output: 'input_api_key_here'

The code block above simply describes the process of the '.env' file. Using the '.env' file safeguards the environmental values you want to conceal, and you should always keep in mind to include the '.env' file in the .gitignore file.

Using .env in various programming languages

The '.env' file can also be used in other programming languages e.g. Ruby, Python, C#(ASP.NET) etc. Below are some of the ways to read the '.env' file in various languages:

Python:

To read the '.env' file in python projects, you can use "pip" to install the "python-dotenv" library. Input the following command in the terminal to install using pip:

pip install python-dotenv

The code above installs the python-dotenv library. Follow this example to use the package in your development environment:

import os
# import the dot_env library
from dotenv import load_dotenv

# load the values from the env file
load_dotenv()
# Access values in your page
sql_password = os.getenv("SQL_PASSWORD")

Ruby

To get started with environmental variables in your ruby project, Add the "dotenv-rails" gem to your gem file and run bundle install :

# To add "dotenv-rails" to your project
gem 'dotenv-rails', require: 'dotenv/rails-now'

The code block above adds the dotenv-rails gem to your project, Follow the code block below to access the values from the '.env' file:

#To load values from the env file
require 'dotenv/load'
# To access the values from the env file
sql_password = ENV['SQL_PASSWORD']

C# (ASP.NET)

To read the '.env' file in c# (asp.net), you would need to install the 'dotenv' NuGet package, you can search for it in the NuGet package manager or open the NuGet package manager console and run:

 Install-Package dotenv.net

After installing the dotenv package, you can load and use the '.env' file values in your code by importing the dotenv NuGet package. In the example below, the API_KEY from the '.env' file is assigned to "api_key".

// Load the values from the .env file using the dotenv NuGet package
DotEnv.Load();
// Assign the value from the .env file "API_KEY" to a local variable "api_key"
var apiKey = Environment.GetEnvironmentVariable("API_KEY");

Also, do not forget to add the using dotenv.net at the top of your page to import the dotenv NuGet package.

Best practices while using the .env file

The following are best practices done to ensure smooth implementation of the '.env' file:

  1. Avoid generic names: Avoid the use of generic names while naming the keys in the '.env' file, and use more specific names. e.g. instead of 'PASSWORD' use 'MONGODB_PASSWORD', instead of 'KEY' use 'API_KEY', etc. Overall, use names that explain the service of what it is intended for.

  2. Proper documentation: Ensure that you properly include comments in the '.env' file, comments about the purpose of each key and what it is used for. You can also write about each key and its purposes in your project Readme.

By following these best practices and guides for using the '.env' file, you tend to minimize issues while using the '.env' file in your project.

Conclusion

The knowledge of data encryption is important in creating scalable applications and the '.env' file provides that environment for securing and encrypting important variables in an application.

By following the guidelines in this article, you can improve your knowledge about data encryption and security thereby creating scalable, secure and maintainable applications.

If you have any questions or suggestions, kindly leave them in the comment section.

P

very helpful and nice

1