Skip to main content

API Rate Limiter

License: MIT

Made with TypeScript

NPM Version Build Status Platform

Live Documentation

A simple and efficient API rate limiter for JavaScript/TypeScript applications. This package helps developers limit API requests per user, preventing abuse and excessive usage.

✨ Features

  • Configurable Limits – Set max requests per second, minute, or hour.
  • In-Memory Storage – Fast and lightweight.
  • Express Middleware – Easily integrate with Express.
  • Multiple Users – Tracks each user separately.

📦 Installation

npm install @the-node-forge/api-rate-limit

or using Yarn:

yarn add @the-node-forge/api-rate-limit

🛠️ Basic Usage

1️⃣ 🌐 JavaScript/TypeScript Example

Easily integrate with JavaScript/TypeScript.

// Javascript
import RateLimiter from '@the-node-forge/api-rate-limit';

// Create a rate limiter allowing 5 requests per minute
const limiter = new RateLimiter({ windowMs: 60000, maxRequests: 5 });

const userId = 'user123'; // Could be an API key or IP

if (limiter.isAllowed(userId)) {
console.log('Request allowed ✅');
} else {
console.log('Too many requests ❌');
}

2️⃣ 🌐 Express Middleware Example - Global

Easily integrate with an Express API.

import express from 'express';
// ESM
import { RateLimiter, rateLimitMiddleware } from '@the-node-forge/api-rate-limit';
// or CommonJs
const {
RateLimiter,
rateLimitMiddleware,
} = require('@the-node-forge/api-rate-limit');

const app = express();

// Set up rate limiter (10 requests per minute)
const limiter = new RateLimiter({ windowMs: 60000, maxRequests: 10 });

// Apply middleware globally
app.use(rateLimitMiddleware(limiter));

app.get('/', (req, res) => {
res.send('Welcome to my API!');
});

app.listen(3000, () => console.log('Server running on port 3000'));

3️⃣ 🌐 Express Middleware Example - Specific

Easily integrate with an Express API.

import express from 'express';
// ESM
import { RateLimiter, rateLimitMiddleware } from '@the-node-forge/api-rate-limit';
// or CommonJs
const {
RateLimiter,
rateLimitMiddleware,
} = require('@the-node-forge/api-rate-limit');

const app = express();

const limiterFiveReq = new RateLimiter({ windowMs: 60000, maxRequests: 5 });
const limiterTenReq = new RateLimiter({ windowMs: 60000, maxRequests: 10 });

// Apply rate limiting to only specific routes
app.get('/public', (req, res) => {
res.send('This route has no rate limiting!');
});

// Apply rate limiting only to this route
app.get('/limited', rateLimitMiddleware(limiterFiveReq), (req, res) => {
res.send('This route is rate-limited to 5 requests!');
});

app.post('/submit', rateLimitMiddleware(limiterTenReq), (req, res) => {
res.send('This POST request is also rate-limited to ten requests!');
});

app.listen(3000, () => console.log('Server running on port 3000'));

API Reference

RateLimiter Class

new RateLimiter({ windowMs: number, maxRequests: number });
ParameterTypeDescription
windowMsnumberTime window in milliseconds (e.g., 60000 for 1 minute)
maxRequestsnumberMaximum allowed requests in the given window

Methods

isAllowed(userId: string): boolean
MethodReturnsDescription
isAllowed(userId)booleanReturns true if the user is allowed to make a request, otherwise false.

💡 Contributing

Contributions are welcome! Please submit issues or pull requests.


⭐ Support

If you find this package useful, please **give it a ⭐ on GitHub