x402 Protocol Documentation
What is x402?
x402 is a payment protocol that extends HTTP 402 (Payment Required) for blockchain-based micropayments. When you request a paid resource, the server returns a 402 status with payment instructions. After completing the payment on-chain, you can verify it and access the resource. This enables pay-per-use APIs without accounts, subscriptions, or complex authentication.
How to Use
1. Request a Paid Resource
Make a POST request to the paid endpoint. If you haven't paid, you'll receive a 402 response with payment details.
curl -X POST https://your-domain.com/api/hello
2. 402 Response Schema
The server returns payment instructions in this format:
{
"chain": "solana",
"network": "mainnet",
"asset_mint": "TBA",
"amount": "10",
"recipient": "userAddress",
"reference": "<UNIQUE_REFERENCE_PUBKEY>",
"expires_at": "<ISO8601_TIMESTAMP>",
"description": "Access to the hello endpoint result",
"solana_pay_url": "solana:..."
}3. Make the Payment
Use the Solana Pay URL to complete payment in your wallet, or construct a transaction manually:
- Send the specified
amountof SPL tokens (mint:TBA) - To the
recipientaddress - Include the
referencepublic key in the transaction - Complete before
expires_at
4. Verify Payment
Check if your payment has been confirmed:
curl https://your-domain.com/api/verify?reference=<REFERENCE>
# Response when paid:
{
"paid": true,
"sig": "<TRANSACTION_SIGNATURE>"
}
# Response when not yet paid:
{
"paid": false
}5. Access the Resource
Once payment is verified, make the same POST request again to receive the actual resource:
curl -X POST https://your-domain.com/api/hello
# Response:
{
"message": "Hello from x402 x402!",
"paid_reference": "<REFERENCE>"
}Protocol Discovery
Clients can discover x402 capabilities via the well-known endpoint:
curl https://your-domain.com/.well-known/x402.json
Implementation Notes
- Each payment request has a unique reference (2-minute expiry by default)
- References are single-use; once verified, they cannot be reused
- Payment verification checks for finalized transactions on-chain
- The amount must match or exceed the requested amount
- Expired references return 404 from the verify endpoint