An open API service indexing awesome lists of open source software.

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.

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