I have an API key that my frontend application needs to use. I've heard that putting the key directly in frontend code is unsafe because anyone can inspect the site with browser developer tools and potentially reuse the key. Is there a way to use the key securely without building a backend? I'm more familiar with frontend development and not very comfortable with backend work yet.
2 Answers
It depends on what the API provider allows. Some keys are designed to be public and can be restricted by domain, app identity, allowed endpoints, or usage limits. Many keys are not safe to expose, though. A small API gateway or serverless function can proxy the request while keeping the real key on the server.
Yes, that can happen if the provider does not offer restrictions. Public keys should be limited to specific websites or APIs and monitored with quotas and billing alerts. If the key grants broad access or has no useful restrictions, keep it behind a backend.
If the key must remain private, it cannot be safely used directly by browser code. Anything sent to the frontend can be viewed by users, even if it is hidden in a bundled or environment file. In that case, you need a backend or serverless function to store the key and make the API request on the user’s behalf.

Couldn’t someone still abuse a public key, such as by sending lots of requests and increasing the bill?