Skip to main content

Python SDK for AI agents to take and repay revenue-underwritten credit on TrustLine (Stellar). Settles in USDC.

Project description

trustline-agent-sdk (Python)

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

An agent proves real revenue → TrustLine 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 @trustline-agents/agent-sdk; the two speak the same backend API and on-chain contracts.

pip install trustline-agent-sdk

Quickstart

from stellar_sdk import Keypair
from trustline import TrustLineAgent

# An agent holds its own Stellar key.
tl = TrustLineAgent(
    Keypair.random().secret,
    api_base_url="https://trustline.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.

TrustLineAgent(
    secret,
    *,
    rpc_url=None,               # Soroban RPC (default: testnet)
    network_passphrase=None,    # default: testnet
    api_base_url=None,          # TrustLine 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 TrustLineError:

  • 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).
  • MaxDrawExceededErrorpay_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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trustline_agent_sdk-0.2.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trustline_agent_sdk-0.2.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file trustline_agent_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: trustline_agent_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for trustline_agent_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 042616bd936bd7df60db6d317db3b9678e7cab827db2dd23cf12be07f5a3b00d
MD5 90c1f4ab64d1ae25244a4d76552e1c46
BLAKE2b-256 fae4bf3ad5f4e373aeff31213928b7c6b2c84362486d52e538ce01684258606a

See more details on using hashes here.

File details

Details for the file trustline_agent_sdk-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for trustline_agent_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f06309908448295ac0c2a1a316234b703d38564ede26022d1e3eecb84cb5915d
MD5 5e90760aad730879b65580fb30165cd1
BLAKE2b-256 691a9ec0d6973a4874bd2da11ae60da7c52d40faaa1f9db6e5424defb0ca05f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page