Delivery Shield - Setup Guide
Guides setup of a monorepo refund system with mock and production modes, including MongoDB, Gemini AI, and CDP integration.
What this file does
Guides setup of a monorepo refund system with mock and production modes, including MongoDB, Gemini AI, and CDP integration.
When to use it
- Setting up a delivery refund system with mock server for testing
- Configuring MongoDB Atlas with vector search for RAG
- Integrating Coinbase CDP wallet for crypto transfers
- Deploying a monorepo with shared types, backend, and frontend
Assumes this stack
Delivery Shield - Setup Guide
This guide will help you set up and run the Delivery Shield x402 refund system.
Quick Start (Using Mock Server)
The fastest way to test the system without external dependencies:
1. Install Dependencies
npm install
2. Build All Packages
npm run build
3. Start Mock Backend
cd packages/backend
npm run dev:mock
The mock server will start on http://localhost:3001 and provides all API endpoints without requiring MongoDB, Gemini API, or CDP credentials.
4. Start Frontend (in a new terminal)
cd packages/frontend
npm run dev
The dashboard will be available at http://localhost:3000
5. Test the System
- Open
http://localhost:3000in your browser - Toggle "Simulate Delivery Issues" ON to test late delivery refunds
- Click "Request Refund via x402" to process a refund
- Toggle OFF to test normal deliveries (no refund)
- Watch the statistics update in real-time
Full Setup (Production Features)
To use the complete system with MongoDB Atlas, Gemini AI, and CDP:
1. MongoDB Atlas Setup
- Create a MongoDB Atlas account at https://www.mongodb.com/cloud/atlas
- Create a new cluster
- Create a database named
delivery_shield - Create a collection named
refund_policies - Create a Vector Search index:
- Index name:
vector_index - Definition:
{ "fields": [{ "type": "vector", "path": "embedding", "numDimensions": 768, "similarity": "cosine" }] } - Index name:
2. Google Gemini API Setup
- Go to https://makersuite.google.com/app/apikey
- Create a new API key
- Copy the API key
3. Coinbase Developer Platform Setup
- Sign up at https://portal.cdp.coinbase.com/
- Create a new API key
- Save the API Key Name and Private Key
- (Optional) Create a wallet on Base Sepolia testnet
- Note the Wallet ID
4. Configure Environment Variables
cd packages/backend
cp .env.example .env
Edit .env and fill in your credentials:
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/delivery_shield
GEMINI_API_KEY=your_gemini_api_key
CDP_API_KEY_NAME=your_cdp_api_key_name
CDP_PRIVATE_KEY=your_cdp_private_key
CDP_WALLET_ID=your_wallet_id
PORT=3001
5. Start Production Backend
cd packages/backend
npm run dev
The server will:
- Connect to MongoDB Atlas
- Initialize RAG service with Gemini AI
- Initialize CDP wallet
- Insert sample refund policies
- Start on port 3001
6. Start Frontend
cd packages/frontend
npm run dev
Project Structure
delivery-shield/
├── packages/
│ ├── shared/ # TypeScript types
│ │ ├── src/
│ │ │ ├── types.ts # All shared interfaces and enums
│ │ │ └── index.ts
│ │ └── package.json
│ │
│ ├── backend/ # Express API
│ │ ├── src/
│ │ │ ├── middleware/
│ │ │ │ └── x402.ts # x402 payment middleware
│ │ │ ├── services/
│ │ │ │ ├── mongodb.ts # MongoDB Vector Search
│ │ │ │ ├── rag.ts # Gemini RAG service
│ │ │ │ └── cdp.ts # CDP wallet & transfers
│ │ │ ├── controllers/
│ │ │ │ └── refund.ts # Refund logic
│ │ │ ├── routes/
│ │ │ │ └── refund.ts # API routes
│ │ │ ├── index.ts # Main server
│ │ │ └── mock-server.ts # Mock server
│ │ └── package.json
│ │
│ └── frontend/ # React dashboard
│ ├── src/
│ │ ├── components/
│ │ │ ├── X402Button.tsx
│ │ │ ├── SimulateLatencyToggle.tsx
│ │ │ └── DashboardStats.tsx
│ │ ├── services/
│ │ │ └── api.ts # API client
│ │ ├── App.tsx
│ │ └── main.tsx
│ └── package.json
│
├── package.json # Monorepo root
├── tsconfig.base.json # Base TypeScript config
├── README.md
└── ARCHITECTURE.md
API Endpoints
Health Check
GET /health
Refund Operations
POST /api/refunds/evaluate # Evaluate refund eligibility
POST /api/refunds/process # Process refund with transfer
GET /api/refunds/status/:id # Get refund status
POST /api/refunds/simulate # Simulate delivery issues
Development Workflow
Build All Packages
npm run build
Run in Development Mode
npm run dev
Lint Code
npm run lint
Testing Scenarios
Scenario 1: Late Delivery (100% Refund)
- Enable "Simulate Delivery Issues"
- Click "Request Refund via x402"
- Expected: Full refund ($24.98), status COMPLETED
Scenario 2: Normal Delivery (No Refund)
- Disable "Simulate Delivery Issues"
- Click "Request Refund via x402"
- Expected: $0.00 refund, status REJECTED
Scenario 3: Multiple Orders
- Process several refunds with different scenarios
- Watch statistics update in real-time
- Verify average refund percentage calculation
Troubleshooting
Backend won't start
- MongoDB connection fails: Check MONGODB_URI in .env
- CDP initialization fails: Verify CDP_API_KEY_NAME and CDP_PRIVATE_KEY
- Port already in use: Change PORT in .env
Frontend build fails
- Run
npm run buildin packages/shared first - Check that shared package built successfully
- Verify node_modules are installed
Mock server recommended for testing
- Use
npm run dev:mockto avoid external dependency issues - Mock server provides same API endpoints
- Perfect for development and testing
Production Deployment
Backend (Railway/Render/Heroku)
cd packages/backend
npm run build
npm start
Set environment variables in the platform's dashboard.
Frontend (Vercel/Netlify)
cd packages/frontend
npm run build
Deploy the dist/ folder. Set VITE_API_URL environment variable to your backend URL.
Security Notes
- Never commit
.envfiles - Use environment variables for all secrets
- Rotate API keys regularly
- Use HTTPS in production
- Implement rate limiting
- Add authentication for production
Support
For issues:
- Check the logs in terminal
- Verify environment variables
- Test with mock server first
- Review ARCHITECTURE.md for system design
Next Steps
- Add user authentication
- Implement real embedding generation
- Add more refund policies
- Create admin dashboard
- Add webhook notifications
- Implement fraud detection
- Add multi-currency support
Happy coding! 🚀
What's inside
7 sections: quick start, full setup, project structure, API endpoints, testing scenarios, troubleshooting, deployment
Change this for your project
- Replace
delivery_shielddatabase name with your own - Replace
vector_indexindex name if different - Replace
delivery-shield/project root path with your repo name
Where it goes
A standard operating procedure. Keep where the team or agent running the process will find it.
Worth borrowing
- Mock server for offline development without external dependencies
- Toggle for simulating delivery issues to test refund logic
Related Documents
Comprehensive AI Assistant Tools Reference
Lists 80+ tools with MCP server associations, bulk support, parallel capability, resource impact, and execution type for AI agent workflows.
iOS Deployment Guide
Walks through setting up an iOS development environment, building a Tauri app for iOS, and publishing to the App Store or alternative channels.
How to Add Resources to Your FastMCP Server
Teaches how to add static and dynamic MCP resources to a FastMCP server, with six ready-to-copy examples for a GitHub crawler.
Continue.dev MCP Integration Setup Guide
Walks through configuring Continue.dev to connect an MCP server for spatial transcriptomics tasks, with local and remote setup options.