๐ 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
x402-UPI is an early-stage TypeScript project in the AI payments / x402 ecosystem. It currently has 1 GitHub stars and 0 forks.
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! ๐ฅ
๐ค 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
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.
โโโโโโโโโโโโโโโโ 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โ
โโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโ 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) โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
Clone and setup
cd x402-UPI
npm install
Generate development keys
npm run setup-keys
Start the server
npm run dev
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
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"
}
}
Mock PSP sends webhook โ Server issues JWT receipt โ Client polls status
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"
}
| 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 |
| Method | Endpoint | Description |
|---|---|---|
GET |
/admin/payreqs |
List all payment requests |
GET |
/health |
Health check |
GET |
/ |
Service information |
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
PORT=3000 # Server port (default: 3000)
DB_PATH=./data/x402-upi.db # SQLite database path
./keys/facilitator_*.pemnpm run devThe mock PSP simulates a real payment service provider:
โ ๏ธ 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
-- 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
);
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
MIT License - see LICENSE file for details.
Built with โค๏ธ for demonstrating payment-required API patterns
The living ecosystem where AI agents complete tasks through workflow loops, improve through iterative execution, are evaluated by mentor agents or humans in the loop, and turn completed work into reusable work experience and data to improve future agents.
Daydreams is a set of tools for building agents for commerce
The first agentic payment network: policy-controlled, gasless, and real money-ready. OmniClaw CLI + Financial Policy Engine let autonomous agents pay and earn safely at machine speed.
Agent behavior that compiles
The A2A x402 Extension brings cryptocurrency payments to the Agent-to-Agent (A2A) protocol, enabling agents to monetize their services through on-chain payments. This extension revives the spirit of HTTP 402 "Payment Required" for the decentralized agent ecosystem.
Live data for AI agents โ search, research, markets, crypto, X/Twitter. Pay-per-call via x402 micropayments.