DEV Community

Cover image for How to use securely API's in node.js?
Saad Abbasi
Saad Abbasi

Posted on

How to use securely API's in node.js?

Hi everyone
in this blog, I am going to share how you can use your APi's PORTS, Hash Secrets in node.js by using environment variables.

1) Install dotenv by running this command in node.js terminal npm install dotenv
Node.js environment variables
2) Creat new .env file name should b .env you will see an gear icon

3) define secret values and variables like the bellow snippet. follow exact same patter don't use "Quotes for strings"
node.js env file
4) require it in the root of the application. before calling any route it's good practice to use it ASAP. also call config for configuration of.env variables
node.js require and config .env
5) Finally use it anywhere in the application by using process.env.variableName see example bellow
node.js environment variable dotenv process.env

That's all for using .env in node.js. this is my first post on dev.to . please leave your feedback about this blog. (this is my first blog/tutorial post ).

Thanks
Saad Abbasi (MEAN stack Dev)

Top comments (0)