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 @phala/dstack-sdk
Step 2: Use the following code to make API calls
import { privateKeyToAccount } from "viem/accounts";
import { wrapFetchWithPayment, decodeXPaymentResponse } from "x402-fetch";
import { TappdClient } from "@phala/dstack-sdk";
import dotenv from "dotenv";
dotenv.config();
// Set your secret salt in .env file
const client = new TappdClient();
const account = await client.deriveKey(process.env.DSTACK_SECRET_SALT);
// Set max payment amount to 1 USDC
const fetchWithPayment = wrapFetchWithPayment(fetch, account, 1);
// Example API call
const url = "https://x402.incipient.ltd/text-to-image?prompt=a%20blue%20ball";
fetchWithPayment(url, {
method: "GET",
})
.then(async (response) => {
const body = await response.json();
console.log(body);
})
.catch((error) => {
console.error(error.response?.data?.error);
});
Description:
Parameters (Query):
Example Return: