wowprompt

API rate limiter for login and public endpoints with tests

Writes server-side rate limiting for login, signup, OTP and public API routes, with the right algorithm and keys for each route, clear 429 responses and tests. Use it when bots hammer your login, an OTP route is costing money, or one client can slow the whole API.

A table of routes with limit, window and key, middleware code with a sliding window counter, 429 response format, and tests that prove the limit holds
The output this prompt produced, unedited.

The prompt

What to give

  • Language and framework: [e.g. Node.js with Express, Python with FastAPI, PHP with Laravel, Go]
  • Shared store available: [e.g. Redis, the database, none and a single server]
  • Routes to protect: [e.g. POST /login, POST /otp/send, GET /api/search, POST /contact]
  • Normal use per route: [e.g. a real user logs in a few times a day, search up to 30 times a minute]
  • Behind a proxy or CDN: [yes or no, and which header carries the client IP]
  • Signed-in users and API keys: [how requests are identified]

Task

Design the limits and write the rate limiting code.

Design checklist

  1. Pick the key per route: client IP for anonymous routes, user ID or API key for signed-in ones, and the target account or phone for login and OTP routes.
  2. Use a sliding window or token bucket; explain the choice in one line.
  3. Read the client IP only from the trusted proxy header given; never trust a header the client can set freely.
  4. Store counters in the shared store with an expiry, so limits hold across servers.
  5. Return 429 with a Retry-After header and a short JSON error body.
  6. Add a stricter second tier for repeated failures on login and OTP, such as a growing wait.
  7. Allow-list health checks and internal services.
  8. Log blocked requests with route and key, never the password or OTP.
  9. Decide what happens when the store is down: fail open for search, fail closed for OTP.

Output format

  1. | Route | Key | Limit | Window | Algorithm | On store failure |
  2. The middleware code with comments.
  3. How to attach it to each route.
  4. Tests: under the limit passes, over the limit returns 429, the window resets, separate keys stay separate.

Guardrails

  • Rate limiting slows abuse; it does not replace strong passwords, OTP expiry or bot checks.
  • Avoid limits so tight that users behind one office or mobile network IP are locked out together.
  • State every assumed number as an assumption to tune.
rate limitingapi securitybrute force protectionsliding window429 too many requestsotp abuse

How to run this

Copy the prompt and paste it into Gemini, then set the aspect ratio to 3:4. No reference photo is needed for this one.

The prompt is written in Markdown with each part under its own heading, which is what the copy action hands over. Change one section at a time, keeping the rest intact, so you can tell which change did the work.

Related prompts

All productivity