Menu

Explorer & Settings

Tempo Explorer Submit Project
Back to all projects

x402-UPI

by dharmik-at ยท Updated 10 months ago

๐ŸŒŸ THE WORLD'S FIRST AI AGENT PAYMENT SYSTEM USING UPI A groundbreaking implementation combining **x402 Payment Required + Model Context Protocol (MCP) + India's UPI to enable AI agents to autonomously pay for services

In the AI payments ecosystem

x402-UPI is an early-stage TypeScript project in the AI payments / x402 ecosystem. It currently has 1 GitHub stars and 0 forks.

๐Ÿš€ x402-MCP-UPI: Revolutionary AI Agent Payment System

๐ŸŒŸ THE WORLD'S FIRST AI AGENT PAYMENT SYSTEM USING UPI

A groundbreaking implementation combining x402 Payment Required + Model Context Protocol (MCP) + India's UPI to enable AI agents to autonomously pay for services!

THIS WILL CHANGE EVERYTHING! ๐Ÿ”ฅ

๐ŸŽฏ REVOLUTIONARY BREAKTHROUGH

What We've Built:

๐Ÿค– AI agents that can autonomously pay for services using India's UPI system
๐Ÿ’ณ Seamless integration of x402 Protocol + MCP + UPI payments
๐ŸŒ Bridge between AI capabilities and real-world financial transactions
โšก Instant micropayments for AI tool usage
๐Ÿ” Cryptographically secured payment receipts

Why This Will Blow The Internet:

๐Ÿš€ AI Agent Economy

  • Autonomous AI Operations: Agents can now operate independently with real money
  • Pay-Per-Use AI Tools: No subscriptions - pay exactly for what you use
  • Global AI Marketplace: Any AI agent worldwide can access UPI-powered services
  • Micropayment Revolution: Enable โ‚น2-โ‚น15 payments that were impossible before

๐Ÿ‡ฎ๐Ÿ‡ณ UPI + AI = Global Game Changer

  • 300M+ UPI Users: Instant access to AI services for India's digital population
  • Real-Time Settlements: Payments settle in seconds, not days
  • Zero Infrastructure: No credit cards, banks, or complex setup needed
  • Mobile-First: Works perfectly with India's smartphone-centric economy

๐Ÿ”ง Technical Innovation

  • HTTP 402 Protocol: First real-world implementation of payment-required HTTP status
  • MCP Integration: Standard protocol for AI tool discovery and execution
  • Cryptographic Receipts: JWT-based proof of payment prevents fraud
  • Multi-Agent Support: Multiple AI agents can share the same payment infrastructure

๐ŸŒ Global Implications

  1. AI Democratization: Small AI agents can now monetize services globally
  2. Creator Economy 2.0: AI-generated content with built-in payment collection
  3. Automated Commerce: Agents trading with other agents autonomously
  4. Real-Time AI Markets: Supply and demand driven pricing for AI capabilities

๐Ÿ“‹ Overview

This system enables AI agents to autonomously pay for and consume services using India's UPI payment infrastructure. When an agent requests a protected resource, the server responds with 402 Payment Required and provides UPI payment options that the agent can handle programmatically.

Key Features

๐Ÿค– AI Agent Features (NEW!)

  • ๐Ÿ”ง MCP Tool Integration - Standard protocol for AI tool discovery
  • ๐Ÿ’ฐ Autonomous Payments - Agents can pay for services automatically
  • ๐ŸŽฏ Batch Tool Execution - Multiple tool calls with optimized payments
  • ๐Ÿ“Š Agent Analytics - Spending tracking and budget management
  • ๐Ÿ”„ Payment Strategies - Auto-pay, human approval, pre-funded wallets

๐Ÿ’ณ Payment System

  • ๐Ÿ” JWT Receipt System - Cryptographically signed payment receipts
  • ๐Ÿ“ฑ UPI Integration - QR codes, deeplinks, instant settlements
  • ๐Ÿฆ Mock PSP Service - Simulates real payment service providers
  • โšก Real-time Processing - Webhook handling and status polling
  • ๐Ÿšซ Replay Protection - Prevents receipt reuse and fraud

๐ŸŒ Human & AI Interfaces

  • ๐Ÿ“ฑ Interactive Demo Client - Beautiful web interface for humans
  • ๐Ÿค– Agent API Endpoints - RESTful APIs for AI agent integration
  • ๐Ÿ—„๏ธ SQLite Database - Comprehensive payment and usage tracking
  • ๐Ÿ“‹ Tool Registry - Discoverable AI services with pricing

