E-Commerce REST API built with NestJS, MongoDB, Redis, AWS S3, and Stripe, featuring authentication, role-based authorization, cart, coupons, payments, refunds, and secure order management.
Live API: https://e-commerce-nest-js.vercel.app API Docs: Postman Documentation GitHub: E-Commerce-Nest-Js
| Layer | Technology |
|---|---|
| Runtime | Node.js + TypeScript |
| Framework | NestJS 11 |
| Database | MongoDB + Mongoose |
| Cache | Redis (ioredis) |
| File Storage | AWS S3 |
| Auth | JWT (Access + Refresh tokens) |
| Payments | Stripe (Checkout + Webhooks + Refunds) |
| Nodemailer | |
| Validation | class-validator + class-transformer |
| Security | Helmet, CORS, @nestjs/throttler, bcrypt |
| Deployment | Vercel |
src/
├── modules/
│ ├── users/
│ ├── brand/
│ ├── category/
│ ├── product/
│ ├── cart/
│ ├── coupon/
│ └── order/
├── DB/
│ ├── models/
│ └── repository/ # BaseRepository<T>
├── common/
│ ├── guards/ # AuthenticationGuard, AuthorizationGuard
│ ├── middleware/ # Multer (memory storage)
│ ├── service/ # S3Service, StripeService
│ ├── redis/
│ └── enum/
└── main.ts
Request
↓
Guards (Authentication → Authorization)
↓
Controller
↓
Service
↓
Repository (BaseRepository<T>)
↓
MongoDB
Signup
↓
Verify OTP
↓
Login
↓
Browse Products
↓
Add to Cart
↓
Apply Coupon
↓
Create Order
↓
Stripe Checkout
↓
Webhook (checkout.session.completed)
↓
Order Marked as Paid
- Signup with email + OTP confirmation via Nodemailer
- JWT access tokens (1h) + refresh tokens (1y)
- Full forget password flow: send OTP → verify → reset
- OTP rate limiting via Redis: max 3 attempts, 5-minute block on exceed
- Passwords hashed with bcrypt
- Phone numbers encrypted at rest
- Role-based access control:
admin/user AuthenticationGuardverifies JWT and attaches user to requestAuthorizationGuardchecks roles via@SetMetadatadecorator
- Full CRUD with soft delete on all entities
- Main image + up to 5 sub-images uploaded to S3
- S3 rollback: if DB insert fails, uploaded files are deleted automatically
- Discount percentage, stock tracking, brand and category linking
- Auto slug generation via
slugify - Pagination with search, populated brand and category
- Wishlist toggle per user (add / remove) on
UserModel
- Add products with quantity validation against real-time stock
- Auto-calculates
subTotalon every operation - Update quantity, increase by 1, decrease by 1 (minimum guard enforced)
- Remove single product or clear entire cart
- One active cart per user (
isOrdered: false)
- Admin creates coupons with validity dates, discount percentage, and minimum order price
- Custom validators: future dates only,
toDatemust be afterfromDate - Per-user usage tracking (each coupon usable once per user)
- Apply coupon returns discount value and final total before checkout
- Soft delete with automatic deactivation
- Cash on Delivery: create order directly from cart, coupon applied if provided
- Card (Stripe): create Stripe Checkout Session with full product line items
- Webhook: listens for
checkout.session.completed, marks order paid, storespaymentIntent - Refund: triggers Stripe refund, restores stock, removes coupon usage, unlocks cart
- Cancel: cancels pending unpaid orders, restores stock and coupon usage
- Stock deducted atomically with
$gteconditional update to prevent overselling - Admin can get all orders with pagination; users can get their own orders
- File upload via memory storage (Multer), streamed directly to S3
- Automatic cleanup of old files on logo / image update
- S3 rollback pattern on any failure during create or update
- OTP storage with TTL (2 minutes per OTP)
- Attempt counter with 5-minute block key
- Verified OTP flag (5-minute window to complete password reset)
- Helmet — HTTP security headers globally (
crossOriginResourcePolicy: falsefor S3 image delivery) - CORS — Enabled for all origins
- Rate Limiting — 100 requests per 60 seconds per IP via
@nestjs/throttler - Stripe Webhook excluded via
@SkipThrottleto ensure Stripe always reaches it
| Module | Endpoints |
|---|---|
| Auth / Users | 13 |
| Brand | 5 |
| Category | 5 |
| Product | 5 |
| Cart | 7 |
| Coupon | 5 |
| Order | 8 |
Full documentation with request bodies, validation rules, and response examples: Postman Docs
PORT=3000
NODE_ENV=development
MONGO_LOCAL=mongodb://...
MONGO_URI=mongodb+srv://...
JWT_TOKEN=your_jwt_secret
JWT_REFRESH_TOKEN=your_refresh_secret
GMAIL_USER=your@gmail.com
GMAIL_PASS=your_app_password
AWS_REGION=us-east-1
AWS_ACCESS_KEY=...
AWS_SECRET_ACCESS_KEY=...
AWS_BUCKET_NAME=...
STRIPE_SECRET_KEY=sk_...
STRIPE_WEBHOOK_SECRET=whsec_...
Redis_URL=redis://...git clone https://github.com/diaaeldeenn/E-Commerce-Nest-Js
cd E-Commerce-Nest-Js
npm install
# add your .env file
npm run start:devEng. Diaa Eldeen