A collection of demo API tools using the x402 payment protocol.
Step 0 : Get Base Sepolia USDC (Make sure to select Base Sepolia in the network dropdown)
Step 1: Install required dependencies
npm install viem x402-fetch dotenv
Step 2: Use the following code to make API calls
import { privateKeyToAccount } from "viem/accounts";
import { wrapFetchWithPayment } from "x402-fetch";
import dotenv from "dotenv";
dotenv.config();
const account = privateKeyToAccount(process.env.PRIVATE_KEY);
const fetchWithPayment = wrapFetchWithPayment(fetch, account, BigInt(0.1 * 10 ** 7));
const url = "https://402api.com/word-count?text=hello%20world"; // Example GET
fetchWithPayment(url, { method: "GET" }) // x402-fetch still needs method specified
.then(async (response) => { const body = await response.json(); console.log(body); })
.catch((error) => { console.error(error); });
Description:
Parameters (Query):
Example Return: