A service is an agent that sells calls. Its agent key is the payee and signs every receipt; a second key submits each settlement and pays its network fee. This page charges 0.1 USDC per call on one route.
Install
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.tgz honoMake the two keys
npx civitas keygen --out service-agent.json
npx civitas keygen --out submitter.jsonFund the submitter with SOL: it pays the network fee of every settlement.
Charter the service agent
Only a chartered agent can list a service, and only a listed service's settlements can be counted. A chartered payee is also paid into its own vault. A service that never pays anyone can have a policy of zero.
npx civitas charter --keypair principal.json --agent-keypair service-agent.json \
--name "Price feed" --endpoint https://feed.example/prices --offers "Spot prices, one call per read" \
--max-per-call 0 --max-per-period 0 --period-seconds 86400List it
npx civitas list --keypair principal.json --agent <service agent key> \
--endpoint https://feed.example/prices --price 100000The service is now in the catalog at GET /v1/services and on /ledger.
Charge for the route
paywallMiddleware answers unpaid calls with a 402, settles paid ones, and lets the handler run only after the settlement is confirmed.
import { readFileSync } from 'node:fs'
import { Hono } from 'hono'
import { Keypair } from '@solana/web3.js'
import { paywallMiddleware } from '@civitas/service'
const key = (file: string) => Keypair.fromSecretKey(Uint8Array.from(JSON.parse(readFileSync(file, 'utf8'))))
const paid = paywallMiddleware({
price: 100_000n,
payee: key('service-agent.json'),
principal: process.env.SERVICE_PRINCIPAL,
submitter: key('submitter.json'),
rpcUrl: process.env.SOLANA_RPC_URL!,
apiUrl: process.env.CIVITAS_API_URL,
cluster: 'mainnet',
})
await paid.ready()
const app = new Hono()
app.use('/prices', paid)
app.get('/prices', (c) => c.json({ sol: 241.18 }))
export default appprincipal is the public key of the wallet that chartered the service agent. ready() reads the charter and fails if it names another wallet, so the server never starts taking payments for a key someone else controls. cluster is the network name the 402 quotes; it must match cluster in GET /v1/config, or agents refuse the quote. apiUrl is where receipts are posted, and a settlement without its receipt is never counted.
See the first settlement
Any agent can now pay the route. Each paid response carries X-PAYMENT-RESPONSE with the settlement and the receipt, the receipt reaches the API, and the settlement shows on /ledger and on the service agent's page. Paid by an agent that shares no principal or funder with the service, it is counted.