r/learnjavascript • u/Adamstrad • Aug 14 '26
Where should I be storing important things?
Where should I be storing things like API keys or other sensitive strings when using JavaScript?
Edit: Why is everyone insisting I need to use git, are you old enough to remember the Internet before git?
0
Upvotes
5
u/milan-pilan Aug 14 '26 edited Aug 14 '26
In your backend. There is no secure way to store secrets in the frontend.
2
u/Icy-Taste-3096 Aug 14 '26
You can put them in a
.envfile on your backend and exclude that file from git.6
u/chmod777 Aug 14 '26
i think you need to understand, and i mean really understand, what front end and back end are. front end javascript can never be secured. it will always be plain text, delivered to a user. no amount of obfuscating/minifying/uglifying will ever secure it.
if you have an actual secret or anything that has to be secured, it needs to run server side. that means the server requests a resource with the secret, and then exposes the response to your front end.
the confusion is probably because there is backend js, via nodejs, and front end js (like react) that may be rendered by node into plain js for the browser to use.
people are saying use an .env file - but that only works for your local dev enviroment. you need to find out how your server stores enviroment variables, and how your backend code accesses those variables. https://nodejs.org/api/process.html#processenv
4
u/FishBobinski Aug 15 '26
Based on your responses, I feel like you are conflating what a backend is and what git is. They are not the same concept.
1
u/InitiativeGold7953 Aug 15 '26
Pack it up bro, this isn’t for you
If you still insist just hardcode the api keys in your index.html. You do you
3
u/bonnth80 Aug 14 '26
Usually either in a .env file or your platform's environment variables.
Check out this video:
https://www.youtube.com/watch?v=YtkZR0NFd1g
PS: Do not push your .env to your repo.
1
u/itsmoirob Aug 14 '26
What is it that you have made? Is it just the front end website? If so you you shouldn't be storing API keys anywhere in your files. Those kinds of things should be on a server. Ideally you should be creating a front end and back end. The back end / server would have all your secrets, and then your front end can make authorised API calls to it, which will make the next API call on your behalf.
1
u/Aggressive_Ad_5454 Aug 15 '26
Environment variables.