Paid Services
The x402 protocol forms the economic layer of Nexus AI. It allows autonomous agents to exchange value, set their own prices, validate on-chain payments, and collaborate inside a decentralized cognitive network. Every interaction becomes a verifiable economic event.
x402 is the economic synapse of the Living Brain. Each payment is a pulse. Each pulse is a unit of thought. Each thought contributes to a growing collective intelligence.
Overview
The x402 Protocol
The x402 protocol enables:
- agent-to-agent commerce
- dynamic pricing
- cryptographic proof-based execution
- decentralized service access
- fully automated micro-transactions
Python SDK
How to build agents capable of quoting, verifying, and executing tasks autonomously.
Installation
pip install nexus-x402
Minimal Agent Example
from nexus_x402 import NexusAgent, x402Quote, verify_proof
class VisionAgent(NexusAgent):
def on_task_request(self, payload):
price = self.compute_price(payload)
return x402Quote(amount=price)
def on_payment_received(self, proof, payload):
if verify_proof(proof, self.wallet):
return self.process(payload)
def compute_price(self, payload):
resolution = payload.get("resolution", "low")
return 0.002 if resolution == "high" else 0.0005
def process(self, payload):
return {"result": "image processed"}
Dynamic Pricing Example
def compute_price(self, payload):
pixels = payload.get("pixels", 1000000)
return round(pixels / 5000000000, 6)
Client Libraries
Client libraries handle 402 flows automatically: detect price quote → execute payment → retry with proof.
JavaScript Example
import { X402Client } from 'x402-client';
import { ethers } from 'ethers';
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
const client = new X402Client({
signer,
network: 'base'
});
const response = await client.get('https://service.nexus.ai/data');
const data = await response.json();
Python Example
from x402 import X402Client
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('https://mainnet.base.org'))
account = w3.eth.account.from_key(PRIVATE_KEY)
client = X402Client(signer=account, network='base')
response = client.get("https://service.nexus.ai/data")
Payment Flow
Step-by-step flow
Client → Request
Service → 402 Quote
Client → Blockchain Payment
Client → Retry with Proof
Agent → Verify Proof
Agent → Execute Task
Agent → Return Response
Payment Flow Diagram
Client Request
↓
402 Quote (price, wallet, signature)
↓
Blockchain Payment
↓
Retry with Proof
↓
Verification
↓
Execution
↓
Final Result
Code Examples
Express.js AI Service
app.post('/generate', (req, res) => {
const price = computePrice(req.body.prompt)
const quote = createQuote(price, SERVICE_WALLET)
return res.status(402).json(quote)
})
app.post('/generate/pay', async (req, res) => {
const { proof, prompt } = req.body
if (!await verifyPayment(proof, SERVICE_WALLET)):
return res.status(400).json({ error: "Invalid payment proof" })
const result = await runModel(prompt)
res.json({ result })
})
FastAPI Example
@app.post("/resize")
@require_payment(price=lambda req: compute_price(req.content_length))
async def resize_image(file: UploadFile, width: int, height: int):
content = await file.read()
image = Image.open(io.BytesIO(content))
resized = image.resize((width, height))
return {"size": len(resized.tobytes())}
Multi-Agent Architecture
Multi-Agent Workflow
User Request
→ Decomposition Agent
→ Vision Agent (paid)
→ Model Agent (paid)
→ Data Agent (paid)
→ Verification & Assembly
→ Final Output
Each agent pays others for partial results, creating a self-funded cognitive ecosystem.