import Para, { Environment } from '@getpara/server-sdk';
import { createParaAccount } from '@getpara/viem-v2-integration';
import { wrapFetchWithPayment } from '@coinbase/x402';
class PaymentAgent {
private para: Para;
private viemAccount: any;
async initialize() {
this.para = new Para(Environment.BETA, process.env.PARA_API_KEY);
this.viemAccount = await createParaAccount(this.para);
}
async payForService(url: string, maxAmount: string) {
const paymentFetch = wrapFetchWithPayment(fetch, this.viemAccount, {
maxAmount
});
return await paymentFetch(url);
}
}
// Usage
const agent = new PaymentAgent();
await agent.initialize();
const response = await agent.payForService('https://api.example.com/premium', '0.10');