Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Why the RapidAPI key stays out of git and out of the browser entirely

When I rebuilt my video downloader as plain PHP on shared hosting, the first decision was not about design or speed. It was about where the RapidAPI key lives. If that key leaks, the whole site stops working for everyone

When I rebuilt my video downloader as plain PHP on shared hosting, the first decision was not about design or speed. It was about where the RapidAPI key lives. If that key leaks, the whole site stops working for everyone, so I decided early on that the key lives in exactly one place — on the server — and nowhere else. Not in git. Not in the browser. Not in any file a visitor can fetch.

The site is ramrom.io, a free downloader for TikTok, Instagram, Facebook and YouTube. When you paste a link, the page sends it to resolve.php on my server. That script reads the key from api/config.php, adds it to the request headers, calls the provider, and sends back only the download links. The key itself never leaves the server. The browser talks to my endpoint, and my endpoint talks to the provider. That one hop is the whole security model.

config.php is a tiny file. It defines one constant and nothing else — no functions, no HTML, nothing a visitor would ever need. It sits inside the api/ folder, and a .htaccess rule blocks direct access to it, so requesting /api/config.php gets a 403. I check that rule still works after every upload, because a single bad deploy could undo it.

The part people get wrong most often is the browser. It is tempting to call the provider straight from JavaScript and skip the server round trip. It would even be faster. But JavaScript is public by definition. Anyone can open dev tools, read the source, copy the key, and call the provider themselves. Then my quota burns, my account gets flagged, and the site goes dark for real visitors. A key in the browser is not a secret at all, so it does not go there.

Git is the other trap. I do not keep this project in a public repo, but even a private one is not a safe place for a key. Repos get shared, contractors get added, a screenshot of a file list slips out, and one day a private repo goes public by mistake. So config.php is never committed. Deployment is a zip upload through the hosting file manager, and config.php stays on the server, untouched by uploads. The code I ship has a placeholder where the key goes, and the real value exists in exactly one live file.

I also keep the blast radius small. Facebook and YouTube each have their own dedicated provider, so a problem with one key does not take down every platform at once. And I never log the key anywhere. When a provider call fails, the log records the status code and the provider's error message, never the request headers. A log file is just another place a secret can hide.

If a key ever did leak, the fix is dull on purpose: generate a new key in the RapidAPI dashboard, paste it into the one file, and the site is back. No code changes, no redeploy of the whole project, no hunting through a dozen files to find every place the old key was used. That is the real payoff of keeping a secret in one place. It is not just safer — it is easier to fix when something goes wrong.

The rule sounds obvious once you write it down, and that is the point. Secrets live on the server, in one file, behind a blocked path. Everything else — the browser, the repo, the logs — is treated as public, because sooner or later it is.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.