I learned API security the hard way: by running FastAPI services in production where a single unguarded endpoint can wreck your bill or your uptime. One of my services leans on caching and rate limiting to do the quiet, unglamorous work. Another serves LLM predictions, 10K+ a day, where every abusive request costs real money in tokens. So when I talk about api security, rate limiting, and auth, I'm not quoting a checklist I found online. I'm describing the guardrails I wish I'd added on day one instead of day ninety. The OWASP API Security Top 10 is the map I keep coming back to. Let me walk you through what actually matters.
Key Takeaways
- Authentication proves who you are; authorization decides what you can touch. Confusing them is how data leaks.
- Broken object-level authorization (BOLA) is the #1 risk in the OWASP API Security Top 10 (OWASP, 2023).
- Rate limiting is your cheapest defense against abuse, scraping, and runaway LLM costs.
- Validate every input, scope every token, rotate every secret, and never trust the client.
- LLM endpoints need extra care: cost abuse and prompt-injection are real, not hypothetical.
Which threats actually hit your API?
Most breaches aren't exotic. They're boring authorization bugs at scale. The OWASP API Security Top 10 ranks broken object-level authorization (BOLA) as the number one risk (OWASP, 2023), and in my experience it's the one teams ship without noticing. You return another user's record because you forgot one ownership check.
Here's what's actually hit my services or come close. Credential stuffing against login routes. Scrapers hammering a public read endpoint until latency spikes. Someone passing `?user_id=43` and getting back data that wasn't theirs. None of these needed a zero-day. They needed me to forget one check, one limit, or one validation rule. That's the uncomfortable truth: your biggest threat is usually your own missing guardrail.
- 1Broken object-level authorization (BOLA): one user reads or edits another user's data.
- 2Broken authentication: weak tokens, no expiry, guessable sessions.
- 3Unrestricted resource consumption: no rate limits, no payload caps, no timeouts.
- 4Security misconfiguration: open CORS, verbose errors, secrets in responses.
- 5Server-side request forgery and injection from untrusted input.
In the OWASP API Security Top 10, broken object-level authorization (BOLA) is ranked the number one API risk (OWASP, 2023). It's an authorization bug, not a fancy exploit: the API forgets to confirm the caller owns the object they're asking for.
If you only fix one category this quarter, fix authorization checks on every object you return. For the broader picture of how this fits a deployed stack, I covered the infra side in deploying a full-stack app on AWS.
Authentication vs authorization: what's the difference?
They sound alike and get mixed up constantly, but they answer different questions. Authentication asks "who are you?" Authorization asks "what are you allowed to do?" The OWASP API project treats both as distinct, high-severity failure modes (OWASP, API Security Project, 2023). You need both, and passing one doesn't grant the other.
| Question | Authentication | Authorization |
|---|---|---|
| Answers | Who are you? | What can you do? |
| Mechanism | Tokens, sessions, OAuth | Scopes, roles, ownership checks |
| Fails as | Account takeover | Privilege escalation, BOLA |
| My rule | Verify on every request | Default deny, least privilege |
My working defaults are simple. Use short-lived access tokens with refresh rotation. Attach scopes to tokens so a read token can't write. Default to deny, then grant the minimum. And re-check ownership at the data layer, not just the route, because a valid token still doesn't mean the caller owns row 43. Least privilege isn't paranoia. It's the difference between a small bug and a full breach.
How do you build rate limiting that actually works?
Rate limiting is the cheapest protection you'll ever add, and it stopped more abuse on my services than any clever firewall rule. The OWASP API Top 10 lists unrestricted resource consumption as a top risk specifically because missing limits let one client degrade service for everyone (OWASP, 2023). A token bucket per client is my go-to.
In-memory limits are fine for a single process, but the moment you run multiple workers or pods, you need shared state. I use Redis so every instance sees the same counter. Here's a trimmed token-bucket dependency I've used in FastAPI. It refills at a steady rate and rejects bursts past the bucket size.
import time
from fastapi import Depends, HTTPException, Request
import redis.asyncio as redis
r = redis.from_url("redis://localhost:6379")
RATE = 5 # tokens added per second
BUCKET = 30 # max burst
async def rate_limit(request: Request):
key = f"rl:{request.client.host}"
now = time.time()
# store tokens + last-refill timestamp
data = await r.hgetall(key)
tokens = float(data.get(b"tokens", BUCKET))
last = float(data.get(b"ts", now))
tokens = min(BUCKET, tokens + (now - last) * RATE)
if tokens < 1:
raise HTTPException(status_code=429, detail="Rate limit exceeded")
tokens -= 1
await r.hset(key, mapping={"tokens": tokens, "ts": now})
await r.expire(key, 60)
# attach to any route
# @app.get("/data", dependencies=[Depends(rate_limit)])Keying on client IP is a starting point, not a finish line. Behind a proxy you key on the forwarded header you trust. For authenticated traffic, key on the user or token ID so one noisy account can't starve the rest. And always return a clear `429` with a `Retry-After` so honest clients back off gracefully. If you're new to request-level handlers, my write-up on Next.js Server Actions covers similar request-boundary thinking on the front end.
Why does input validation matter so much?
Because the client is not your friend, even when it's your own app. The OWASP API project flags injection and unsafe input handling as recurring high-severity issues (OWASP, API Security Project, 2023). Validate at the edge, reject early, and treat every field as hostile until your schema says otherwise.
- 1Use a schema (Pydantic, Zod) to reject malformed bodies before they reach business logic.
- 2Set hard limits: max payload size, max array length, max string length.
- 3Allow-list values where you can; deny-lists always miss a case.
- 4Parameterize every database query. Never build SQL with string concatenation.
- 5On output, strip internal fields and never echo stack traces to clients.
- 6Escape and encode anything that ends up in HTML, logs, or downstream calls.
One habit saved me repeatedly: a strict response model. If the API only serializes the fields I explicitly declare, I can't accidentally leak a password hash or an internal flag, even when a query over-fetches. What you choose not to return is part of your security model too.
How should you handle secrets and transport?
Encryption in transit and disciplined secret handling are non-negotiable. Security misconfiguration is a named category in the OWASP API Security Top 10 (OWASP, 2023), and leaked credentials are one of the fastest paths from "small mistake" to "full compromise." Run this checklist before every launch I ship.
- Force TLS everywhere. Redirect HTTP to HTTPS and enable HSTS.
- Keep secrets out of code and git. Use environment variables or a secret manager.
- Rotate API keys, tokens, and database credentials on a schedule, not just after a scare.
- Lock down CORS to known origins. No wildcard on authenticated endpoints.
- Set short token lifetimes and revoke on logout or compromise.
- Disable verbose error output in production; log details server-side only.
- Scan dependencies and the repo for committed secrets in CI.
What's different about securing LLM endpoints?
LLM endpoints add two threats most API guides skip: cost abuse and prompt injection. Running 10K+ predictions a day taught me that an unguarded inference route is a financial liability, not just a security one. Every uncapped request burns tokens, and the OWASP ecosystem now tracks LLM-specific risks alongside the API Top 10 (OWASP, API Security Project, 2023).
Cost abuse is the part teams underestimate. Without per-user quotas, one scripted client can run your monthly model budget in an afternoon. I cap tokens per request, set daily quotas per account, and cache common responses so repeated prompts don't repay full inference cost. I broke down that economics in detail in the cost to build a RAG system.
Prompt injection is the other surface. If user text reaches your model and the model can call tools or read documents, treat that text as untrusted input, the same way you'd treat a SQL parameter. Separate system instructions from user content, constrain what tools the model may invoke, and validate outputs before acting on them. The model is powerful, but it's also gullible. Plan for that.
Illustrative values from one of my services, not a benchmark. Shows the directional effect of adding rate limiting, quotas, and validation.
Frequently asked questions
Is rate limiting enough to stop abuse? No, but it's the highest-leverage first step. Rate limiting blunts brute force, scraping, and cost abuse, yet it won't fix a broken authorization check. The OWASP API Top 10 ranks BOLA as the #1 risk (OWASP, 2023), so pair limits with strict ownership checks.
Where should rate limiting live, in the app or a gateway? Both, ideally. A gateway catches crude floods cheaply, while in-app limits, keyed on user or token, enforce fairness per account. I run a shared Redis bucket so all my FastAPI workers agree on counts. One layer alone always leaves a gap somebody finds.
How do I secure an LLM API endpoint specifically? Treat it as both a security and a budget problem. Add per-user token quotas, cap request size, cache repeated prompts, and separate system instructions from user input to limit prompt injection. The OWASP API Security Project tracks these LLM risks (OWASP, 2023).
Where to go from here
Good API security isn't one big feature. It's a stack of small, boring habits: verify identity on every request, check ownership at the data layer, limit what each client can consume, validate everything, and keep secrets out of your code. None of these are hard on their own. The trouble is that missing just one is usually how the breach gets in. Start with the OWASP API Security Top 10, fix authorization first, then add rate limiting, then tighten the rest.
If you're building APIs or LLM endpoints and want a second set of eyes on the security and cost guardrails, I'm happy to talk through what's worked in my own production services. Ship the limits before you need them. Future you will be grateful.
Sources
- 1OWASP, API Security Project - retrieved 2026-06-23
- 2OWASP, API Security Top 10 - retrieved 2026-06-23
Get the next post
New writing on AI engineering, RAG, and shipping real products - straight to your inbox, no noise.
