Skip to main content

Python SDK

The official Python client for the Legend Prime API. Async-first, built on httpx.

Installation

pip install legend-prime
For signing transactions, you’ll also need eth-account:
pip install eth-account

Quick start

from legend_prime import LegendPrime

async with LegendPrime(
    query_key="qk_abc123_8f2e9a7b4c1d6e3f5a0b",
) as client:
    # Create a sub-account
    account = await client.accounts.create(
        signer_type="eoa",
        signer_address="0x742d35Cc6634C0532925a3b844Bc9e7595f2bD18",
    )

    # List all accounts
    result = await client.accounts.list()
    accounts = result["accounts"]

    # Get portfolio
    folio = await client.accounts.folio(account["external_id"])

Client configuration

client = LegendPrime(
    query_key="qk_...",                          # Required: your query key
    base_url="https://custom.url",               # Optional: override the API base URL
)
The client supports async context manager for automatic cleanup:
async with LegendPrime(query_key="qk_...") as client:
    # client is automatically closed when the block exits
    ...
Or manage the lifecycle manually:
client = LegendPrime(query_key="qk_...")
# ... use client ...
await client.close()

Accounts

# Create
account = await client.accounts.create(
    signer_type="eoa",
    signer_address="0x...",
)

# List all
result = await client.accounts.list()

# Get one
account = await client.accounts.get("acc_2o0yiljtp378")

# Get portfolio
folio = await client.accounts.folio("acc_2o0yiljtp378")
cached_folio = await client.accounts.folio("acc_2o0yiljtp378", cached=True)

# Activities
result = await client.accounts.activities("acc_2o0yiljtp378")
activity = await client.accounts.activity("acc_2o0yiljtp378", 1)

# Events (with long-polling)
result = await client.accounts.events("acc_2o0yiljtp378", since=0, poll=True)

Plans

# Create plans
earn_plan = await client.plan.earn(
    "acc_2o0yiljtp378",
    amount="1000000",
    asset="USDC",
    network="base",
    protocol="compound",
)

withdraw_plan = await client.plan.withdraw(
    "acc_2o0yiljtp378",
    amount="500000",
    asset="USDC",
    network="base",
    protocol="compound",
)

transfer_plan = await client.plan.transfer(
    "acc_2o0yiljtp378",
    amount="1000000",
    asset="USDC",
    network="base",
    recipient="0x...",
)

# Sign and execute
from eth_account import Account

signer = Account.from_key(private_key)
digest = earn_plan["chart"]["eip712_data"]["digest"]
sig = signer.unsafe_sign_hash(bytes.fromhex(digest[2:]))
signature = "0x" + sig.signature.hex()

result = await client.plan.execute(
    "acc_2o0yiljtp378",
    plan_id=earn_plan["plan_id"],
    signature=signature,
)

Reference data

# Get your Prime Account info
me = await client.prime_account()

# List supported networks
result = await client.networks()

# List supported assets
result = await client.assets()

Error handling

from legend_prime import LegendPrime, LegendPrimeError

try:
    await client.accounts.get("acc_nonexistent")
except LegendPrimeError as e:
    print(e.code)     # "account_not_found"
    print(e.message)  # "Account not found"
    print(e.status)   # 404