x402 Authentication
In addition to API keys, the Zapper API supports authentication via the x402 protocol. This allows you to pay for API usage on a per-request basis using USDC on Base, Polygon and Avalanche, with 0 fees and instant settlement.
Example Client
Below is an example client implementation that demonstrates how to use x402 authentication with the Zapper API:
import axios, { AxiosInstance } from 'axios';
import { createPaymentHeader, selectPaymentRequirements } from 'x402/client';
import { PaymentRequirementsSchema, createSigner, type Signer } from 'x402/types';
// Chain to use for payments (Polygon and Avalanche also supported)
const PAYMENT_CHAIN = 'base';
async function graphqlWithPayment(client: AxiosInstance, signer: Signer, query: string, variables: any): Promise<any> {
let response = await client.post('/graphql', { query, variables });
const paymentError = response.data.errors?.find(
(err) => err.extensions?.code === 'PAYMENT_REQUIRED' && err.extensions?.accepts,
);
if (paymentError) {
const { x402Version, accepts } = paymentError.extensions;
const parsed = accepts.map((accept) => PaymentRequirementsSchema.parse(accept));
const selectedPaymentRequirements = selectPaymentRequirements(parsed, PAYMENT_CHAIN, 'exact');
const paymentHeader = await createPaymentHeader(signer, x402Version, selectedPaymentRequirements);
response = await client.post(
'/graphql',
{ query, variables },
{
headers: {
'X-PAYMENT': paymentHeader,
'Access-Control-Expose-Headers': 'X-PAYMENT-RESPONSE',
},
},
);
}
if (response.data.errors) {
throw new Error(`GraphQL errors: ${JSON.stringify(response.data.errors)}`);
}
return response.data.data;
}
async function main() {
const privateKey = 'YOUR_PRIVATE_KEY';
const signer = await createSigner(PAYMENT_CHAIN, privateKey);
const client = axios.create({
baseURL: 'https://public.zapper.xyz',
headers: { 'Content-Type': 'application/json' },
});
const data = await graphqlWithPayment(
client,
signer,
`query TokenPriceData($address: Address!, $chainId: Int!) {
fungibleTokenV2(address: $address, chainId: $chainId) {
symbol
address
priceData {
marketCap
price
}
}
}`,
{
address: '0xbe19c96f5deec29a91ca84e0d038d4bb01d098cd',
chainId: 8453,
},
);
console.log('response:', data);
}
main().catch(console.error);
How it Works
- Initial Request: Make a GraphQL request without payment headers
- Payment Required Response: The API returns an
402 Payment Requirederror with payment requirements - Payment Header Creation: Create a payment header using your signer and the selected requirements
- Retry with Payment: Retry the request with the payment header included
Query Pricing
The x402 protocol has no fees. API queries are priced the same as the Pay-As-You-Go plan (USD $0.001 / API credit, ~$0.003 / query). Visit our pricing page for exact query costs.
Supported Chains
The x402 authentication system supports payments on the following chains:
- Base
- Polygon
- Avalanche
Prerequisites
To use x402 authentication, you'll need:
- A private key for signing transactions
- Access to the x402 client libraries (
x402/clientandx402/types) - Sufficient USDC balance on your chosen payment chain