Cover image - Photo by Silas Köhler on Unsplash
Photo by Silas Köhler on Unsplash

Code notes: Storing, using, and keeping environment variables secret in local environments

Reading time: About 1 min.

Published on Friday, September 27, 2019
Last edited on Friday, October 04, 2019

Environment variables are locally stored key=value pairs that can be accessed by your code. They're great for storing API keys, secrets, passwords and other sensitive material. They also help you not expose to those secrets to the public on GitHub, GitLab or Bitbucket. Here's how to create and use environment variables locally on your machine.

In the root folder of your app, create a file to store the keys. We'll call our's local-env

Store any sensitive data you'll use in your app within this file

export MY_API_KEY="ANAOFWQ14124124js214g"
export EMIAL_PASSWORD="apasswordhere"

Enter the root folder of your project using your terminal, and use the source command to bring in the local environment variables

source local-env

Now you can call the local environment variables in your app. An example with Node.js would be

const Airtable_API_Key = process.env.MY_API_KEY

One last thing to remember is to add the local-env to your .gitignore file so that it's not published next time you push your project.

Need a hand, or want to leave a comment?
Get in touch by email or on Facebook Messenger.