A fetch for a chartered, funded agent. On a Civitas 402 it checks the quote, signs one authorization with the agent key and retries the request once. Every other response, other payment schemes included, comes back untouched.
Version 0.2.0. Install it with the two packages it depends on:
npm install ./civitas-contract-0.2.0.tgz ./civitas-program-0.2.0.tgz ./civitas-agent-0.2.0.tgzExports
createAgentFetch
function createAgentFetch(options: AgentFetchOptions): (input: RequestInfo | URL, init?: RequestInit) => Promise<AgentResponse>;Makes the paying fetch for one agent key. It takes the same arguments as fetch and returns an AgentResponse. Make one per agent key and share it across concurrent calls, since it hands out the nonces.
AgentPaymentError
class AgentPaymentError extends Error {
constructor(name: string, message: string);
}Thrown for a refused or invalid payment. Branch on error.name; the names are under Errors.
AgentFetchOptions
interface AgentFetchOptions {
agent: Keypair | Uint8Array;
maxPerCall?: bigint;
fetch?: typeof globalThis.fetch;
api?: string;
rpcUrl?: string;
}What createAgentFetch takes.
agentKeypair | Uint8Arrayrequired- The agent key: a
Keypair, a 32-byte Ed25519 seed or a 64-byte Solana secret key. maxPerCallbigint- Most the agent will pay for one call, amount and fee together, in base units. Checked before signing; above it the call fails with
OverPerCallLimitand nothing is signed. The on-chain policy still applies. fetchtypeof globalThis.fetch- The
fetchused for the service and the API. Defaults to the global one. apistring- The Civitas API’s base URL. When set, the fee terms come from
GET /v1/configand the starting nonce fromGET /v1/agents/{key}. rpcUrlstring- A Solana RPC URL, used when
apiis not set: the fee terms and the starting nonce are read from the Config and Agent accounts atconfirmed.
AgentResponse
interface AgentResponse extends Response {
readonly payment?: PaymentResponse;
}A Response with the payment outcome on it when the call was paid.
paymentPaymentResponse- The service’s PaymentResponse: the settlement’s transaction signature and the signed receipt, both already checked. Absent on a call that wasn’t paid.
On a 402
- Reads the Civitas offer from the 402 body. With no Civitas offer, the 402 is returned as it came.
- Refuses a quote for another URL or another program, a redirect to another origin, and a quote that expires more than
min(maxTimeoutSeconds, 300)seconds ahead (InvalidPaymentRequired). - Checks the quoted program, mint and fee terms against the API or the chain (
InvalidPaymentRequired). - Computes the fee from those terms and refuses a quote whose
extra.feediffers (FeeMismatch), then refuses an outflow abovemaxPerCall(OverPerCallLimit). - Takes the next nonce: the first call starts from the agent’s highest settled nonce plus one, each later call adds one.
- Signs the authorization and retries the request once with
X-PAYMENT, never following a redirect. - Checks the
X-PAYMENT-RESPONSE: success, payer, payee, amount, call ID, and the receipt’s signature against the payee key (InvalidPaymentResponse).
Errors
AgentPaymentError carries one of these as name, with a sentence as message. A service’s HTTP error that isn’t a payment refusal comes back as the response, with its status and body, and network errors propagate as they are.
name | When |
|---|---|
InvalidPaymentRequired | The 402 was malformed, named another resource or program, quoted a mint or fee terms that differ from Civitas’s config, or set its expiry further ahead than the agent will sign for. |
InvalidPaymentResponse | A paid response came back without a valid X-PAYMENT-RESPONSE, or its receipt doesn’t verify for this call. |
OverPerCallLimit | Price plus fee is above maxPerCall, so nothing was signed. The service can also report it from the program. |
AuthorizationExpired | The quote expired before the agent signed. The service can also report it. |
FeeMismatch | The quote’s extra.fee isn’t the fee Civitas’s terms give for its price, so nothing was signed. The service can also report it from the program. |
AgentNotFound | The API or the chain has no charter for this agent key. |
NonceExhausted | The agent has used the highest u64 nonce. |
PaymentRefused | The service refused the payment with a reason that isn’t a program error. The message carries its reason. |
Any program error the service reports comes back under its own name: OverPeriodCap, AgentFrozen, AgentRevoked, PolicyExpired, PayeeNotAllowed, NonceReplayed, BadSignature, FeeMismatch.