X402 Fetch
Transparent payment proxy for X402-enabled endpoints
X402 Fetch
The X402 Fetch API is a transparent proxy that automatically handles HTTP 402 (Payment Required) responses. This is the recommended way to interact with paid APIs.
How It Works
- Your app sends a request to AgnicPay's fetch endpoint
- AgnicPay forwards the request to the target URL
- If the API returns 402, AgnicPay automatically signs and attaches payment
- The response is returned to your app with payment metadata
Endpoint
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string | Yes | The target URL to fetch |
method | string | No | HTTP method (default: GET) |
maxValue | integer | No | Maximum payment in atomic units (USDC has 6 decimals) |
timeout | integer | No | Request timeout in ms (default: 30000) |
Request Headers
Pass any headers to forward to the target API:
Request Body
Any JSON body is forwarded to the target URL:
Response Headers
AgnicPay adds metadata headers to the response:
| Header | Description |
|---|---|
X-Agnic-Paid | "true" if payment was made, "false" otherwise |
X-Agnic-Amount | Amount paid in USD (e.g., "0.25") |
X-Agnic-Network | Blockchain network used (e.g., "base-sepolia") |
X-Agnic-Scheme | Payment scheme: "exact" or "upto" |
Examples
Basic GET Request
POST with JSON Body
With Maximum Payment Limit
Python Example
JavaScript Example
Error Handling
Insufficient Balance
Spending Limit Exceeded
Payment Exceeds maxValue
Best Practices
- Set maxValue - Always set a maximum payment to prevent unexpected charges
- Check response headers - Verify payment was made when expected
- Handle errors gracefully - Implement retry logic for transient failures
- Monitor spending - Use the transactions API to track costs