Open app

Pay from an agent

Run an agent that pays services by the call, settles on time and survives restarts.

An agent pays through one object: agent(key). Its fetch works like the global fetch, and pays when the endpoint asks for payment. A long-running agent also needs to settle on time, retry uncertain requests, and keep its tabs across restarts.

You need an agent created in the app and its AYO_AGENT_KEY. See Fund an agent.

One client for every call

agent.ts
import { agent } from '@ayo/sdk'

export const payments = agent(process.env.AYO_AGENT_KEY!, {
  settleAt: '5000000', // settle any tab at 5 USDC (6 decimals)
})

const quote = await payments.fetch('https://quotes.example/v1/price?pair=SOL-USDC')
const results = await payments.fetch('https://search.example/v1/search?q=solana', {
  method: 'POST',
  headers: { 'content-type': 'application/json' },
  body: JSON.stringify({ limit: 20 }),
})

Create one client per agent key and pass it to every tool that makes paid calls. Endpoints that don't ask for payment are fetched normally, so the same client works for free APIs.

The agent keeps one open tab per service. The first call to a service opens it; every later call to a URL under that service's endpoint reuses it.

When tabs settle

A tab settles when the first of these happens: the service asks with Ayo-Settle: now, the total reaches settleAt, the tab sits idle for idleMs (30 seconds by default), the next call would pass the service's maximum unpaid amount, or a third of the settlement window is left (at least 60 seconds, when the window is longer than a minute).

Settling runs in the background. If it fails, the agent calls onSettlementError and tries again on the next call to that service. To settle one service on demand, or to retry a failed settlement:

ts
await payments.settle(serviceAddress)

Before the process exits, settle everything:

ts
process.on('SIGTERM', async () => {
  await payments.shutdown()
  process.exit(0)
})

A tab left open past its window can be flagged unpaid, which makes its amount public.

A request without a response

When a paid request gets no response (a timeout, a dropped connection), the agent can't know whether the service accepted its voucher, so it keeps the voucher pending. The next request to that service carries the same voucher, and before settling the agent asks the service with HEAD and Ayo-Settle: reconcile. Either way it pays for exactly the calls the service accepted. Nothing is required of your code: send the next request as usual.

Keep tabs across restarts

Open tabs, their last vouchers and any signed settlement transfer are journaled to files under .ayo/ in the working directory, one per tab. A restarted agent loads them, restarts its timers and settles what it owes. The settlement transfer is saved before it is broadcast, so after a crash the agent resubmits the same transfer rather than paying again.

agent.ts
import { agent } from '@ayo/sdk'

export const payments = agent(process.env.AYO_AGENT_KEY!, { storeDir: '/var/lib/my-agent/ayo' })

Point storeDir at a folder that survives deploys, or pass a store with your own load and save (see AgentStore). Run one process per agent key, and keep the folder private: it holds signed vouchers.

What the owner sees

Every accepted call is posted to the Ayo API as an activity record encrypted to the agent's viewing key. The owner's page for the agent shows each call, its service, its amount, and the running total of each tab, decrypted in the owner's browser. Records are posted in the background, so posting never delays a call; onActivityError tells you when a post fails.