An agent pays from its vault, under the policy its principal set. This page goes from nothing to one paid call. It needs a principal wallet holding USDC and a little SOL, as a Solana keypair file, and the URL of a service that charges with @civitas/service.
Install
The agent SDK needs the two packages it builds on. The CLI, used here to make the key and charter the agent, needs all five.
npm install ./civitas-contract-0.2.0.tgz ./civitas-program-0.2.0.tgz ./civitas-agent-0.2.0.tgz ./civitas-service-0.2.0.tgz ./civitas-cli-0.2.0.tgzMake the agent's key
npx civitas keygen --out agent.jsonIt prints the new publicKey: the agent key. The file holds the secret half; keep it where only the agent's runtime can read it. Civitas never sees it.
Charter and fund the agent
The principal signs the charter and agent.json signs the key's consent. This charter lets the agent spend at most 0.5 USDC per call and 2 USDC a day, fee included, and moves 2 USDC into its vault.
npx civitas charter --keypair principal.json --agent-keypair agent.json \
--name "Research agent" --endpoint https://agent.example/ --offers "Buys market data" \
--max-per-call 500000 --max-per-period 2000000 --period-seconds 86400 \
--deposit 2000000 --api "$CIVITAS_API_URL" --rpc "$SOLANA_RPC_URL"The console at /app/charter does the same with one wallet signature. Both ways are on Charter an agent.
Call the service
createAgentFetch answers the 402 by signing for that one call and retrying.
import { readFileSync } from 'node:fs'
import { createAgentFetch } from '@civitas/agent'
const secret = Uint8Array.from(JSON.parse(readFileSync('agent.json', 'utf8')))
const agentFetch = createAgentFetch({
agent: secret,
api: process.env.CIVITAS_API_URL,
maxPerCall: 500_000n,
})
const response = await agentFetch('https://feed.example/prices')
const prices = await response.json()
console.log(response.payment?.transaction, response.payment?.receipt.servedAt)response.payment holds the settlement's transaction signature and the service's signed receipt, both already checked against the quote.
From a shell, civitas pay makes the same call:
npx civitas pay https://feed.example/prices --keypair agent.json --max-per-call 500000 --jsonSee it counted
The settlement shows on the agent's page at /agents/<agent key> and in GET /v1/agents/{key}/settlements. It is pending until the service's receipt arrives, then gets its class. A payment to an independent, listed service that sent its receipt is counted.