b402 Protocol
The b402 Protocol is a decentralized payment standard that extends HTTP 402 (Payment Required) for autonomous AI agent transactions.
The HTTP 402 Status Code
HTTP 402 was reserved in the original HTTP specification for future payment systems. While it remained unused for decades, the rise of AI agents and micropayments has created the perfect use case.
HTTP/1.1 402 Payment Required
X-B402-Version: 1.0.0
X-B402-Payment-Address: 0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb
X-B402-Amount: 10.5
X-B402-Token: USDT
X-B402-Chain-Id: 56How b402 Works
1. Service Discovery
An AI agent discovers a service it needs:
const services = await client.discover({
capability: 'translation',
maxPrice: '1.0',
language: 'en-to-zh'
});2. Payment Request
The service provider responds with a 402 status and payment details:
{
"status": 402,
"payment_id": "pay_abc123",
"amount": "0.5",
"currency": "USDT",
"recipient": "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
"expires_at": "2024-01-15T10:30:00Z"
}3. Payment Execution
The client agent executes the payment via smart contract:
const payment = await client.payment.create({
paymentId: 'pay_abc123',
amount: '0.5',
currency: 'USDT',
recipient: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'
});4. Service Delivery
Once payment is confirmed, the service is delivered:
// Service provider verifies payment
const verified = await verifyPayment('pay_abc123');
if (verified) {
// Deliver the service
return translateText(input);
}Key Features
Autonomous Operation
AI agents can complete entire transactions without human intervention:
Discover services
Negotiate prices
Execute payments
Receive services
Verify delivery
Trustless Settlement
Smart contracts ensure:
Escrow: Funds are held until service delivery
Automatic Release: Payment released upon confirmation
Dispute Resolution: Built-in arbitration mechanism
Refund Protection: Automatic refunds for failed services
Micropayment Native
Optimized for small transactions:
Minimum payment: $0.0001
Average gas cost: < $0.01
Settlement time: ~3 seconds
Batch processing for efficiency
Protocol Specification
Payment Request Headers
X-B402-Version
string
Protocol version (e.g., "1.0.0")
X-B402-Payment-Address
address
Recipient wallet address
X-B402-Amount
string
Payment amount
X-B402-Token
string
Token symbol (BNB, USDT, etc.)
X-B402-Chain-Id
number
Blockchain network ID
X-B402-Nonce
string
Unique request identifier
X-B402-Expires
timestamp
Payment expiration time
X-B402-Memo
string
Optional payment memo
Payment States
enum PaymentState {
PENDING = 'pending', // Payment created
PROCESSING = 'processing', // Transaction submitted
CONFIRMING = 'confirming', // Awaiting confirmations
CONFIRMED = 'confirmed', // Payment complete
FAILED = 'failed', // Payment failed
EXPIRED = 'expired', // Payment expired
REFUNDED = 'refunded' // Payment refunded
}Smart Contract Interface
The b402 Protocol is implemented via smart contracts on BNB Chain:
interface IB402Payment {
// Create a payment request
function createPayment(
address recipient,
uint256 amount,
address token,
bytes32 serviceId
) external returns (bytes32 paymentId);
// Execute payment
function pay(bytes32 paymentId) external payable;
// Verify payment status
function verifyPayment(bytes32 paymentId)
external view returns (bool);
// Release escrowed funds
function release(bytes32 paymentId) external;
// Refund payment
function refund(bytes32 paymentId) external;
// Events
event PaymentCreated(
bytes32 indexed paymentId,
address indexed recipient,
uint256 amount
);
event PaymentCompleted(
bytes32 indexed paymentId,
address indexed payer,
uint256 amount
);
event PaymentReleased(
bytes32 indexed paymentId,
address indexed recipient
);
}Security Features
Anti-Replay Protection
Each payment request includes a unique nonce to prevent replay attacks:
const nonce = generateNonce(); // Cryptographically secure random valueExpiration Mechanism
Payments automatically expire if not completed within the specified timeframe:
const payment = await client.payment.create({
amount: '10.5',
expiresIn: 600 // 10 minutes
});Rate Limiting
Built-in protection against spam and abuse:
Maximum 100 requests per minute per agent
Exponential backoff for failed attempts
IP-based rate limiting
Comparison with Traditional Payments
Setup Time
Instant
Days to weeks
Transaction Fee
< $0.01
2-3% + fixed fee
Settlement Time
~3 seconds
1-3 business days
Minimum Amount
$0.0001
~$0.50
Autonomous
✅ Yes
❌ No
Programmable
✅ Yes
❌ Limited
Global
✅ Yes
❌ Restricted
Trustless
✅ Yes
❌ No
Use Cases
1. AI-to-AI Services
// Weather Agent pays Data Agent for weather data
const weather = await weatherAgent.requestService({
provider: 'data-agent-001',
service: 'current-weather',
location: 'New York',
maxPrice: '0.01'
});2. Micropayment Streaming
// Pay per second for compute resources
const stream = await client.payment.createStream({
recipient: 'compute-provider',
ratePerSecond: '0.001',
duration: 3600 // 1 hour
});3. Pay-Per-Use APIs
// Pay only for actual API calls
const result = await client.callAPI({
endpoint: 'https://api.example.com/translate',
payment: {
amount: '0.005',
currency: 'USDT'
}
});Next Steps
Learn about the Payment Flow in detail
Explore Smart Contracts implementation
Try the Quick Start Guide
The b402 Protocol is open source and community-driven
Last updated