DEV Community

Cover image for How I Secure APIs — Practical Steps I Use Every Day
Gimhan Rajapaksha
Gimhan Rajapaksha

Posted on

How I Secure APIs — Practical Steps I Use Every Day

When I started building APIs, I often focused only on making features work. Security? That came later.

But the hard truth is — security can’t wait. Over the years, I’ve learned that securing APIs isn’t about one big fix; it’s about layering small but critical practices.

Here’s my step-by-step approach:


1. 🔐 Always Use HTTPS

  • HTTP sends everything in plain text (yes, even your passwords).
  • HTTPS encrypts data during transit, preventing man-in-the-middle attacks.

👉 Rule I follow: Never send sensitive data over HTTP.


2. 🔑 Authentication & Authorization with OAuth + OIDC

  • OAuth 2.0 handles authorization.
  • OpenID Connect (OIDC) adds authentication.
  • I enforce the principle of least privilege:
    • Users only get access to the exact endpoints they need.

👉 This keeps the attack surface small and manageable.


3. 🌐 Handle CORS Securely

  • Early on, I made the mistake of setting Access-Control-Allow-Origin: *.
  • Now, I explicitly whitelist trusted domains (e.g., https://myapp.com).

👉 No wildcard * in production.


4. ⏱️ Rate Limiting to Prevent Abuse

  • Rate limiting protects against DDoS, scraping, or accidental overload.
  • I apply limits per API key or per IP/user.

👉 Example: Free users = 5 requests/sec, Premium users = 15 requests/sec.


✅ My Security Checklist

Area Technique Used Purpose
Transport Security HTTPS Encrypt data in transit
Auth/Authz OAuth 2.0 + OIDC Secure identity & access
Access Control Principle of Least Privilege Minimize exposure
Cross-Origin CORS Whitelisting Block unauthorized access
Abuse Prevention Rate Limiting Maintain availability

Final Thoughts

API security isn’t optional — it’s essential.

These practices have become my personal checklist:

  • Encrypt everything with HTTPS
  • Authenticate and authorize with OAuth + OIDC
  • Lock down access with least privilege
  • Be strict with CORS
  • Protect performance with rate limits

If you’re just starting out, adopt these early. Trust me, it’s a lot harder to patch security holes later.


Top comments (0)