API 参考

查看 searchAssets 的详细文档

快速开始

TypeScript
// Replace YOUR_API_KEY with your Helius API key
const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`;

async function searchAssets(params) {
  const body = {
    jsonrpc: "2.0",
    id: "search-assets-example",
    method: "searchAssets",
    params,
  };
  const res = await fetch(url, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });
  if (!res.ok) {
    throw new Error(`${res.status} ${res.statusText}`);
  }
  const { result } = await res.json();
  return result;
}

// Example: fetch first 50 compressed NFTs in a wallet
searchAssets({
  ownerAddress: "86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY",
  tokenType: "compressedNft",
  limit: 50,
}).then(console.log);
page 参数从 1 开始。为了实现极快的无限滚动,请使用游标分页(before/after)。

核心概念

1. 选择 tokenType

tokenType
string
required
一个必需的字符串,指定您希望在响应中获得哪种资产
可接受的值:fungiblenonFungibleregularNftcompressedNftall
tokenType您得到的内容典型用例
fungible仅限 SPL 和 Token-22 代币钱包余额、代币门控
nonFungible所有 NFT(压缩常规)投资组合概览
regularNft传统和 pNFT(未压缩)市场列表
compressedNft仅限 cNFT超低成本大规模铸造
all所有内容(代币NFT)全面发现

2. 分页和排序

Solana 钱包可以拥有数千个资产——高效的分页很重要。
  • Page / Limit – 经典分页(page1 开始)。适用于静态视图。
  • Cursor – 传递上一个响应中的 beforeafter 值,以实现快速的无限滚动。
"sortBy": {
  "sortBy": "created",      // created | recent_action | updated | none
  "sortDirection": "desc"   // asc | desc
}
有关 page/limit 和基于游标的策略的完整代码示例,请访问专门的分页指南

3. 显示选项 (options)

这些标志添加元数据;它们从不改变返回的资产。
标志效果
showNativeBalance包含钱包的 SOL 余额
showCollectionMetadata添加集合级别的 JSON 数据
showGrandTotal返回总匹配计数(较慢)
showInscription附加铭文和 SPL-20 信息(实验性)
"options": {
  "showNativeBalance": true,
  "showCollectionMetadata": true,
  "showGrandTotal": true,
  "showInscription": true
}

代码示例

搜索钱包中的所有可替代代币

const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`

const searchAssets = async () => {
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-id',
      method: 'searchAssets',
      params: {
        ownerAddress: '86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY',
        tokenType: 'fungible',
      },
    }),
  });
  const { result } = await response.json();
  console.log("Search Assets: ", result);
};
searchAssets();

搜索所有可替代代币(显示本机余额和代币信息)

const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`

const searchAssetsTokenInfo = async () => {
    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            jsonrpc: '2.0',
            id: 'my-id',
            method: 'searchAssets',
            params: {
                ownerAddress: '86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY',
                tokenType: 'fungible',
                displayOptions: {
                    showNativeBalance: true,
                },
            },
        }),
    });

    const { result } = await response.json();

    result.items.forEach(item => {
      console.log(item.token_info);
    });

    console.log("Native Balance: ", result.nativeBalance);
};

searchAssetsTokenInfo();

搜索 vibhu.sol 拥有的 Drip NFT

const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`

const searchAssetsDrip = async () => {
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-id',
      method: 'searchAssets',
      params: {
        ownerAddress: 'BAVjpySHMMGDpq3whU7qaqCCRE8ksCeGwxa53Qv2i8jS',
        grouping: ["collection", "DRiP2Pn2K6fuMLKQmt5rZWyHiUZ6WK3GChEySUpHSS4x"],
        page: 1,
        limit: 1000
      },
    }),
  });
  const { result } = await response.json();
  console.log("Drip Haus Assets: ", result);
};
searchAssetsDrip();

搜索钱包中的压缩资产

const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`

const searchAssetsCompressed = async () => {
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-id',
      method: 'searchAssets',
      params: {
        ownerAddress: '2k5AXX4guW9XwRQ1AKCpAuUqgWDpQpwFfpVFh3hnm2Ha',
        compressed: true,
      },
    }),
  });
  const { result } = await response.json();
  console.log("Search Assets: ", result);
};
searchAssetsCompressed();

搜索铭文和 SPL-20 数据

const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`

const searchAssetsInscriptions = async () => {
    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            jsonrpc: '2.0',
            id: 'my-id',
            method: 'searchAssets',
            params: {
                ownerAddress: '6GmTFg5SCs4zGfDEidUAJjS5pSrXEPwW8Rpfs3RHrbc5',
                tokenType: 'regularNft',
                displayOptions: {
                    showInscription: true,
                },
            },
        }),
    });

    const { result } = await response.json();
    console.log(result.items.map((i) => [i.id, i.inscription, i.spl20]));
};
searchAssetsInscriptions();