DEV Community

Justin Bermudez
Justin Bermudez

Posted on

4 2

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

require('dotenv').config()

Now you are able to use the key in this file by calling
process.env.API_KEY

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)

nextjs tutorial video

Youtube Tutorial Series πŸ“Ί

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series πŸ‘€

Watch the Youtube series

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay