Skip to main content

Overview

@billionsnetwork/x402-human-proof-client is the client SDK for agents calling services that require per-request payments and human-backed verification powered by the x402 human proof protocol. It enables clients to:
  • Sign CAIP-122 (SIWE) challenges embedded in 402 responses so the server can verify the agent is operated by a real human.
  • Check held attestations against the Billions Network registry before attempting a payment, so attestation-gated (discounted) pricing tiers are selected only when the client qualifies.
  • Fall back gracefully when attestations are missing (MissingAttestationsError) or when a per-human usage cap is hit (isMaxUseExceededError + disqualify()).
The SDK integrates with the x402 client lifecycle as a registered extension. It is framework-agnostic and compatible with any HTTP client library supported by x402.
Just want your agent to access human-proof protected APIs? The Verified Agent Identity skill is built on top of this SDK — install it and your agent is ready without any code changes. If your agent hasn’t installed the skill yet, the paywall HTML returned by the server already includes instructions to do so, so pre-installation is not required.

How It Works

x402 Human Proof request lifecycle
  • The selector runs synchronously at payment creation time; attestation state is cached and refreshed via refresh().
  • The extension re-checks attestations live just before signing — the selector cache and the live check serve different purposes: tier selection vs. proof construction.
  • If the server returns max_use_exceeded, call disqualify() on the selector and retry — it will skip discounted tiers permanently for that instance.

Installation

Requires Node.js >= 20.11.0 and an x402-compatible HTTP client. This reference matches version 0.1.7. The agent’s DID must hold an ownership attestation to use discounted pricing tiers.
Attestation checks use the production Attestations API by default, which is where pairings made with the Verified Agent Identity skill live. To test against testnet pairings, pass attestationsApiBaseUrl: "https://attestations-api-testnet.billions.network/api/v1/attestations" to createAttestationAwareSelector and createHumanProofExtension. See Environments.

Quick Start

A runnable client for an agent you build yourself. In a new folder:
Add a .env with the agent’s key. Use a dedicated key, never one that holds funds you care about:
Save this as client.ts. It uses top-level await, which is why the project is set to "type": "module":
Run it against a local server, such as the Server SDK Complete Example:
With a key whose DID has no ownership attestation, you’ll see Missing attestations: with the required schema ID, followed by Failed to create payment payload: Missing required attestations. That confirms the gate works. With a paired DID, the client signs the human proof and pays at the discounted tier. With a funded Base Sepolia wallet, the request succeeds and the script prints { weather: 'sunny', temperature: 70 }.

The Human-Proof Challenge

When the server is configured with @billionsnetwork/x402-human-proof-server, its 402 responses include an extensions["human-proof"] object:
createHumanProofExtension handles signing and proof construction automatically when registered with the x402 client. CAIP-122 defines chain-agnostic sign-in messages. The SDK signs this challenge in its Ethereum form, Sign-In with Ethereum (eip191). The fields are listed under CAIP-122 Signed Messages in the Server SDK. signHumanProofChallengeEVM formats and signs it, and createHumanProofExtension calls it for you. Use it directly only if you need lower-level control.

Configuration Reference

createHumanProofExtension(signer, options?)

Register this extension on your x402 client to handle challenge signing automatically. On each payment attempt it extracts the human-proof challenge from the 402 body, verifies the wallet holds all required attestations, and injects the signed proof into the payment payload.
Parameters: HumanProofExtensionOptions: If no challenge is present in the 402 body the payload is returned unchanged. Otherwise it checks each required attestation schema (throwing MissingAttestationsError if any are missing), signs the SIWE message with the first eip191 chain, and returns the payload with extensions["human-proof"] set.
Compatibility: the EVMSigner interface is intentionally minimal. It is compatible with viem WalletClient, viem LocalAccount (privateKeyToAccount), and ethers v6 Signer.

createAttestationAwareSelector(did, schemas?, options?)

Creates a selector + refresh + disqualify triple for use with new x402Client(selector). The selector picks the most favorable accepts entry based on cached attestation state.
Parameters: Returns:

buildDIDFromAddress(ethAddress)

Derives the Billions Network DID from an EVM wallet address.
Returns: string — the full DID string using the did:iden3:billions:main: method.

checkAttestation(did, schemaId, options?)

Queries the Billions Network attestations API to check whether a DID holds at least one attestation matching a given schema ID.
Returns: Promise<boolean> — true if at least one matching attestation record exists. Revoked attestations are not counted. It also returns false when the API responds with an error status, so false can mean “not paired” or “API unavailable”. To tell them apart, call the Attestations API directly.
checkAttestation is a lookup, not proof of control. It answers whether a DID holds an attestation, not whether the caller controls that DID. On the client that’s fine, because you pass your own DID. A server must not call it with a DID an agent reported about itself: see Agent Verification.

Low-Level Helpers

signHumanProofChallengeEVM(challenge, signer, chainId)

Lower-level function that signs a CAIP-122 challenge for an EVM chain and returns the JSON proof string. createHumanProofExtension calls this internally.
Returns: Promise<string> — a JSON-encoded string containing all CAIP-122 fields plus address, signature, chainId, and type. Throws: Error if chainId is not found in challenge.supportedChains with type eip191.

extractHumanProofChallenge(paymentRequired)

Extracts the human-proof extension object from a 402 response body. Returns null if no challenge is present — safe to call on any paymentRequired response.
Returns: HumanProofChallenge | null

Error Handling

MissingAttestationsError

Thrown by createHumanProofExtension when the wallet doesn’t hold one or more required attestations.
Expect this when one or more schemas in challenge.info.requiredAttestations are not held by the DID — either the user hasn’t completed Billions Network registration yet, or the DID was derived from a different wallet than the one that holds the attestation.

isMaxUseExceededError

Returns true when an HTTP error response has status 402 and the payment-required header contains error: "max_use_exceeded".
Returns boolean. Compatible with axios, fetch, node-fetch, and similar.

Environment Variables

The SDK itself reads no environment variables. The variables below are used by example code. The Quick Start above uses EVM_PRIVATE_KEY. The example scripts in the SDK repository use all three: