"""x402 documentation endpoints — embedded docs site + sandbox mode.""" from __future__ import annotations import json import os import time from fastapi import APIRouter, HTTPException, Query, Request from fastapi.responses import HTMLResponse router = APIRouter(tags=["x402-Docs"]) DOCS_PAGE = """ x402 — Pay-per-Call AI Tools

⚡ x402

Pay-per-call AI tools. Send USDC, get intelligence. No subscription. No commitment.

274+ tools
7 chains
MCP-native
Free trials

Quickstart (30 seconds)

# 1. List available tools
curl https://mcp.rugmunch.io/mcp/x402 \\
  -H "Content-Type: application/json" \\
  -d '{"method":"tools/list"}'

# 2. Create invoice for a tool
curl https://mcp.rugmunch.io/mcp/x402 \\
  -d '{"method":"tools/call","params":{"name":"x402_create_invoice","arguments":{"tool":"token_scan"}}}'

# 3. Pay with USDC (Base or Solana)
#    Send the amount to the pay_to address from the invoice

# 4. Verify payment
curl https://mcp.rugmunch.io/mcp/x402 \\
  -d '{"method":"tools/call","params":{"name":"x402_verify_payment","arguments":{"tx_hash":"0x...","tool":"token_scan"}}}'

MCP Integration

Every major AI tool can discover and use x402 natively:

Claude Code

Add to claude.json: mcpServers.x402.url = "https://mcp.rugmunch.io/mcp/x402"

Cursor

Add .cursor/mcp.json: @x402 list_tools in chat

opencode

Add opencode.json: mcp.x402.url = "https://mcp.rugmunch.io/mcp/x402"

aider

Run with: aider --mcp-servers x402=https://mcp.rugmunch.io/mcp/x402

Python SDK

pip install x402-sdk

from x402_sdk import x402
client = x402()
tools = client.list_tools(category="intelligence")
invoice = client.create_invoice(tool="token_scan")
# Pay on-chain → verify
result = client.verify_payment(tx_hash="0x...", tool="token_scan")

Supported Chains

Base      USDC: 0x833589fcd6edb6e08f35c603c69b1e1c4b8e7a8b
Solana    USDC: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
Ethereum  USDC: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
BNB Chain USDC: 0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d
TRON      USDC: TEkxiTehnzSmSe2XqrBj4w32RUN966rdz

Pricing

Category        Price Range     Free Trials
Intelligence    $0.01-$0.15     2-3 per tool
Premium         $0.10-$0.30     1 per tool
Monitoring      $0.02-$0.05     2-3 per tool
Analysis        $0.05-$0.10     2 per tool
Bundles         $0.20-$0.35     1 per bundle

Discovery Endpoint

GET /.well-known/x402
→ Returns service metadata, supported chains, pricing, facilitator list

x402 — Rug Munch Media LLC · Wyoming, USA · MIT License

""" @router.get("/x402/docs") async def x402_docs_page(): """HTML documentation for the x402 payment system.""" return HTMLResponse(content=DOCS_PAGE) @router.post("/x402/sandbox/create-invoice") async def sandbox_create_invoice(tool: str = Query(...), mode: str = Query("test")): """Sandbox mode — create a test invoice without real payment. Use ?mode=test to get a fake invoice for integration testing. The invoice includes a mock payment that self-verifies. """ from app.domain.x402.middleware import create_invoice, get_tool_price_usd invoice = create_invoice(tool, "sandbox_user") if mode == "test": invoice["mode"] = "test" invoice["test_instructions"] = ( "SANDBOX MODE: Use tx_hash '0xSANDBOX_' + any 50 hex chars to verify without real payment. " "Example: 0xSANDBOX_abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" ) return invoice @router.post("/x402/sandbox/verify") async def sandbox_verify_payment(tx_hash: str = Query(...), tool: str = Query(...)): """Sandbox mode — verify a test payment. Accepts any tx_hash starting with '0xSANDBOX_' as valid. Returns mock verification result for integration testing. """ if tx_hash.startswith("0xSANDBOX_"): return { "verified": True, "tx_hash": tx_hash[:20] + "...", "tool": tool, "amount_usd": 0.01, "chain": "base (sandbox)", "mode": "test", "note": "SANDBOX: Payment verified. This was a test — no real USDC transferred.", } raise HTTPException(status_code=400, detail="Invalid sandbox tx_hash. Use 0xSANDBOX_ prefix.")