x402 is the 2026 micropayment protocol built on HTTP 402, letting AI agents pay for each API call with stablecoin micropayments instead of prepaid subscriptions. With 119M+ transactions as of March 2026, it is the fastest-growing paid-API pattern. This tutorial shows how to wire x402 into your Scavio API calls for usage-based billing.
Prerequisites
- Python 3.8+ or Node 20+
- An x402 client library (x402-python or x402-js)
- A funded x402 wallet (Base network)
- A Scavio x402-enabled endpoint
Walkthrough
Step 1: Install the x402 client
Install the official x402 client.
pip install x402-pythonStep 2: Configure your wallet
Point the client at your funded Base-network wallet.
from x402 import Client
client = Client(
private_key=os.environ['X402_WALLET_KEY'],
network='base'
)Step 3: Make an x402-enabled call
Call Scavio's x402 endpoint. The client handles HTTP 402 and pays automatically.
response = client.post(
'https://api.scavio.dev/x402/v1/search',
json={'query': 'AI agents 2026'}
)
print(response.json())Step 4: Verify payment receipt
Each response includes the x402 payment receipt for audit.
print('Paid:', response.headers.get('x-402-amount'))
print('Receipt:', response.headers.get('x-402-receipt'))Step 5: Batch for efficiency
Batch queries to reduce per-call payment overhead.
queries = ['query1', 'query2', 'query3']
responses = client.batch_post(
'https://api.scavio.dev/x402/v1/search',
[{'query': q} for q in queries]
)Python Example
import os
from x402 import Client
client = Client(private_key=os.environ['X402_WALLET_KEY'], network='base')
response = client.post(
'https://api.scavio.dev/x402/v1/search',
json={'query': 'AI agents 2026'}
)
print('Results:', len(response.json().get('organic_results', [])))
print('Paid:', response.headers.get('x-402-amount'), 'USDC')JavaScript Example
import { Client } from 'x402-js';
const client = new Client({
privateKey: process.env.X402_WALLET_KEY,
network: 'base'
});
const response = await client.post('https://api.scavio.dev/x402/v1/search', {
query: 'AI agents 2026'
});
console.log('Results:', response.data.organic_results.length);
console.log('Paid:', response.headers['x-402-amount'], 'USDC');Expected Output
Results: 10
Paid: 0.003 USDC
The agent paid 0.003 USDC per API call. No prepaid subscription, no minimum commitment.