One visitor, a reasonable pace
Login bursts, forms filled by scripts, APIs called endlessly: set a maximum per visitor and per period, or a token budget for expensive calls.
Scenario
ALLOW · allowed2 of 3 allowed requests this minute: the request goes through, and the decision says when the counter resets.
Zarex response· excerptDocs
{
"reason": {
"current": 2,
"max": 3,
"reset_in_ms": 41250,
"window": "1m"
}
}Fixed window, sliding window or token budget
A fixed window counts requests per period; a sliding window smooths period edges; a token budget lets a burst through, then refills at your pace, and each request can consume several tokens depending on its real cost.
The counter is atomic and follows the counting server's clock, not each of your servers' clocks: clock skew does not change the limit.
Who counts as “a visitor”: your call
By default, the IP address (IPv6 addresses are grouped by /64). You can count per user, per targeted account or per route, and combine these criteria.
If none of the criteria is present on a request, Zarex falls back to the IP address: an anonymous visitor is still limited.
In your code
A strict limit on login, a token budget on an expensive API.
import { withZarex, rateLimit } from "@zarex/next";
export const POST = withZarex(
{
key: process.env.ZAREX_KEY!,
ipSource: "vercel",
rules: [rateLimit({ max: 5, window: "1m" })],
},
async (request) => Response.json({ ok: true })
);Ready for production
429 and Retry-After
Standard response, no code to write.
Atomic counter
A single reference clock.
Per IP or per user
Up to 16 combined criteria.
DRY_RUN
Find the right limit before enforcing it.
Other protections
Start by observing
Turn the rule on in DRY_RUN, watch the log, then switch to LIVE. A question before you start? Write to us.