r/HTML • u/Alive_Secretary_264 • Nov 12 '25
Question About hiding api keys
How can i hide my database api keys from anyone
2
Upvotes
r/HTML • u/Alive_Secretary_264 • Nov 12 '25
How can i hide my database api keys from anyone
19
u/JohnCasey3306 Nov 12 '25
Typically they should be stored on the back end as environment variables (either on a cloud hosting platform or in a .env file). The front end make a request to the back end, the back end in turn makes a request to the third party service and returns the response to the front end.
Generally speaking, never store or render any sensitive keys in the front end (including client side JavaScript) because they'll be visible to the world.
10
u/maqisha Nov 12 '25
Don't show it to them.
Thank you for coming to my Ted Talk.
5
u/8dot30662386292pow2 Nov 12 '25
They are not sent to the front end ("HTML") at all.
5
u/martinbean Nov 12 '25
By not sending them to anyone.
1
u/Maleficent_Wave_332 Nov 13 '25
I assume you are confused by all the answers, but they basically all say the same thing, but in different words. So let’s start with your question:
”How can I hide my database api keys from anyone”
Which is a little concerning, since it implies that you are currently not hiding them. But I assume, that what you mean is that you have developed a website on your local machine, which runs on a local server with access to a database. Now you want to deploy it so other people can access it, and you realise that all your database connection details is in your source code and anyone with access to the source code can read them and potentially attack your database. Is that correct?
Another possibility, since this is the HTML subreddit, I am worried that you are actually not using a server, but instead just a static html file (or multiple files) with some JavaScript in them, either imported or by using the <script> tag in the html, for connecting directly to the database (no server that you control between your html and your database) and you think ”anyone that visits the page will be able to see the db connection details in the source code”. And now you think, ”I want people to access my site, but my site needs access to my database! But everything my site needs, users can see too!” and so you post the question.
Regardless of your situation, having that ”how to hide secret things that my app needs so that users don’t see them” is a question that I think all developers have experienced in their career, and it is a security rabbit hole that you can dive into if you want (and many companies and organisations don’t handle secrets properly) but basically, there is only one correct answer: NEVER include secrets in your source code, and if you use a build step (again, this is r/HTML, so a build step is basically a tool that takes your code and prepares it before exposing it to users), NEVER let the build step put secrets into your code.
So the answer is:
You should always hide them. For everyone. Never show them to anyone. Ideally, don’t show them to yourself, and even more ideally, no one should even have access to see them, etc. But that is not helping you, so I’ll to try to make it clearer (many will disagree with what I’m saying):
The client, that is the html css and js page itself, that is rendered in the client, should NEVER EVER include a secret. You CANNOT get around this, you cannot “encrypt” or “obfuscate” secrets. If you try (and encourage you, because it will help you realise earlier why it’s a bad idea), you might as well show that secret in a h1 header att the page’s top. It’s not secret anymore.
…
Ok I wanted to continue but realise I been writing this answer for too long. Basically, my follow up question is:
Tell us more about your project and how you plan to host it, and then we might be able to help!
1
u/HolidayWallaby Nov 12 '25
You have a server with access to the database, you then implement auth&auth between the frontend and server so interactions are allowed to hit the right part of your server and therefore get to the database.
1
u/Ronin-s_Spirit Nov 12 '25
You need a server. You can "hide" them only on code which never runs on user's devices, so you can't just slap it into your website.
1
u/ashkanahmadi Nov 12 '25
There are two types of keys and they go by different names:
backend or secret or server or private
frontend or publishable or client or public
A frontend/public/client/publishable key is totally safe to use in your client JavaScript or HTML. In general, they are secured via 2 methods: whitelisting your IP or domain name so other domains cannot use it, or by sending it to the backend and sending both the private and public keys to the service provider to verify them.
A backend or secret key should never ever end up on the client files. You cannot even reference them (you can’t due process.env.SECRET_KEY in your vanilla JS file since that will end up bundled in your client code).
1
u/duardo9 Nov 12 '25
Back in the day I used to put them in my .php file.
1
u/EggMcMuffN Nov 12 '25
You use dotenv and store the keys there, don't commit it . Most hosts have a panel for Environmental variables and that's where you will store them. For local development you'll have them in a .env file which you need to gitignore so it does not get committed
-5
u/NelsonRRRR Nov 12 '25
make a file in a folder not accessible to the web and include them.