SDK
createAgentFetch returns a fetch that pays x402 endpoints from your key, within budgets.
import { createAgentFetch, BudgetError } from "@tollsfun/sdk";
const agentFetch = await createAgentFetch({
secretKey: process.env.AGENT_SECRET_KEY, // or keypairPath, or any @solana/kit signer
cluster: "mainnet",
policy: { maxPerCall: "0.05", dailyCap: "1.00", perOriginDailyCap: "0.50" },
});
try {
const res = await agentFetch(url, { method: "GET" }, { maxPrice: "0.01" });
console.log(res.status, await res.json(), res.receipt);
} catch (err) {
if (err instanceof BudgetError) console.log("refused before signing:", err.reason);
}
- Picks the cheapest
exactUSDC option on your network from the402. - Checks the policy and reserves the amount before signing. Refusals throw
BudgetErrorwith areason:over_max_per_call,over_max_price,over_daily_cap,over_origin_daily_cap,origin_not_allowed,asset_not_allowed,network_mismatch. res.receipthastxSig,amount,payTo,urland anexplorerlink.- Spend is counted in memory by default. Pass
ledger: new FileLedger(path)to keep daily caps across restarts. - Paid requests need a replayable body (string or bytes).