DEV Community

Cover image for How to Store API Keys Securely in a .env File
Technophile
Technophile

Posted on

How to Store API Keys Securely in a .env File

Hey everyone, welcome back! In this post, I’ll show you how to store secret keys securely in a .env file. You can also watch the YouTube video if you want to see how I did it.

Storing sensitive information like API keys directly in your code can lead to major security risks. For example, there were cases of OpenAI API keys being leaked, which is not very good. To prevent this, we’ll go over the correct way to handle secret keys in your project. Let’s get started!

Step 1: Create the .env file

To begin, open your project in VS Code, or any editor, and create a new file called .env. This file will hold your secret keys and sensitive information.

Step 2: Write the environment variables

Inside the .env file, write your variables as key-value pairs. For example, if you have an API key, write:

API_KEY=your_secret_key
Enter fullscreen mode Exit fullscreen mode

Make sure there are no spaces around the equals sign.

Step 3: Add .env to .gitignore

Next, it’s important to prevent your secret keys from being committed to GitHub. Because it can lead to other developers viewing your secret keys. Open your .gitignore file and add .env to it. This will ensure your .env file isn’t pushed to your repository, keeping your sensitive data private.

Step 4: Use the environment variables in your code

Now, to use the keys in your code, you can access them with process.env. Here’s an example in JavaScript:

const apiKey = process.env.API_KEY;
Enter fullscreen mode Exit fullscreen mode

Now, your API key is securely stored in the .env file and easily accessible in your code.

Step 5: Install dotenv (Optional)

If you’re working on a Node.js project, you’ll need to install the dotenv package to load the .env file. To to this, open up terminal and run this command:

npm install dotenv
Enter fullscreen mode Exit fullscreen mode

Then, in your javascript file, add:

require('dotenv').config();
Enter fullscreen mode Exit fullscreen mode

Or if you prefer using import instead of require, here’s how you can do it. Go to your package.json file, add “type”: “module”. Now, in your JavaScript file, instead of using require(), you can use import() to import your secret keys. Personal preference, but I like the second approach more.

And that’s it! A simple and secure way to store secret keys in a .env file.

Top comments (8)

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)

Or use dotenvx and even enjoy additional encryption capabilities.

Collapse
 
_bbb2762792e3f125a5ad7b profile image
张磊

i have simple and convinent way to store and use .env.

  1. just add a priviate submodule:
  2. put your sensitive info to the submodule.
  3. write a copy script in your main project,to copy .env from your submodule.
  4. every time u init your project,run the script
Collapse
 
meley profile image
Martin Eley

You can also use the --env-file flag.

Collapse
 
miguelgisbert profile image
Miguel Gisbert

And how to make it work on prod?

Collapse
 
thevediwho profile image
Vaibhav Dwivedi

Simple yet essential tutorial. Good one, friend!

Collapse
 
ngtduc693 profile image
Duc Nguyen Thanh

that ways I tried

Some comments may only be visible to logged-in visitors. Sign in to view all comments.