Skip to main content

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:
This response includes:
  • 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:
Success Response:
Insufficient Balance Response (HTTP 402):
You must have sufficient credit balance to settle x402 payments. Top up your account in the dashboard if needed.

3. Retry with Payment Proof

Once settled, retry the original request with payment headers:
The server validates the payment proof and returns the protected resource:

Transaction Lifecycle

An x402 transaction goes through these states:
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)
Source Reference: See /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:
  1. Verifies you have sufficient balance
  2. Creates a debit entry in the credit ledger
  3. Updates the transaction status to settled
  4. Generates a receipt ID for proof

Usage Tracking

When a payment is consumed (step 3), a usage event is recorded:
This enables detailed billing analytics and usage tracking.

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
Example MCP Request:
Response:
MCP requests use standard API key authentication and don’t require x402 payment flow for basic operations.

Best Practices

  1. Store Receipt IDs: Keep receipt IDs for audit trails and refund requests
  2. Handle 402 Gracefully: Build automatic retry logic after settlement
  3. Monitor Balance: Check credit balance before making paid requests
  4. Set Timeouts: Payment challenges expire after 10 minutes
  5. Validate Responses: Always check transaction status before retrying

Error Handling

Common error scenarios:

Next Steps

Making Paid Requests

Learn how to implement x402 in your application

Billing & Credits

Understand the credit-based billing system

API Reference

View detailed API endpoints for x402

Dashboard Transactions

Monitor your x402 transactions