import { X402Client } from '@coinbase/x402-sdk';
import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('YOUR_SKALE_RPC_URL');
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const x402Client = new X402Client({
wallet: wallet,
facilitatorURL: 'https://facilitator.dirtroad.dev',
});
async function accessPaywalledResource(url: string) {
const response = await fetch(url);
if (response.status === 402) {
const paymentInfo = await response.json();
const payment = await x402Client.createPayment(paymentInfo);
await x402Client.settle(payment);
const retryResponse = await fetch(url, {
headers: {
'X-PAYMENT': JSON.stringify(payment),
},
});
return await retryResponse.json();
}
return await response.json();
}