trustline-agent-sdk is the Python interface an AI agent uses to take and repay revenue-underwritten credit on TrustLine. It is a faithful port of the JavaScript/TypeScript SDK — same lifecycle, same on-chain contracts, same backend underwriter. The agent holds its own Stellar key; on-chain writes (register/borrow/repay/deposit) are signed by that key. Reads (credit_line/vault_state) are simulate-only. Scoring/underwriting is delegated to the TrustLine backend (the trusted underwriter, v1).
Install
trustline module and depends on stellar-sdk and requests.
Construct an agent
The full loop at a glance
Function reference
public_key()
register()
score_registry (one-time, idempotent on-chain). A write, signed and submitted by the agent’s own key. Returns a TxResult (tx_hash, return_value, explorer_url).
underwrite(skip_proof=False, from_ledger=None)
score_registry. This is the call that determines your credit limit and APR. Returns the backend’s full underwriting result (score, tier, limit, APR, per-payer independence breakdown, zkTLS result if attempted).
skip_proof=True— skip the slow (~20–90s) zkTLS proof step; score on on-chain revenue only.from_ledger— explicit ledger to start indexing from, for history older than the RPC’s ~24h window.
onboard(skip_proof=False, from_ledger=None)
register() then underwrite(). Returns {"register": TxResult, "underwrite": <result>}. Almost always what you want for a brand-new agent.
credit_line()
credit_line contract (a free simulated read). Returns a CreditTerms dataclass:
available_credit_usdc()
limit − outstanding principal, read live from the vault. Check this before borrow() to avoid a failed transaction. Returns 0.0 if nothing is available.
vault_state()
usdc_balance_usdc()
balance read) — the actual wallet balance, not the credit line.
revenue(from_ledger=None)
underwrite().
borrow(usdc)
usdc against your credit line into your own wallet. A write, signed by your key. Raises TxError if the vault rejects the draw (e.g. over your limit); raises ValidationError locally for a non-positive amount before any network call.
repay(usdc)
usdc — interest first (funding the first-loss reserve and lender yield), then principal. A write, signed by your key. On-time repayment ramps your limit up on subsequent underwrite() calls.
deposit(agent_address, usdc)
usdc of liquidity into agent_address’s isolated vault, exposed only to that one agent’s default risk. Any keypair can act as borrower or lender.
pay_with_credit(url, price_usdc, ...)
requests.Response.
price_usdc— the resource price in USDC (you know what you’re buying; the SDK doesn’t parse the x402 challenge for you).max_draw— optional cap on how much credit a single call may draw. If the shortfall would exceed it, raisesMaxDrawExceededError.method/headers/data/json_body— forwarded to the actual paid request (e.g.method="POST", json_body={...}).
Errors
All SDK errors extendTrustLineError, so you can catch specific failures instead of string-matching:
Exported helpers & constants
The pure helpersto_stroops, from_stroops, is_valid_stellar_address, and credit_shortfall_usdc are exported for reuse, along with the constants TESTNET_PASSPHRASE and TESTNET_RPC.
A complete, runnable example
Seepackages/agent-sdk-py/examples/quickstart.py for a full script that funds a fresh testnet agent and runs the register → underwrite → borrow → repay loop end to end.
Build with Claude — the TrustLine skill
If you use Claude Code, there’s an officialtrustline-agent-sdk skill that teaches Claude to drive this SDK (both Python and JS/TS): the full lifecycle, draw-on-402, error handling, and the common pitfalls. Install it any of these ways:
/reload) and invoke it with /trustline-agent-sdk. If you clone the TrustLine repo, the skill is already present (it lives in .claude/skills/) — no install needed.