Paying for APIs with x402
When your AI agent calls a paid API, the API returns HTTP402 Payment Required with a payment challenge. The x402 protocol standardizes this flow — and Conto sits in the middle to enforce policies before the agent pays.
This guide walks through the complete cycle: receive a 402, check it with Conto, pay, and track spending.
Prerequisites
How x402 Works
| Step | Who | What happens |
|---|---|---|
| 1 | Agent | Calls an x402-enabled API |
| 2 | API | Returns 402 Payment Required with amount, recipient, facilitator |
| 3 | Agent | Sends payment details to Conto for policy check |
| 4 | Conto | Evaluates x402 policies — returns authorized or denied |
| 5 | Agent | If authorized, signs payment and retries the API call |
| 6 | Agent | Records the completed transaction in Conto for tracking |
Step 1: Set Up x402 Policies
Before your agent starts paying for APIs, set guardrails. Without x402-specific policies, only your general spend limits apply.Recommended Starter Policy
Create the policy
Go to Policies → New Policy.
| Field | Value |
|---|---|
| Name | x402 API Guardrails |
| Policy Type | X402_CONTROLS |
Add rules
Add these rules to cap per-request cost and total per-service spend:Rule 1: Cap per API call
No single x402 payment can exceed $0.50.Rule 2: Budget per service
Total spend per API service domain cannot exceed $50.
| Field | Value |
|---|---|
| Rule Type | X402_MAX_PER_REQUEST |
| Operator | LTE |
| Value | 0.50 |
| Action | ALLOW |
| Field | Value |
|---|---|
| Rule Type | X402_MAX_PER_SERVICE |
| Operator | LTE |
| Value | 50 |
| Action | ALLOW |
You can also add
X402_ALLOWED_SERVICES to restrict which API domains your agent can pay. This is the strongest guardrail — the agent can only pay APIs you’ve explicitly approved.Step 2: Pre-Authorize a Payment
When your agent receives an HTTP 402, extract the payment details and check them against Conto:If Authorized
If Denied
Step 3: Record the Transaction
After the x402 payment executes on-chain, record it in Conto so it counts toward budgets and shows up in analytics:Batch Recording
For high-frequency API calls, batch multiple records in one request:Step 4: Monitor Spending
Check Budget
See how much your agent has spent and what’s remaining:View Services Used
List all x402 API services your agent has interacted with:Dashboard
Go to Analytics in the dashboard to see:- x402 spend trends over time
- Per-service cost breakdown
- Request frequency and average cost per call
Full Integration Example
Here’s how an agent handles the complete x402 flow in TypeScript:x402 Policy Reference
All available x402-specific policy rules:| Rule Type | What It Controls |
|---|---|
X402_MAX_PER_REQUEST | Max amount per single API call |
X402_PRICE_CEILING | Hard ceiling on any x402 payment |
X402_MAX_PER_ENDPOINT | Budget per specific API endpoint |
X402_MAX_PER_SERVICE | Budget per service domain |
X402_ALLOWED_SERVICES | Allowlist of API domains |
X402_BLOCKED_SERVICES | Blocklist of API domains |
X402_ALLOWED_FACILITATORS | Allowed facilitator addresses |
X402_VELOCITY_PER_ENDPOINT | Rate limit per endpoint |
X402_SESSION_BUDGET | Session-level spending cap |
Anomaly Detection
Conto automatically monitors x402 patterns and creates alerts for:| Alert | Trigger |
|---|---|
| Price Spike | API suddenly charging more than usual |
| High Frequency | Unusual burst of API calls |
| New Service | Agent paying an API for the first time |
| Budget Burn | Approaching per-service budget limit |
| Duplicate Payment | Same amount + endpoint in quick succession |
| Failed Streak | Repeated authorization failures |
Troubleshooting
Pre-authorize returns denied but I haven't set x402 policies
Pre-authorize returns denied but I haven't set x402 policies
Budget shows $0 remaining but I haven't spent that much
Budget shows $0 remaining but I haven't spent that much
Ensure you’re recording transactions after they execute. If you skip the record step, Conto can’t decrement the budget correctly. Also check if batch records are being sent for high-frequency calls.
X402_ALLOWED_SERVICES blocks everything
X402_ALLOWED_SERVICES blocks everything
The allowlist is strict — only listed domains can receive x402 payments. Make sure you’ve added the exact domain (e.g.,
api.example.com, not example.com or www.api.example.com).Pre-authorize is slow
Pre-authorize is slow
Next Steps
MPP Sessions
Session-based micropayments for streaming APIs
x402 SDK Reference
Full API reference for x402 endpoints
Advanced Policies
All x402 policy rule types and value formats
Recipes
Copy-paste x402 recipes