x402 Test Endpoint

Official machine-to-machine (M2M) test endpoint for HTTP 402 Payment Required integration testing

Mock EndpointCORS EnabledBase Network

Quick Start

GET /api/x402Always returns 402
curl -i https://402payment-test.com/api/x402
Response: HTTP 402 with WWW-Authenticate header containing payment details

How to Test x402 Payments

This endpoint simulates a real x402 payment flow, allowing you to test your machine-to-machine integration without deploying your own server.

1

Send Initial Request

Make a GET request to the endpoint. You'll receive a 402 Payment Required response.

const response = await fetch('https://402payment-test.com/api/x402');
console.log(response.status); // 402
2

Parse Payment Details

Extract payment information from the WWW-Authenticate header.

const authHeader = response.headers.get('WWW-Authenticate');
// x402 address="0x..." amount="0.01" chainId="8453" token="0x..."

const addressMatch = authHeader.match(/address="([^"]+)"/);
const amountMatch = authHeader.match(/amount="([^"]+)"/);
const tokenMatch = authHeader.match(/token="([^"]+)"/);
3

Execute Payment

Send USDC to the specified address on Base network (chainId: 8453).

import { parseUnits } from 'viem';

const hash = await writeContract({
  address: tokenAddress, // USDC on Base
  abi: erc20Abi,
  functionName: 'transfer',
  args: [paymentAddress, parseUnits(amount, 6)]
});
4

Retry with Proof

Retry the request with the transaction hash in the Authorization header.

const retryResponse = await fetch('https://402payment-test.com/api/x402', {
  headers: {
    'Authorization': `x402-proof ${hash}`
  }
});
// In production: would return 200 with protected content

Complete Code Examples

Node.js / TypeScript

viem
import { createWalletClient, http, parseUnits } from 'viem';
import { base } from 'viem/chains';

async function testX402() {
  // 1. Initial request
  const res = await fetch(
    'https://402payment-test.com/api/x402'
  );
  
  if (res.status === 402) {
    console.log('Payment Required');
    
    // 2. Parse header
    const auth = res.headers.get('WWW-Authenticate');
    const address = auth.match(/address="([^"]+)"/)[1];
    const amount = auth.match(/amount="([^"]+)"/)[1];
    const token = auth.match(/token="([^"]+)"/)[1];
    
    // 3. Pay with USDC
    const client = createWalletClient({
      chain: base,
      transport: http()
    });
    
    const erc20Abi = [{
      name: 'transfer',
      type: 'function',
      inputs: [
        { name: 'to', type: 'address' },
        { name: 'amount', type: 'uint256' }
      ],
      outputs: [{ type: 'bool' }]
    }];
    
    const hash = await client.writeContract({
      address: token,
      abi: erc20Abi,
      functionName: 'transfer',
      args: [address, parseUnits(amount, 6)]
    });
    
    console.log('Payment sent:', hash);
    
    // 4. Retry with proof
    const finalRes = await fetch(
      'https://402payment-test.com/api/x402',
      {
        headers: {
          'Authorization': `x402-proof ${hash}`
        }
      }
    );
    
    return finalRes;
  }
}

testX402();

Python

web3.py
import requests
import re
from web3 import Web3

def test_x402():
    # 1. Initial request
    res = requests.get(
        'https://402payment-test.com/api/x402'
    )
    
    if res.status_code == 402:
        print('Payment Required')
        
        # 2. Parse header
        auth = res.headers['WWW-Authenticate']
        address = re.search(r'address="([^"]+)"', auth).group(1)
        amount = re.search(r'amount="([^"]+)"', auth).group(1)
        token = re.search(r'token="([^"]+)"', auth).group(1)
        
        # 3. Pay with USDC
        w3 = Web3(Web3.HTTPProvider('https://base.llamarpc.com'))
        
        # ERC20 transfer
        contract = w3.eth.contract(
            address=token,
            abi=[{
                'name': 'transfer',
                'type': 'function',
                'inputs': [
                    {'name': 'to', 'type': 'address'},
                    {'name': 'amount', 'type': 'uint256'}
                ],
                'outputs': [{'type': 'bool'}]
            }]
        )
        
        # USDC has 6 decimals
        amount_wei = int(float(amount) * 10**6)
        
        tx = contract.functions.transfer(
            address,
            amount_wei
        ).transact()
        
        tx_hash = w3.eth.wait_for_transaction_receipt(tx)
        print(f'Payment sent: {tx_hash.hex()}')
        
        # 4. Retry with proof
        final_res = requests.get(
            'https://402payment-test.com/api/x402',
            headers={
                'Authorization': f'x402-proof {tx_hash.hex()}'
            }
        )
        
        return final_res

test_x402()

Technical Specifications

Endpoint Details

URL:
https://402payment-test.com/api/x402
Method:
GET
Response:
402 Payment Required
CORS:
Enabled (*)

Payment Details

Network:
Base (Chain ID: 8453)
Token:
USDC
Amount:
0.01 USDC
Decimals:
6

Frequently Asked Questions

Why use a test endpoint for x402?

The test endpoint allows you to validate your x402 integration without deploying your own server. It's perfect for testing agent behavior, debugging payment flows, and ensuring your code handles 402 responses correctly before going to production.

How do I handle 402 errors in my code?

Check for response.status === 402, then parse the WWW-Authenticate header to extract payment details. Execute the payment transaction, then retry your original request with the transaction hash in the Authorization header.

Can I use this endpoint for automated testing?

Yes! The endpoint is CORS-enabled and designed for machine-to-machine testing. It's perfect for CI/CD pipelines, integration tests, and agent development. The endpoint always returns consistent 402 responses with valid payment parameters.

What about network latency in M2M environments?

When testing, account for blockchain confirmation times (typically 2-12 seconds on Base). Implement exponential backoff for retries and consider using transaction receipts to verify payment completion before retrying the protected endpoint.

Do I need real USDC to test?

Yes, this endpoint uses real USDC on Base mainnet for authentic testing. The amount is minimal (0.01 USDC ≈ $0.01). For testnet development, consider using Base Sepolia with test tokens, though this specific endpoint targets production-ready testing.

Related Resources