Skip to content

Rate Limiting & Validation

Two defenses round out API security: limiting how often a caller may hit you, and never trusting what they send.

Rate limiting caps requests per client per window, protecting against abuse, runaway clients, and accidental loops. When a caller exceeds the limit, respond 429 Too Many Requests with a Retry-After header telling them when to try again. It is good practice to advertise the budget with RateLimit-Limit, RateLimit-Remaining, and RateLimit-Reset headers.

A common algorithm is the token bucket: each client has a bucket that refills at a steady rate; each request spends a token; an empty bucket means 429.

JavaScript

Every value from a client is untrusted until validated. The main risks:

  • Injection — never build SQL/commands by string concatenation; use parameterized queries.
  • Mass assignment — do not blindly spread a request body onto a model; accept only an allowlist of fields, or a user could set isAdmin: true.
  • Oversized payloads — cap request body size and array lengths to avoid resource exhaustion.
  • Type and range — validate types, lengths, and ranges with a schema, and reject with 422 (see Validation Errors).
Which status and header signal that a client is rate limited?
What is mass assignment, and how do you prevent it?
How should you prevent SQL injection?