Skip to main content
aifinpay-agent is a non-custodial x402 payment client for autonomous AI agents. It is the Python surface of the same payment infrastructure that powers the rest of AiFinPay — Stripe for AI agents. Your agent generates an Ed25519 keypair locally, funds it on-chain, and then agent.pay(url) handles the entire HTTP 402 handshake for you: detect the facilitator, sign, settle, retry.
The keypair is generated inside your process and never transmitted. The server only ever sees a signature, never your secret key. AiFinPay is token-free — there is nothing to buy, stake, or trade. mSECCO usage credits are non-transferable and exist purely for on-chain accounting.

Install

The import package is aifinpay:

Your first paid call

1

Create an agent identity

Agent.new() generates a fresh Ed25519 keypair locally. Print the address to fund it, and persist secret_b58 if you want to reuse this identity later.
2

Fund the wallet, then wait for it on-chain

Send a few cents of value to agent.address, then block until the funding is visible on-chain. min_usd_cents is the threshold in US cents.
This polls the public leaderboard until your address shows up with enough reserved, and raises FundingTimeoutError if it never does (default timeout 600s).
3

Pay a gated endpoint

agent.pay(url) sends the request, and on an HTTP 402 it auto-detects the facilitator, builds the right auth payload, settles, and retries — returning a standard requests.Response.

Paying any x402 endpoint

agent.pay() works against any supported x402 facilitator, not just AiFinPay’s own endpoints. Extra keyword arguments (method, json, headers, …) are forwarded to the underlying requests call.
Two convenience wrappers pin the verb for you:

pay() parameters

Controlling cost with PayOptions

If a 402 challenge demands more than max_amount_usd, pay() raises PaymentTooExpensiveError before anything is signed or settled — your funds are never spent above the cap.

Loading an existing keypair

Reuse a persisted identity instead of generating a new one each run:
Both accept the same keyword arguments as Agent.new() (e.g. base_url, timeout).

The AiFinPay-native Seat flow

For AiFinPay’s own Seat-based flow you can request an invoice directly. The SDK is non-custodial, so it returns the on-chain instructions and does not submit the transaction — you build and sign it with the chain SDK of your choice.
asset accepts "SOL" (routes to /api/invoice) or an SPL asset such as "USDC" / "USDT" (routes to /api/invoice-spl).
How the handshake works. On a 402 the SDK reads the x-nonce from the challenge, computes SHA-256("AiFinPay-x402:{nonce}:{pubkey}"), signs it with Ed25519, and sets three headers on the retry: x-agent-pubkey, x-nonce, x-signature. Nonces are consumed on use, so the auth is replay-resistant.

Errors

All exceptions subclass AiFinPayError and are importable from aifinpay:

Advanced: paying registered providers across chains

For a chain-opaque surface that selects and settles a payment per call, use AiFinPayAgent. One identity derives both a Solana base58 address and a Polygon EVM address; call(provider=…) looks the provider up in the registry, settles the 402 on-chain, and returns the upstream response. Install the unified extras (adds the EVM and Solana signing dependencies):
call() accepts provider, body, method (default "POST"), chain ("polygon" | "solana", defaults to the provider’s preferred chain), cost (a budget cap), and timeout. To reuse an identity, construct it from a seed or an existing Solana secret:
Same identity, both SDKs: a seed produces byte-for-byte the same Solana and EVM addresses in the Python and Node SDKs, so an agent can move between runtimes without changing wallets.