๐Ÿ—๏ธ Revolutionary Architecture

๐Ÿค– AI Agent Flow (THE GAME CHANGER!)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   MCP Tool Call    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   402 Payment   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  AI Agent    โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ โ”‚ MCP Server   โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ โ”‚ UPI Gateway โ”‚
โ”‚              โ”‚                     โ”‚              โ”‚                 โ”‚             โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚   Payment Proof    โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚   Tool Result   โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚Auto-Pay  โ”‚ โ”‚ โ†โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚ โ”‚x402 Core โ”‚ โ”‚ โ†โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚ โ”‚Webhook  โ”‚ โ”‚
โ”‚ โ”‚Engine    โ”‚ โ”‚                     โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚                 โ”‚ โ”‚Handler  โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚                     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                                      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ–ฒ                                     โ”‚                                โ”‚
       โ”‚ Budget & Spending                   โ–ผ JWT Receipt                     โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚Agent Analyticsโ”‚                   โ”‚  Tool Registryโ”‚                  โ”‚  Database   โ”‚
โ”‚& Wallet      โ”‚                   โ”‚  - Summary โ‚น5 โ”‚                  โ”‚  (SQLite)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ”‚  - Translate โ‚น3โ”‚                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                   โ”‚  - Code Gen โ‚น10โ”‚
                                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ‘ฅ Human Flow (Traditional)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    402 Payment Required    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Human     โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ โ”‚   Server    โ”‚
โ”‚   Client    โ”‚                             โ”‚             โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚    UPI QR / Deeplink       โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚Web UI   โ”‚ โ”‚ โ†โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚ โ”‚Express  โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚                             โ”‚ โ”‚API      โ”‚ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                             โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚                                           โ”‚
       โ”‚ User scans QR/clicks payment             โ”‚
       โ–ผ                                           โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         Webhook               โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Mock PSP  โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ โ”‚  Database   โ”‚
โ”‚   Service   โ”‚    (Payment Success)           โ”‚  (SQLite)   โ”‚  
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿšฆ Quick Start

Prerequisites

  • Node.js 18+
  • npm or yarn

Installation

  1. Clone and setup

    cd x402-UPI
    npm install
    
  2. Generate development keys

    npm run setup-keys
    
  3. Start the server

    npm run dev
    
  4. Test the system

    For Humans: Navigate to http://localhost:3000/client/index.html

    For AI Agents:

    # Test AI agent demo
    npm run demo-agent
    
    # Or list available tools
    curl http://localhost:3000/mcp/tools/list | jq
    

๐ŸŽฏ How It Works

Step 1: Request Protected Resource

curl -X POST http://localhost:3000/generate-summary \\
  -H "Content-Type: application/json" \\
  -d '{"text": "Your article text here..."}'

Response: 402 Payment Required

{
  "error": "PaymentRequired",
  "payment_challenge": {
    "id": "payreq_abc123",
    "amount": 5.0,
    "currency": "INR",
    "expires_at": "2024-01-01T12:00:00.000Z",
    "payment_methods": ["UPI_DEEPLINK", "PSP_CHECKOUT"],
    "upi_deeplink": "upi://pay?pa=merchant@bank&pn=Demo&am=5.0&tn=payreq_abc123",
    "qr_png_url": "data:image/png;base64,...",
    "psp_checkout_url": "http://localhost:4001/mock/pay?order_id=payreq_abc123&amount=5.0"
  }
}

Step 2: Complete Payment

  • Option A: Scan QR code with UPI app
  • Option B: Click mock checkout link (simulates PSP)

Step 3: Receive Payment Confirmation

Mock PSP sends webhook โ†’ Server issues JWT receipt โ†’ Client polls status

Step 4: Access Protected Resource

curl -X POST http://localhost:3000/generate-summary \\
  -H "Content-Type: application/json" \\
  -H "Authorization: UPI-Receipt eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..." \\
  -d '{"text": "Your article text here..."}'

Response: 200 OK

{
  "success": true,
  "payreq_id": "payreq_abc123", 
  "summary": "SUMMARY for payreq payreq_abc123: Your article text here...",
  "timestamp": "2024-01-01T12:05:00.000Z"
}

๐Ÿ“š API Reference

Core Endpoints

