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.
- 🔐 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.
Layer | Tech/Tool |
---|---|
Backend | Node.js, Express.js |
Database | MongoDB (Mongoose ODM) |
Nodemailer + Gmail SMTP | |
Scheduler | node-cron |
Data Format | json2csv (CSV generation) |
Security | Hashed API Keys (SHA-256) |
POST /api/users
{
"name": "Faizan",
"email": "faizan@example.com",
"plan": "basic"
}
PUT /api/admin/users/:id/apikey
Response will contain a new raw API key (shown once only).
GET /api/data
Header:
x-api-key: YOUR_API_KEY_HERE
GET /api/admin/dashboard
📊 Returns all users with today’s usage.
PUT /api/users/:id
{
"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
Create a .env
file:
PORT=3000
MONGO_URI=mongodb+srv://your-db-uri
EMAIL_USER=youremail@gmail.com
EMAIL_PASS=yourapppassword
ADMIN_EMAIL=admin@example.com
-
🔒 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.
npm run dev
– Start dev server (with nodemon)
node index.js
– Run usage reset cron
node scripts/dailyReport.js
– Send daily CSV