Service payer
An agent that pays per-call API fees to a single service. Useful for any pay-per-use API that bills onchain.
Policy
const policies = [
{ type: "spend", token: "USDC", amount: 100, period: "monthly" },
{ type: "contract", whitelist: ["api.acme.eth"] },
{ type: "rate", count: 100, period: "hourly" },
] as const;100 USDC monthly cap. Single recipient. Up to 100 calls per hour.
Implementation
import { createAgent } from "@agensai/sdk";
const agent = await createAgent({
name: "my-agent",
chainId: 8453,
apiKey: process.env.AGENSAI_API_KEY!,
ownerPrivateKey: process.env.AGENSAI_OWNER_PRIVATE_KEY! as `0x${string}`,
policies: [
{ type: "spend", token: "USDC", amount: 100, period: "monthly" },
{ type: "contract", whitelist: ["api.acme.eth"] },
{ type: "rate", count: 100, period: "hourly" },
],
});
async function callApi(payload: unknown) {
// 1. Pay the fee onchain.
await agent.execute({ to: "api.acme.eth", amount: "0.50 USDC" });
// 2. Make the actual API request, attaching the agent's ENS as auth.
const res = await fetch("https://api.acme.example/v1/run", {
method: "POST",
headers: { "x-agent": agent.name, "content-type": "application/json" },
body: JSON.stringify(payload),
});
return res.json();
}Notes
- The API can verify the call by reading the agent's recent transactions and confirming the fee landed.
- The agent's ENS name is the natural auth identifier — no API keys to rotate.
- If usage spikes unexpectedly, the rate and spend policies cap the damage automatically.