Fetch API Client
Make x402 payments using the native Fetch API
Installation
npm install @getconduit/x402-client @solana/web3.jsBasic Usage
import { X402Client } from '@getconduit/x402-client'
import { Connection, Keypair } from '@solana/web3.js'
// Initialize client
const client = new X402Client({
connection: new Connection('https://api.mainnet-beta.solana.com'),
payer: yourKeypair, // or wallet adapter
})
// Make a paid request
const response = await client.fetch('https://api.example.com/premium')
const data = await response.json()
console.log(data) // Premium contentHow It Works
1
Initial Request
Client makes request, server responds with 402 Payment Required
2
Sign Transaction
Client signs a payment transaction with the required amount
3
Retry with Payment
Client retries request with X-Payment header containing signed transaction
4
Access Granted
Facilitator verifies payment, server returns content
Advanced Example
import { X402Client } from '@getconduit/x402-client'
import { Connection, Keypair } from '@solana/web3.js'
const client = new X402Client({
connection: new Connection('https://api.mainnet-beta.solana.com'),
payer: yourKeypair,
})
// Make multiple paid requests
async function fetchPremiumData() {
try {
// First request
const response1 = await client.fetch('https://api.example.com/data/1')
const data1 = await response1.json()
// Second request (payment cached if same price)
const response2 = await client.fetch('https://api.example.com/data/2')
const data2 = await response2.json()
return { data1, data2 }
} catch (error) {
if (error.code === 'INSUFFICIENT_FUNDS') {
console.error('Not enough USDC in wallet')
} else if (error.code === 'PAYMENT_REJECTED') {
console.error('Payment was rejected')
}
throw error
}
}
fetchPremiumData()Browser Integration
import { X402Client } from '@getconduit/x402-client'
import { Connection } from '@solana/web3.js'
// Use with Phantom or other wallet adapters
const client = new X402Client({
connection: new Connection('https://api.mainnet-beta.solana.com'),
payer: window.solana, // Phantom wallet
})
// Make paid request
document.getElementById('unlock-btn').addEventListener('click', async () => {
const response = await client.fetch('https://api.example.com/premium')
const data = await response.json()
document.getElementById('content').textContent = data.message
})