> ## Documentation Index
> Fetch the complete documentation index at: https://docs.legend.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Introduction

> Programmatic access to DeFi yield, transfers, and portfolio management

# Legend API

The Legend API gives you programmatic access to Legend's DeFi infrastructure. Create accounts, earn yield, transfer assets, and monitor portfolios — all through a simple REST API.

## What you can do

* **Manage accounts** — Provision sub-accounts with on-chain smart wallets. Hold assets on your own behalf, run an omnibus account for your users, or let end-users own their own keys.
* **Earn yield** — Deploy assets into DeFi protocols like Compound, Morpho, and Aave
* **Swap assets** — Trade tokens via DEX aggregation with best-price routing
* **Loop** — Open and manage looped long positions on Morpho markets
* **Borrow & repay** — Access lending protocols with collateral management
* **Transfer assets** — Move funds between wallets and across networks
* **Migrate positions** — Move supply positions between protocols atomically
* **Re-invest rewards** — Auto-reinvest protocol rewards
* **Monitor portfolios** — View balances, track activities, and poll for real-time updates

## How it works

You tell Legend **what** you want to do and **where** — Legend figures out the rest. Every plan endpoint takes a `network` parameter for the target network. Legend handles bridging funds across chains, covering gas, and submitting transactions.

You never need to bridge manually or check which chain holds the funds. Just specify the action and the target network — this applies to earn, swap, transfer, and every other action that requires funding. For example, to **re-invest rewards**, Legend will claim rewards on-chain, swap them for the target asset, bridge them to the target chain, and re-supply to the yield protocol — all from a single plan.

Legend uses a **plan-sign-execute** model. Your server creates a plan (e.g., "earn 1000 USDC on Compound"), the account's signer approves it with an EIP-712 signature, and Legend executes the on-chain transactions.

```mermaid theme={null}
sequenceDiagram
    participant S as Your Server
    participant L as Legend
    participant B as Blockchain
    S->>L: POST /plan/earn
    L-->>S: plan_id + EIP-712 digest
    Note over S: Sign digest locally
    S->>L: POST /plan/execute { signature }
    L->>B: Submit transaction
    L-->>S: { status: executing }
    B-->>L: Confirmation
    S->>L: GET /activities
    L-->>S: { status: confirmed }
```

## Architecture

Every integration starts with a **Prime Account** — your organization's container. Under it, you create **sub-accounts**, each with its own on-chain smart wallet (Quark Wallet) and signer key.

```mermaid theme={null}
graph TD
    PA[Prime Account]
    QK[Query Keys]
    SA1[Sub-Account A]
    SA2[Sub-Account B]
    SA3[Sub-Account C]
    W1[Quark Wallet]
    W2[Quark Wallet]
    W3[Quark Wallet]

    PA --> QK
    PA --> SA1
    PA --> SA2
    PA --> SA3
    SA1 -->|EOA| W1
    SA2 -->|EOA via Privy| W2
    SA3 -->|Turnkey P256| W3
```

## Non-custodial

Legend is non-custodial — we never hold signing keys. Every fund movement requires a cryptographic signature from the sub-account's signer, and Legend cannot produce these signatures.

You can use Legend with your own key provider (Privy, Turnkey, Ledger, etc), or you can use a Legend-managed (non-custodial) signer key provided by Turnkey. In either case, you will be in complete control of the funds and are "in the loop" for all transactions.

Each sub-account has a signer, controlled by either you or your end-user. Legend supports two signer types:

| Type             | Description                                                                                             |
| ---------------- | ------------------------------------------------------------------------------------------------------- |
| **EOA**          | Standard Ethereum private key (secp256k1). Can optionally be secured via services like Privy or Ledger. |
| **Turnkey P256** | Key managed via Turnkey with email/social recovery.                                                     |

Query keys grant API access — creating accounts, viewing portfolios, creating plans. But **moving funds always requires the signer's approval**.

<Note>
  Funds on Ethereum are held in Legend's smart wallet contracts (sometimes referred to as "quark wallets"). Thus, when funding your account, you should fund the smart wallet address, not the EOA address.
</Note>

## Next steps

<CardGroup cols={2}>
  <Card title="Quickstart" icon="rocket" href="/quickstart">
    Go from zero to earning yield in 5 minutes
  </Card>

  <Card title="Authentication" icon="key" href="/authentication">
    Set up your query key or OAuth login
  </Card>

  <Card title="Agent Integration" icon="robot" href="/agents/overview">
    Connect AI agents via MCP or CLI
  </Card>

  <Card title="CLI Reference" icon="terminal" href="/agents/cli">
    Install the legend-cli binary
  </Card>

  <Card title="TypeScript SDK" icon="js" href="/sdks/typescript">
    Install the TypeScript client
  </Card>

  <Card title="Python SDK" icon="python" href="/sdks/python">
    Install the Python client
  </Card>
</CardGroup>
