A production-grade, real-time fraud detection platform with AI-powered risk scoring, analyst workflows, and live transaction monitoring.
Features Β· Tech Stack Β· Architecture Β· Setup Β· API Reference
FraudShield is a full-stack MERN application that simulates a real-world payment fraud detection system. Every incoming transaction is analyzed by an AI engine (Groq + Llama 3) that computes a risk score from 0β100, flags suspicious activity, and surfaces actionable alerts for human analysts to review.
The system mirrors workflows used at fintech companies like Razorpay, PayU, and Stripe β where ML-based risk scoring is combined with analyst review queues to minimize false positives while catching genuine fraud.
Built to demonstrate: real-time systems design, AI integration, REST API architecture, MongoDB aggregation pipelines, and production-grade React UI patterns.
- Risk scoring (0β100) on every transaction using Groq AI (Llama 3)
- Factors analyzed: transaction amount, time of day, merchant reputation, user history, location anomaly, device type, payment method
- Automatic model fallback chain β if one Groq model hits rate limits, the engine silently retries with the next available model
- Rule-based fallback β if AI is unavailable, a deterministic scoring engine kicks in so the system never goes dark
- Live transaction feed via Socket.io β zero polling
- 7-day fraud trend (bar chart), risk distribution (pie chart), volume by payment method
- Stats: total transactions, flagged count, pending reviews, confirmed fraud, total volume
- Prioritized queue of flagged transactions sorted by risk score
- One-click analyst actions: Confirm Fraud, False Positive, Approve
- AI Explain button β asks the AI to explain in plain English why a transaction was flagged
- Real-time badge counter on sidebar updates without page refresh
- Full CRUD with server-side filtering, search, and pagination
- Review modal with inline risk score breakdown, flagging reasons, and analyst notes
- Audit trail: every decision is stamped with analyst name and timestamp
- Fraud rate per merchant auto-calculated from transaction history
- Dynamic risk levels (Low / Medium / High / Critical) based on fraud rate thresholds
- Blacklist / unblacklist merchants β blacklisted merchants automatically increase risk score on new transactions
- Conversational interface backed by Groq AI with full transaction context injected into every query
- Suggested questions for quick analysis
- Maintains conversation history for multi-turn dialogue
| Layer | Technology | Reason |
|---|---|---|
| Frontend | React 18, React Router v6 | Component model, client-side routing |
| State Management | React Context API | Lightweight β no Redux overhead needed |
| Charts | Recharts | Composable, React-native charting |
| Real-Time | Socket.io Client | Bi-directional event streaming |
| Backend | Node.js + Express.js | Non-blocking I/O, ideal for event-driven systems |
| Database | MongoDB Atlas + Mongoose | Flexible schema, powerful aggregation pipeline |
| Authentication | JWT + bcryptjs | Stateless auth, bcrypt salt rounds: 12 |
| AI / LLM | Groq API (Llama 3.1, Llama 3, Mixtral) | Free tier, fastest inference available (~500 tok/s) |
| Real-Time Server | Socket.io | Push fraud alerts to all connected dashboards instantly |
| Validation | express-validator | Schema-level request validation |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β React Frontend β
β Dashboard β Transactions β Alerts β Merchants β AI β
β Socket.io Client β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β HTTP + WebSocket
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ
β Express.js Backend β
β β
β /api/auth /api/transactions /api/merchants β
β /api/alerts /api/ai β
β β
β ββββββββββββββββββββββββββββββββ β
β β Fraud Engine β β
β β 1. Build transaction prompt β β
β β 2. Call Groq AI (w/fallback)β β
β β 3. Parse risk score 0-100 β β
β β 4. Rule-based fallback β β
β ββββββββββββββββ¬ββββββββββββββββ β
β β β
β Socket.io ββββββ€ Emit: new_transaction, fraud_alert β
βββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββ
β MongoDB Atlas β
β users β transactions β merchants β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββ
β Groq AI API β
β llama-3.1-8b-instant β llama3-8b-8192 β mixtral β
β (fallback chain) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Why Socket.io over polling? Fraud alerts are time-sensitive. A polling interval of even 5 seconds means an analyst might miss a critical transaction window. Socket.io pushes events the instant a transaction is processed, keeping every connected dashboard in sync.
Why a model fallback chain?
Free-tier AI APIs have per-minute and per-day quota limits. Rather than failing with a 429 error, the fraud engine silently retries with the next model in the chain (llama-3.1-8b-instant β llama3-8b-8192 β mixtral-8x7b-32768). If all AI models are exhausted, a deterministic rule-based engine takes over β the system never returns an error to the user.
Why MongoDB aggregation pipelines?
Dashboard stats (fraud rates, 7-day trends, category breakdowns) are computed server-side using MongoDB's $group, $match, and $sort stages rather than fetching all documents and calculating in JavaScript. This keeps response times fast even with large datasets.
fraud-detection/
β
βββ backend/
β βββ middleware/
β β βββ auth.js # JWT verification middleware
β βββ models/
β β βββ User.js # Analyst accounts (bcrypt hashed passwords)
β β βββ Transaction.js # Core transaction schema with risk fields
β β βββ Merchant.js # Merchant profiles with fraud rate tracking
β βββ routes/
β β βββ auth.js # Register, login, /me
β β βββ transactions.js # CRUD, stats aggregation, analyst review
β β βββ merchants.js # List, blacklist toggle
β β βββ alerts.js # Flagged transaction queue
β β βββ ai.js # Explain transaction, analyst chat
β βββ services/
β β βββ fraudEngine.js # AI scoring logic + model fallback chain
β βββ seed/
β β βββ seedData.js # 200 realistic transactions + demo users
β βββ server.js # Express app + Socket.io setup
β βββ package.json
β βββ .env.example
β
βββ frontend/
βββ public/
β βββ index.html
βββ src/
βββ context/
β βββ AuthContext.jsx # Global auth state (login/logout/token)
βββ utils/
β βββ api.js # Axios instance + all API call functions
βββ components/
β βββ Layout.jsx # Sidebar nav + Socket.io live badge updates
βββ pages/
β βββ Login.jsx # Auth with demo credential shortcut
β βββ Register.jsx # New analyst registration
β βββ Dashboard.jsx # Stats + charts + live feed counter
β βββ Transactions.jsx # Full table with filters, review modal
β βββ Alerts.jsx # Fraud alert queue with AI explain
β βββ Merchants.jsx # Merchant risk grid + blacklist controls
β βββ AIAnalyst.jsx # Chat interface with context injection
βββ App.jsx # Router + protected/public route guards
βββ index.js
βββ index.css # Full dark-mode design system (no UI lib)
- Node.js 16+
- MongoDB Atlas account (free) β cloud.mongodb.com
- Groq API key β console.groq.com
git clone https://github.com/yourusername/fraud-detection.git
cd fraud-detectioncd backend
npm install
cp .env.example .envEdit .env:
MONGO_URI=mongodb+srv://<user>:<password>@cluster0.xxxxx.mongodb.net/fraud-detection
JWT_SECRET=your_long_random_secret_here_minimum_32_chars
GROQ_API_KEY=gsk_your_groq_key_here
PORT=5000
FRONTEND_URL=http://localhost:3000Populates MongoDB with 200 realistic transactions across 10 merchants and 5 users, with a realistic mix of low/medium/high/critical risk scores.
npm run seedExpected output:
β
Connected to MongoDB
ποΈ Cleared existing data
β
Seeded 10 merchants
β
Seeded 200 transactions
β
Updated merchant fraud stats
β
Created demo user: analyst@demo.com / demo1234
π Seed complete!
npm run dev
# β
MongoDB connected
# π Server running on port 5000cd ../frontend
npm install
npm start
# Opens http://localhost:3000Email: analyst@demo.com
Password: demo1234
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| POST | /api/auth/register |
β | Create analyst account |
| POST | /api/auth/login |
β | Login, returns JWT |
| GET | /api/auth/me |
β | Get current user profile |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| GET | /api/transactions |
β | List with filters: riskLevel, isFlagged, status, search, page, limit |
| GET | /api/transactions/stats |
β | Dashboard stats + 7-day trend + risk distribution |
| GET | /api/transactions/:id |
β | Single transaction detail |
| POST | /api/transactions |
β | Create transaction β triggers AI fraud scoring |
| PUT | /api/transactions/:id/review |
β | Submit analyst decision (Approved, Confirmed Fraud, False Positive) |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| GET | /api/merchants |
β | List with filters: riskLevel, search |
| GET | /api/merchants/:merchantId |
β | Single merchant profile |
| PUT | /api/merchants/:merchantId/blacklist |
β | Toggle blacklist status |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| GET | /api/alerts |
β | Pending flagged transactions, sorted by risk score |
| GET | /api/alerts/summary |
β | Count by risk level (for sidebar badge) |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| POST | /api/ai/explain |
β | Plain-English explanation of why a transaction was flagged |
| POST | /api/ai/chat |
β | Conversational fraud analyst with transaction context |
| Event | Direction | Payload | Description |
|---|---|---|---|
new_transaction |
Server β Client | Transaction object | Fires on every new transaction |
fraud_alert |
Server β Client | Transaction object | Fires when isFlagged: true |
transaction_reviewed |
Server β Client | Updated transaction | Fires after analyst review |
- All private routes protected with JWT middleware β unauthenticated requests return
401 - Passwords hashed with bcryptjs at 12 salt rounds
- JWT tokens expire after 30 days
- CORS restricted to
FRONTEND_URLenvironment variable only - Request body validation on all POST/PUT routes via
express-validator .envexcluded from version control via.gitignoreβ secrets never committed