Skip to main content
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).
New to TrustLine? Read the onboarding kit first — it walks through funding a testnet account (the same prerequisites apply regardless of SDK language).

Install

The package installs as the trustline module and depends on stellar-sdk and requests.

Construct an agent

The full loop at a glance

Function reference

public_key()

Returns this agent’s Stellar public address. No network call.

register()

Registers the agent in the on-chain 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)

Runs a full underwriting pass on the backend: indexes real revenue, checks counterparty independence, optionally verifies an off-chain zkTLS proof, computes a score, signs it, and publishes it to 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).
Don’t borrow against the raw limitUsdc in the result — that’s the tier ceiling, not what a cold agent can actually draw. The real drawable amount is ramped (see How the credit engine works §4.3). Always check available_credit_usdc() or credit_line() for the live number.
  • 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)

Convenience wrapper: register() then underwrite(). Returns {"register": TxResult, "underwrite": <result>}. Almost always what you want for a brand-new agent.

credit_line()

Reads your current, live, on-chain credit terms directly from the credit_line contract (a free simulated read). Returns a CreditTerms dataclass:

available_credit_usdc()

Your remaining drawable credit right now — 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()

Full isolated-vault accounting for this agent:

usdc_balance_usdc()

Your agent’s spendable USDC balance (a live SAC balance read) — the actual wallet balance, not the credit line.

revenue(from_ledger=None)

A cheap, read-only live index of your on-chain x402 revenue — check what the underwriter will see before running a full underwrite().

borrow(usdc)

Draws 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)

Repays 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)

The lender action, not the borrower action — the caller (this keypair) supplies 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, ...)

Draw-on-402 — the flagship convenience method. Pays for an x402-priced resource, automatically borrowing any shortfall between your current USDC balance and the price before making the request. The agent never “decides to borrow” — it just transacts, and the credit line silently covers what its cash can’t. Returns the final 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, raises MaxDrawExceededError.
  • method / headers / data / json_body — forwarded to the actual paid request (e.g. method="POST", json_body={...}).

Errors

All SDK errors extend TrustLineError, so you can catch specific failures instead of string-matching:

Exported helpers & constants

The pure helpers to_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

See packages/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 official trustline-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:
Then restart Claude Code (or run /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.