DEV Community 👩‍💻👨‍💻

Justin Bermudez
Justin Bermudez

Posted on

Hide Your API Key In NodeJS

Node has an object called 'process' that can be used throughout the whole Node project.

When starting your Node project you can have a list of all your global variables in one file which we will call '.env'

If you do not have it then add it to the top level of your directory.
Inside of your '.env' file, put in your api key with any variable in ALL CAPS

API_KEY = blahblahblahimakey

Now go to your 'app.js' file or wherever you need the key,
and require the '.env' file


Now you are able to use the key in this file by calling

But to finally hide the key, since we were able to put this key into another file. We can include this file into the gitignore so we do not need to worry about our key being open on GitHub.

Top comments (0)

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!