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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️