https://github.com/faizan-devs/api-rate-limiter-nodejs
A complete Node.js API Rate Limiter system with hashed API keys, plan-based usage limits, warning emails on 90% and 100% quota usage, daily CSV reports, and scheduled cron jobs for automation.
https://github.com/faizan-devs/api-rate-limiter-nodejs
api automate backend exec express javascript mongodb mongoose nodecron nodejs nodemailer nodemon
Last synced: 3 months ago
JSON representation
A complete Node.js API Rate Limiter system with hashed API keys, plan-based usage limits, warning emails on 90% and 100% quota usage, daily CSV reports, and scheduled cron jobs for automation.
- Host: GitHub
- URL: https://github.com/faizan-devs/api-rate-limiter-nodejs
- Owner: faizan-devs
- Created: 2025-07-15T10:43:54.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-15T11:03:41.000Z (12 months ago)
- Last Synced: 2025-10-09T07:36:44.612Z (9 months ago)
- Topics: api, automate, backend, exec, express, javascript, mongodb, mongoose, nodecron, nodejs, nodemailer, nodemon
- Language: JavaScript
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# API Rate Limiter System (with API Key, Daily Limits & Email Notifications)
This project implements a robust **API Rate Limiting System** that restricts API usage based on user subscription plans (`basic`, `pro`, `premium`). It features **API key generation**, **daily usage tracking**, **email alerts at 90% and 100% usage**, and **CSV reporting via cron job**.
---
## π Key Features
- π **API Key Generation** for each user.
- π **Daily API Usage Tracking** per user via MongoDB.
- β οΈ **Automatic Email Warning** at 90% usage.
- β **Limit Reached Email** and block at 100% usage.
- π¬ **Daily CSV Usage Report** emailed to admin at 8 AM.
- π **Usage Reset** every midnight via scheduled cron.
- π **API Key Regeneration** (admin only).
- π§Ύ **User Dashboard (Admin Route)** with daily usage summary.
---
## π§° Tech Stack
| Layer | Tech/Tool |
| ----------- | ------------------------- |
| Backend | Node.js, Express.js |
| Database | MongoDB (Mongoose ODM) |
| Email | Nodemailer + Gmail SMTP |
| Scheduler | node-cron |
| Data Format | json2csv (CSV generation) |
| Security | Hashed API Keys (SHA-256) |
---
## π§ͺ API Routes and Usage (Postman Guide)
### 1. Create User (with API Key)
`POST /api/users`
```
{
"name": "Faizan",
"email": "faizan@example.com",
"plan": "basic"
}
```
### 2. Regenerate API Key (Admin)
`PUT /api/admin/users/:id/apikey`
Response will contain a new raw API key (shown once only).
### 3. Rate-Limited Test API Route
`GET /api/data`
Header:
```makefile
x-api-key: YOUR_API_KEY_HERE
```
### 4. Admin Dashboard
`GET /api/admin/dashboard`
π Returns all users with todayβs usage.
### 5. Update User Details
`PUT /api/users/:id`
```json
{
"name": "Updated Name",
"email": "newemail@example.com"
}
```
`scripts/dailyReport.js`
Runs every day at 8 AM (via separate cron or manually) to:
π€ Generate CSV of daily usage
π§ Email it to ADMIN_EMAIL defined in .env
### .env Configuration
Create a `.env` file:
```ini
PORT=3000
MONGO_URI=mongodb+srv://your-db-uri
EMAIL_USER=youremail@gmail.com
EMAIL_PASS=yourapppassword
ADMIN_EMAIL=admin@example.com
```
---
### Future Implementations
- π JWT-based authentication + role-based access.
- π° Stripe integration for upgrading plans.
- π Analytics dashboard with charts (React/Next.js).
- π¬ Admin alerts for users hitting limits.
- π§ͺ API testing dashboard (Postman clone UI).
- π Download logs & reports from dashboard.
## Scripts
`npm run dev` β Start dev server (with nodemon)
`node index.js` β Run usage reset cron
`node scripts/dailyReport.js` β Send daily CSV