Skip to main content

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.

Learn more about x402

Example Client

Below is an example client implementation that demonstrates how to use x402 authentication with the Zapper API:

import axios from 'axios';
import { x402Client, wrapAxiosWithPayment } from '@x402/axios';
import { registerExactEvmScheme } from '@x402/evm/exact/client';
import { privateKeyToAccount } from 'viem/accounts';

async function main() {
const privateKey = 'YOUR_PRIVATE_KEY';

const client = new x402Client();

const signer = privateKeyToAccount(privateKey);
registerExactEvmScheme(client, { signer });

const api = wrapAxiosWithPayment(
axios.create({
baseURL: 'https://public.zapper.xyz',
headers: { 'Content-Type': 'application/json' },
}),
client
);

const response = await api.post('/graphql?tool=token_price', {
query: `query TokenPriceData($address: Address!, $chainId: Int!) {
fungibleTokenV2(address: $address, chainId: $chainId) {
symbol
address
priceData {
marketCap
price
}
}
}`,
variables: {
address: '0xbe19c96f5deec29a91ca84e0d038d4bb01d098cd',
chainId: 8453,
},
});

console.log('response:', response.data.data);
}

main().catch(console.error);

How it Works

  1. Initial Request: Make a GraphQL request without payment headers
  2. Payment Required Response: The API returns an 402 Payment Required error with payment requirements
  3. Payment Header Creation: Create a payment header using your signer and the selected requirements
  4. 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/client and x402/types)
  • Sufficient USDC balance on your chosen payment chain