What is x402?
The x402 protocol is a standardized payment flow built on HTTP 402 (Payment Required). It enables machine-readable payment requirements, allowing clients to automatically detect payment needs, settle transactions, and retry requests without custom per-API billing logic.The x402 protocol is inspired by HTTP 402 Payment Required, a status code originally reserved for future payment systems.
How it Works
The x402 protocol follows a three-step challenge-response pattern:1. Initial Request (Payment Challenge)
When you make a request to a paid endpoint without payment credentials, the server responds with HTTP 402 and a payment challenge:- paymentId: Unique identifier for this payment transaction
- amountCents: Cost in cents (USD)
- settlementEndpoint: Where to settle the payment
- expiresAt: Payment challenge expiration time (10 minutes)
2. Settlement
To settle the payment, send a POST request to the settlement endpoint:3. Retry with Payment Proof
Once settled, retry the original request with payment headers:Transaction Lifecycle
An x402 transaction goes through these states:| Status | Description |
|---|---|
pending | Payment challenge issued, awaiting settlement |
settled | Payment settled, ready to be consumed |
completed | Payment consumed, resource delivered |
Payments can only be used once. After completion, the transaction is marked as consumed and cannot be reused.
Implementation Details
The x402 protocol is implemented in the ActumX API service layer: Database Schema (x402_transactions table):
- id: Unique transaction ID (prefixed with
x402tx_) - userId: Owner of the transaction
- apiKeyId: API key used for the request
- endpoint: Protected endpoint path
- amountCents: Cost in cents
- status: Transaction status (pending/settled/completed)
- receiptId: Payment receipt after settlement
- consumedAt: Timestamp when payment was used
- metadata: Additional context (e.g., query parameters)
/home/daytona/workspace/source/api/src/modules/x402/service.ts:229-248 for the payment challenge builder and /home/daytona/workspace/source/api/src/modules/x402/service.ts:250-336 for the settlement logic.
Credit Deduction
When you settle an x402 payment, the system:- Verifies you have sufficient balance
- Creates a debit entry in the credit ledger
- Updates the transaction status to
settled - Generates a receipt ID for proof
Usage Tracking
When a payment is consumed (step 3), a usage event is recorded:MCP Integration
ActumX also implements the Model Context Protocol (MCP) over x402, providing JSON-RPC 2.0 endpoints for tool calls: Available Tools:wallet_balance: Get Solana wallet balance for an agent
MCP requests use standard API key authentication and don’t require x402 payment flow for basic operations.
Best Practices
- Store Receipt IDs: Keep receipt IDs for audit trails and refund requests
- Handle 402 Gracefully: Build automatic retry logic after settlement
- Monitor Balance: Check credit balance before making paid requests
- Set Timeouts: Payment challenges expire after 10 minutes
- Validate Responses: Always check transaction status before retrying
Error Handling
Common error scenarios:| Error | Cause | Solution |
|---|---|---|
payment_not_found | Invalid payment ID | Verify the payment ID from the challenge |
insufficient_balance | Not enough credits | Top up your account balance |
invalid_payment_proof | Wrong receipt ID | Ensure you’re using the correct receipt |
payment_not_settled | Premature retry | Complete settlement before retrying |