TypeScript example of consuming x402 resources with Faremeter
This guide demonstrates how to consume paid APIs using the x402 protocol with the Faremeter Client SDKs. We’ll walk through creating a simple buyer application that pays for string utility services.
If you prefer to use Base Sepolia Testnet, you can skip this step and use base in Step 4.
Create src/custom-chain.ts:
src/custom-chain.ts
Copy
// Note that this should satisfy the `Chain` type from viem, but due to Typescript type-safety being super strange cross version; the recommended approach is to use the `any` type to avoid issues.export const skaleBaseSepoliaTestnetV1: any = { id: 2140350733, name: "SKALE Base Sepolia Testnet", rpcUrls: { default: { http: ["https://base-sepolia-testnet.skalenodes.com/v1/basic-defiant-hadar"] } }, blockExplorers: { default: { name: "Blockscout", url: "https://base-sepolia-testnet-explorer.skalenodes.com:10011" } }, nativeCurrency: { name: "Credits", decimals: 18, symbol: "CRED" }};
The createPaymentHandler supports various configurations:
Copy
// For exact amount paymentsconst handler = createPaymentHandler(wallet, { asset: "USDC" // USDC});<Note>To override the asset to something that is not explicitly available in the @faremeter/info package, you can use the `ContractInfo` type as the asset object which requires `address` and `contractName`.</Note>// For specific token contracts (SKALE)const skaleHandler = createPaymentHandler(wallet, { asset: { address: "0x61a26022927096f444994dA1e53F0FD9487EAfcf", // Axios USD contractName: "Axios USD" }});
Most SDKs including the official ones from Coinbase currently only support USDC which is why the config for the SKALE example is more complex since we are overriding Faremeter’s default behavior.