Simple rate limiting middleware for Express. Drop it in, set your limits, done.
npm install @hand-on-web/api-rate-limiterconst express = require('express');
const { rateLimiter } = require('@hand-on-web/api-rate-limiter');
const app = express();
// 100 requests per 60 seconds per IP
app.use(rateLimiter({
windowSeconds: 60,
maxRequests: 100
}));
app.get('/api/data', (req, res) => {
res.json({ message: 'Hello' });
});
app.listen(3000);| Option | Type | Default | Description |
|---|---|---|---|
windowSeconds |
number | 60 |
Time window in seconds |
maxRequests |
number | 100 |
Max requests per window |
keyFn |
function | req => req.ip |
Function that returns a string key for the requester |
store |
object | MemoryStore |
Storage backend (must implement increment(key, windowMs)) |
message |
string | 'Too many requests' |
Response message when rate limited |
Rate limit by API key instead of IP:
app.use(rateLimiter({
windowSeconds: 60,
maxRequests: 1000,
keyFn: req => req.headers['x-api-key'] || req.ip
}));Uses a sliding window approach. Each request increments a counter tied to the current window. When the counter exceeds maxRequests, the middleware returns a 429 status with a Retry-After header telling the client how long to wait.
Every response includes:
X-RateLimit-Limit— your max requestsX-RateLimit-Remaining— how many you have leftX-RateLimit-Reset— when the current window resets (unix timestamp)
When rate limited (429):
Retry-After— seconds until you can try again
We build AI chatbots, voice agents, and automation tools for businesses.
- 🌐 handonweb.com
- 📧 outreach@handonweb.com
- 📍 Chester, UK
MIT