API Security Best Practices for Frontend Developers
Using APIs is easy. Using them securely takes a bit more thought. If you're building apps that call third-party APIs, here's what you need to know to avoid leaking keys, exposing user data, or getting your accounts banned.
Rule #1: Never put API keys in client-side code
This is the most common mistake developers make. If your API key appears anywhere in your HTML, JavaScript, or browser network requests, it's public. Anyone can view-source your page, open DevTools, or inspect network traffic to steal your key.
Your backend (Node.js, Python, etc.) makes the API call with the key stored in an environment variable. Your frontend only talks to your backend.
Store keys in environment variables
Never hardcode API keys in your source files. Use environment variables instead:
And always add .env to your .gitignore. If you've already committed a key to Git, rotate it immediately — the key is in your Git history forever, even if you delete the file.
Validate and sanitize all input
If your app takes user input and passes it to an API, validate it first. Never pass raw user input to API requests without checking it.
Implement rate limiting on your proxy
If you're proxying API requests through your backend, add rate limiting. Without it, someone could use your proxy to exhaust your API quota:
Use HTTPS everywhere
Always use HTTPS for API requests. HTTP requests send data (including API keys and user data) in plain text that anyone on the network can read. Most modern APIs require HTTPS anyway, but double-check — if an API offers both http:// and https:// endpoints, always use https://.
Limit API key permissions
Many API providers let you restrict what your key can do:
- IP restrictions — Only allow requests from your server's IP address
- Domain restrictions — Only allow requests from your website's domain
- Scope restrictions — Only grant the permissions your app actually needs
- Read-only keys — If you only need to read data, don't use a key with write access
The principle of least privilege applies: give your API key the minimum permissions it needs to function.
Handle API responses carefully
- Don't trust API data blindly. Sanitize any data you display in your HTML to prevent XSS attacks. If an API returns a name like
<script>alert('hacked')</script>, you need to escape it before rendering. - Don't expose internal API responses. If an API returns error details or internal URLs, don't pass those directly to your frontend. Translate them into safe, generic error messages.
- Don't log sensitive data. Avoid logging full API responses that might contain user data or tokens.
Security checklist
- API keys stored in environment variables, not code
.envadded to.gitignore- No API keys in client-side JavaScript
- All API calls use HTTPS
- User input validated before passing to APIs
- Rate limiting on your API proxy endpoints
- API key permissions restricted to minimum needed
- API response data sanitized before rendering in HTML
- Error messages don't expose internal API details
Security isn't a feature you add at the end — it's a habit you build from the start. Following these practices costs minimal extra effort and prevents serious problems down the road.