Method Endpoint Description
POST /generate-summary Protected endpoint requiring payment
GET /payment/status?payreq=XXX Check payment status
GET /payment/receipt?payreq=XXX Get payment receipt
POST /webhook/psp PSP webhook handler

Admin Endpoints

Method Endpoint Description
GET /admin/payreqs List all payment requests
GET /health Health check
GET / Service information

๐Ÿ—‚๏ธ Project Structure

x402-UPI/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ server.ts           # Main Express application
โ”‚   โ”œโ”€โ”€ db.ts              # SQLite database wrapper  
โ”‚   โ”œโ”€โ”€ mock_psp.ts        # Mock payment service provider
โ”‚   โ”œโ”€โ”€ utils/
โ”‚   โ”‚   โ””โ”€โ”€ jwt.ts         # JWT receipt signing/verification
โ”‚   โ”œโ”€โ”€ keys/
โ”‚   โ”‚   โ””โ”€โ”€ gen_keys.js    # RSA keypair generation
โ”‚   โ””โ”€โ”€ client/
โ”‚       โ””โ”€โ”€ index.html     # Demo web interface
โ”œโ”€โ”€ migrations/
โ”‚   โ””โ”€โ”€ init.sql           # Database schema
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tsconfig.json
โ””โ”€โ”€ README.md

๐Ÿ”ง Configuration

Environment Variables

PORT=3000                    # Server port (default: 3000)
DB_PATH=./data/x402-upi.db  # SQLite database path

JWT Configuration

  • Algorithm: RS256 (RSA with SHA-256)
  • Key Size: 2048 bits
  • Receipt Expiry: 10 minutes
  • Keys Location: ./keys/facilitator_*.pem

๐Ÿงช Testing the Demo

  1. Start the server: npm run dev
  2. Open demo client: http://localhost:3000/client/index.html
  3. Enter text to summarize
  4. Click "Request Summary" - receives 402 Payment Required
  5. Click "Open Mock Checkout" - simulates payment
  6. Wait 3 seconds - webhook processes payment
  7. View result - summary appears after payment verification

๐Ÿฆ Mock PSP Behavior

The mock PSP simulates a real payment service provider:

  • Checkout URL: Creates a payment page
  • 3-second delay: Simulates user payment process
  • Automatic webhook: Calls merchant on success
  • Transaction ID: Generates mock transaction reference

๐Ÿ” Security Notes

โš ๏ธ DEVELOPMENT ONLY

This implementation is for demonstration purposes only. For production use:

  • โœ… Use secure key management (HSM, KMS)
  • โœ… Implement real PSP integration
  • โœ… Add webhook signature verification
  • โœ… Use HTTPS everywhere
  • โœ… Add rate limiting and validation
  • โœ… Rotate keys regularly
  • โœ… Add comprehensive logging

๐Ÿ“Š Database Schema

-- Payment requests from clients
CREATE TABLE payment_challenges (
  id TEXT PRIMARY KEY,           -- payreq_xxx
  amount REAL,                   -- Payment amount
  currency TEXT,                 -- INR
  status TEXT,                   -- PENDING, PAID, EXPIRED
  expires_at INTEGER,            -- Unix timestamp
  created_at INTEGER             -- Unix timestamp
);

-- Successful payments from PSP
CREATE TABLE payments (
  id INTEGER PRIMARY KEY,
  payreq_id TEXT,               -- Links to payment_challenges
  psp_txn_id TEXT,              -- PSP transaction ID
  amount REAL,                  -- Paid amount
  payer_vpa TEXT,               -- User's UPI ID
  created_at INTEGER            -- Unix timestamp
);

-- JWT receipts issued to clients  
CREATE TABLE issued_receipts (
  payreq_id TEXT,               -- Links to payment_challenges
  receipt TEXT,                 -- JWT token
  issued_at INTEGER             -- Unix timestamp
);

-- Consumed receipts (prevent replay)
CREATE TABLE consumed_receipts (
  receipt TEXT,                 -- JWT token (for dedup)
  payreq_id TEXT,               -- Links to payment_challenges  
  consumed_at INTEGER           -- Unix timestamp
);

๐Ÿš€ Deployment

Production Checklist

  • Replace mock PSP with real integration (Razorpay, Stripe, etc.)
  • Implement proper key management
  • Add HTTPS termination
  • Set up monitoring and logging
  • Configure database backups
  • Add input validation and sanitization
  • Implement rate limiting
  • Add webhook signature verification

Docker Support

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿ”— References


Built with โค๏ธ for demonstrating payment-required API patterns

All MCP projects →