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

# 快速开始

> 在 5 分钟内开始使用 LI.FI Earn。列出金库、获取金库详情、查看用户持仓，并通过 Composer 存入。

本快速开始将带你了解 Earn 的核心流程：通过 Earn Data API 发现金库、查看用户持仓，然后通过 Composer 执行存入。

## 前置条件

* **curl** 或 **Node.js 18+**（用于 `fetch`）
* 一个来自 [LI.FI Partner Portal](https://li.fi/plans/) 的 **API key**，通过 `x-lifi-api-key` 请求头传递
* 数据调用（步骤 1 到 4）无需钱包。仅存入（步骤 5）需要钱包。

***

## 1. 列出可用金库

获取按 APY 排序、过滤到特定链的金库：

<CodeGroup>
  ```bash curl theme={"system"}
  curl -X GET 'https://earn.li.fi/v1/vaults?chainId=8453&sortBy=apy&limit=5' \
    --header 'x-lifi-api-key: YOUR_API_KEY'
  ```

  ```ts TypeScript theme={"system"}
  const response = await fetch(
    'https://earn.li.fi/v1/vaults?chainId=8453&sortBy=apy&limit=5',
    { headers: { 'x-lifi-api-key': 'YOUR_API_KEY' } }
  );
  const { data, nextCursor, total } = await response.json();

  console.log(`Found ${total} vaults on Base`);
  data.forEach((vault) => {
    console.log(`${vault.name}: ${(vault.analytics.apy.total * 100).toFixed(2)}% APY`);
  });
  ```
</CodeGroup>

响应包含一个 [NormalizedVault](/earn/how-it-works#the-normalizedvault-schema) 对象数组，附带完整的元数据、分析数据和交易能力。

***

## 2. 获取单个金库

按 chain ID 和合约地址获取特定金库的完整详情：

<CodeGroup>
  ```bash curl theme={"system"}
  curl -X GET 'https://earn.li.fi/v1/vaults/8453/0x7BfA7C4f149E7415b73bdeDfe609237e29CBF34A' \
    --header 'x-lifi-api-key: YOUR_API_KEY'
  ```

  ```ts TypeScript theme={"system"}
  const response = await fetch(
    'https://earn.li.fi/v1/vaults/8453/0x7BfA7C4f149E7415b73bdeDfe609237e29CBF34A'
  );
  const vault = await response.json();

  console.log(`${vault.name} on ${vault.network}`);
  console.log(`APY: ${(vault.analytics.apy.total * 100).toFixed(2)}%`);
  console.log(`TVL: $${vault.analytics.tvl.usd}`);
  console.log(`Depositable: ${vault.isTransactional}`);
  ```
</CodeGroup>

<Tip>
  使用 `GET /v1/chains` 和 `GET /v1/protocols` 构建动态过滤 UI，仅显示当前拥有金库的链和协议。这两个端点均返回轻量级列表，可在每个会话中获取一次。
</Tip>

***

## 3. 查看用户持仓

查询用户在所有支持协议中的 DeFi 持仓：

<CodeGroup>
  ```bash curl theme={"system"}
  curl -X GET 'https://earn.li.fi/v1/portfolio/0xYOUR_WALLET_ADDRESS/positions' \
    --header 'x-lifi-api-key: YOUR_API_KEY'
  ```

  ```ts TypeScript theme={"system"}
  const response = await fetch(
    'https://earn.li.fi/v1/portfolio/0xYOUR_WALLET_ADDRESS/positions'
  );
  const { positions } = await response.json();

  positions.forEach((pos) => {
    console.log(`${pos.asset.symbol} on ${pos.protocolName}: $${pos.balanceUsd}`);
  });
  ```
</CodeGroup>

示例响应：

```json theme={"system"}
{
  "positions": [
    {
      "chainId": 1,
      "address": "0xa17581a9e3356d9a858b789d68b4d866e593ae94",
      "protocolName": "aave-v3",
      "asset": {
        "address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
        "name": "USD Coin",
        "symbol": "USDC",
        "decimals": 6
      },
      "balanceUsd": "1523.45",
      "balanceNative": "1523450000"
    }
  ]
}
```

***

## 4. 通过 Composer 存入

一旦找到金库，将其合约地址用作 `toToken`，并借助 [Composer](/composer/overview) 执行存入。Composer 在单笔交易中处理交换、桥接（如涉及跨链）和存入。

<CodeGroup>
  ```bash curl theme={"system"}
  # Deposit 1 USDC into a Morpho vault on Base
  curl -X GET 'https://li.quest/v1/quote?fromChain=8453&toChain=8453&fromToken=0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913&toToken=0x7BfA7C4f149E7415b73bdeDfe609237e29CBF34A&fromAddress=0xYOUR_WALLET_ADDRESS&toAddress=0xYOUR_WALLET_ADDRESS&fromAmount=1000000'
  ```

  ```ts TypeScript theme={"system"}
  // Use the vault's contract address as toToken
  const vault = data[0]; // from step 1

  const quote = await fetch(
    `https://li.quest/v1/quote?` +
    `fromChain=${vault.chainId}` +
    `&toChain=${vault.chainId}` +
    `&fromToken=0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913` + // USDC on Base
    `&toToken=${vault.address}` +
    `&fromAddress=0xYOUR_WALLET_ADDRESS` +
    `&toAddress=0xYOUR_WALLET_ADDRESS` +
    `&fromAmount=1000000` // 1 USDC
  ).then((r) => r.json());

  // quote.transactionRequest is ready to sign and send
  console.log('Transaction:', quote.transactionRequest);
  ```
</CodeGroup>

<Note>
  存入步骤使用的是 **Composer**（`li.quest`），而非 Earn Data API。有关执行交易的完整详情，请参阅 [Composer API 集成指南](/composer/guides/api-integration)。
</Note>

***

## 后续步骤

<CardGroup cols={2}>
  <Card title="API 集成指南" icon="code" href="/earn/guides/api-integration">
    包含所有参数和响应示例的完整端点参考
  </Card>

  <Card title="发现与存入配方" icon="book" href="/earn/recipes/discover-and-deposit">
    端到端配方：为某个代币找到最佳金库，然后存入
  </Card>
</CardGroup>
