.env file
Learn the fundamentals of .env file, including syntax, security best practices, and how to securely manage environment variables in your projects.
π What is a .env file?
.env file?A .env file is a simple text file used to store environment variables. It is the industry standard for keeping sensitive information, such as API tokens, database passwords, and secret keys, separate from your application's source code.
By using a .env file, you ensure that your secrets are not hardcoded, making your application more secure and easier to configure across different environments (development, staging, production).
π οΈ How it Works
The file consists of Key-Value pairs, where each line represents a variable.
Syntax Rules:
Format:
KEY=VALUENo Spaces: Do not put spaces around the
=sign.Quotes: Values with spaces should be wrapped in double quotes (
").Comments: Use
#to add comments.
# This is a comment
DISCORD_TOKEN=your_token_here
DATABASE_URL="mongodb+srv://user:[email protected]/myFirstDatabase"
PORT=8080π Security Best Practices
βοΈ Usage on Discloud
On Discloud, the .env file is the primary way to manage your application's secrets.
Placement: Your environment files must be located in the root of your project, alongside your
discloud.config.Loading: While
.envis the default name for most libraries, Discloud allows you to use custom filenames (e.g.,.env.production) as long as your application code is configured to load them.
π Language Examples
Here is how you can access environment variables in different programming languages:
In Node.js, you typically use the dotenv package. By default, it looks for .env, but you can specify a path.
In Python, you can use python-dotenv.
In Java, you can use System.getenv().
Last updated