Skip to main content

fianza-agent-sdk (Python)

Python SDK for AI agents to take and repay revenue-underwritten, uncollateralized USDC credit on Fianza (Stellar / Soroban).

An agent proves real revenue → Fianza underwrites it → the agent gets a credit line it can draw against and repay, all from its own Stellar key. This is the Python port of the TypeScript @fianza/agent-sdk; the two speak the same backend API and on-chain contracts.

PyPI

pip install fianza-agent-sdk

Quickstart

from stellar_sdk import Keypair
from fianza import FianzaAgent

# An agent holds its own Stellar key.
tl = FianzaAgent(
    Keypair.random().secret,
    api_base_url="https://trustline-rpxt.onrender.com",
)

tl.register()                      # one-time, on-chain
result = tl.underwrite(skip_proof=True)   # backend scores + publishes on-chain
print(result["score"])

terms = tl.credit_line()           # on-chain read (simulate-only)
print(terms)                       # CreditTerms(tier=..., limit_usdc=..., apr_bps=...)

if terms.limit_usdc > 0:
    tl.borrow(1)                   # draw 1 USDC against the line
    # ...do paid work...
    tl.repay(1)                    # interest first (lender yield), then principal

A full runnable walkthrough (fresh keypair → Friendbot → faucet → register → underwrite → borrow → repay) lives in examples/quickstart.py.

API

Construct with the agent's secret; contract ids are auto-resolved from the backend /config unless you pass them explicitly.

FianzaAgent(
    secret,
    *,
    rpc_url=None,               # Soroban RPC (default: testnet)
    network_passphrase=None,    # default: testnet
    api_base_url=None,          # Fianza backend (default: http://localhost:8787)
    contracts=None,             # {"registry","creditLine","vault"} — else from /config
)

Underwriting (delegated to the backend)

method what it does
revenue(from_ledger=None) live x402 revenue index for this agent
underwrite(skip_proof=False, from_ledger=None) full pass: revenue → proof → score → publish
onboard(skip_proof=False, from_ledger=None) register() then underwrite()

On-chain reads (simulate-only)

method returns
credit_line() CreditTerms(tier, limit_usdc, apr_bps)
vault_state() VaultState(liquidity_usdc, principal_usdc, amount_owed_usdc, total_assets_usdc, yield_pool_usdc, limit_usdc, apr_bps)
available_credit_usdc() remaining drawable credit (USDC)
usdc_balance_usdc() this agent's spendable USDC

On-chain writes (signed by the agent's key)

method what it does
register() register in the score registry (one-time)
borrow(usdc) draw against the credit line
repay(usdc) repay (interest → lender yield, then principal)
deposit(agent_address, usdc) LP: supply liquidity into another agent's isolated vault

Each returns a TxResult(tx_hash, return_value, explorer_url).

Draw-on-402

resp = tl.pay_with_credit(url, price_usdc, max_draw=None, method="GET")

Pay for an x402-priced resource, auto-drawing any shortfall from the credit line first. The agent never "decides to borrow" — it just transacts, and the line silently covers what its cash can't. Returns a requests.Response. Pass json_body=..., data=..., and headers=... for POST bodies. This implements the x402 exact-Stellar scheme (SEP-41 transfer, facilitator-sponsored) directly, so no extra dependency is required.

Errors

All SDK errors subclass FianzaError:

  • ValidationError — bad input (amount, address).
  • ApiError — backend returned non-2xx (.status, .method, .path, .body).
  • TxError — an on-chain tx failed to simulate/submit/confirm (.contract_method, .detail).
  • MaxDrawExceededError — pay_with_credit would draw past max_draw (.need, .max_draw).

Pure helpers

to_stroops, from_stroops (USDC ↔ 7-decimal stroops), is_valid_stellar_address, and credit_shortfall_usdc (the draw-on-402 math) are exported and unit-tested.

Development

pip install -e ".[dev]"
pytest

License

MIT

Metadata

Release files for fianza-agent-sdk 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fianza-agent-sdk 0.2.1
File Size Uploaded Provenance Actions
fianza_agent_sdk-0.2.1.tar.gz 15.3 kB Without provenance Details for fianza_agent_sdk-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fianza-agent-sdk 0.2.1
File Interpreter ABI Platform Actions
fianza_agent_sdk-0.2.1-py3-none-any.whl Python 3 none any Details for fianza_agent_sdk-0.2.1-py3-none-any.whl

Total release size: 30.1 kB

Release files / fianza_agent_sdk-0.2.1.tar.gz

Download URL fianza_agent_sdk-0.2.1.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0bfcd5c3d4edd26ac3efdf0a0bd1bb6861022c2fb3ebc6ce6f997785862b2a8d
BLAKE2b-256 checksum
How to use checksums
0d19a1fc13bfe57e98a83b7e4235c6bede8413e5ab82db7d7a8cc733f155a7b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / fianza_agent_sdk-0.2.1-py3-none-any.whl

Download URL fianza_agent_sdk-0.2.1-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a61f0a0815396b3f1c012f87c190f1a3ba11a68fb469e45cff4df00acf56b70
BLAKE2b-256 checksum
How to use checksums
9cacf8c8fbd50dadac81c2e0bd64d22ca31ad43f186482f7366eaf5b8265a545
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for fianza-agent-sdk 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page