Reference

@civitas/agent

Every export of the agent SDK, its options, what the fetch does on a 402, and every error it throws.

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:

install
npm install ./civitas-contract-0.2.0.tgz ./civitas-program-0.2.0.tgz ./civitas-agent-0.2.0.tgz

Exports

createAgentFetch

ts
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

ts
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

ts
interface AgentFetchOptions {
    agent: Keypair | Uint8Array;
    maxPerCall?: bigint;
    fetch?: typeof globalThis.fetch;
    api?: string;
    rpcUrl?: string;
}

What createAgentFetch takes.

AgentFetchOptions
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 OverPerCallLimit and nothing is signed. The on-chain policy still applies.
fetchtypeof globalThis.fetch
The fetch used 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/config and the starting nonce from GET /v1/agents/{key}.
rpcUrlstring
A Solana RPC URL, used when api is not set: the fee terms and the starting nonce are read from the Config and Agent accounts at confirmed.

AgentResponse

ts
interface AgentResponse extends Response {
    readonly payment?: PaymentResponse;
}

A Response with the payment outcome on it when the call was paid.

AgentResponse
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

  1. Reads the Civitas offer from the 402 body. With no Civitas offer, the 402 is returned as it came.
  2. 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).
  3. Checks the quoted program, mint and fee terms against the API or the chain (InvalidPaymentRequired).
  4. Computes the fee from those terms and refuses a quote whose extra.fee differs (FeeMismatch), then refuses an outflow above maxPerCall (OverPerCallLimit).
  5. Takes the next nonce: the first call starts from the agent’s highest settled nonce plus one, each later call adds one.
  6. Signs the authorization and retries the request once with X-PAYMENT, never following a redirect.
  7. 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.

nameWhen
InvalidPaymentRequiredThe 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.
InvalidPaymentResponseA paid response came back without a valid X-PAYMENT-RESPONSE, or its receipt doesn’t verify for this call.
OverPerCallLimitPrice plus fee is above maxPerCall, so nothing was signed. The service can also report it from the program.
AuthorizationExpiredThe quote expired before the agent signed. The service can also report it.
FeeMismatchThe 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.
AgentNotFoundThe API or the chain has no charter for this agent key.
NonceExhaustedThe agent has used the highest u64 nonce.
PaymentRefusedThe 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.