Sui.

Bài viết

Chia sẻ kiến thức của bạn.

McMMoKing.
Jan 25, 2025
Hỏi đáp Chuyên Gia

How to query more than 50 NFTs using getObjects API?

I'm trying to retrieve a large number of NFTs from a blockchain address but ran into a limit in the getObjects API, which restricts the number of objects I can fetch at a time to 50. What's the correct way to handle this limitation and obtain more results? Is there any specific method or example code for handling pagination?

  • Sui
  • Architecture
3
7
Chia sẻ
Bình luận
.

Câu trả lời

7
mssoni.
Jan 25 2025, 08:07

Để vượt qua giới hạn 50 đối tượng trong API GetObjects, bạn nên sử dụng phân trang. Điều này liên quan đến việc sử dụng con trỏ do API cung cấp để tìm nạp tập hợp kết quả tiếp theo. Bắt đầu bằng cách thực hiện lệnh gọi API ban đầu, sau đó kiểm tra phản hồi để biết chi tiết phân trang như 'nextCursor' và 'hasNextPage'. Sử dụng 'nextCursor' làm tham số trong các lệnh gọi API tiếp theo của bạn cho đến khi 'hasNextPage' là false, cho biết không còn đối tượng nào để tìm nạp nữa.

3
Bình luận
.
Tawhid.
Jan 25 2025, 12:14

Để thực hiện, hãy tham khảo tài liệu API Sui về phân trang và truy vấn đối tượng. Điều này thường liên quan đến việc thực hiện một loạt các yêu cầu bằng con trỏ cho đến khi bạn truy xuất tất cả dữ liệu mong muốn. Mỗi phản hồi sẽ cung cấp một con trỏ cho tập hợp các đối tượng tiếp theo, cho phép bạn xử lý các bộ dữ liệu lớn hơn một cách hiệu quả.

2
Bình luận
.
believer.
Sep 7 2025, 12:00

You're right, getObjects is limited and usually used when you already have the specific object IDs. For retrieving objects owned by an address, especially when you need more than 50, you should use the queryObjects API instead. This API supports pagination using a cursor. You'll make an initial call, and if hasNextPage is true, you'll receive a nextCursor which you then pass into your subsequent calls to fetch the next batch of results. You keep doing this in a loop until hasNextPage is false, indicating you've retrieved all available objects for that address. You can also filter by object type to specifically target your NFTs.

2
Bình luận
.
Vhekee.
Aug 27 2025, 00:31

To query more than 50 NFTs using the getObjects API, you'll need to use pagination. Here's a step-by-step approach to handle this limitation:

Pagination with getObjects API

  1. Initial Request: Make an initial request to the getObjects API with the desired filter and limit (up to 50 objects).
  2. Check for Next Page: Check if the response contains a nextCursor field. If it does, it means there are more objects available.
  3. Fetch Next Page: Use the nextCursor value as the cursor parameter in the next request to fetch the next page of objects.
  4. Repeat Until No Next Page: Continue fetching pages until the response no longer contains a nextCursor field, indicating that you've retrieved all available objects.

Example Code Snippet Here's an example code snippet demonstrating pagination with the getObjects API: import { SuiClient } from '@mysten/sui.js/client';

const suiClient = new SuiClient(); const ownerAddress = '0x...'; // Replace with the owner's address const typeFilter = { Type: '0x2::devnet_nft::DevNetNFT' }; // Replace with the desired type filter

async function getNFTs() { let hasNextPage = true; let cursor = null; const nfts = [];

while (hasNextPage) { const response = await suiClient.getOwnedObjects({ owner: ownerAddress, filter: typeFilter, options: { showType: true, showContent: true, }, limit: 50, cursor, });

nfts.push(...response.data);

if (response.hasNextPage) {
  cursor = response.nextCursor;
} else {
  hasNextPage = false;
}

}

return nfts; }

getNFTs().then((nfts) => console.log(nfts)); Additional Resources

  • Sui Documentation: Refer to the official Sui documentation for more information on pagination and using the getObjects API.
  • Sui Client: Explore the Sui Client documentation for details on implementing pagination and handling large datasets [1].

By using pagination, you can efficiently retrieve a large number of NFTs from a blockchain address while handling the getObjects API's limit.

1
Bình luận
.
dhaholar.
Aug 26 2025, 22:52

When you want to fetch more than 50 NFTs using the getObjects API in Sui, you need to handle pagination manually since the API limits each request to 50 objects. To work around this, you can use the cursor parameter to keep track of where you left off and make multiple requests until you've retrieved all the data. Each response includes a nextCursor value, which you pass into the next request to continue fetching from that point. This way, you can loop through all the NFTs in batches of 50 until there are no more pages left. Here's a basic example in JavaScript using the Sui SDK:

import { SuiClient } from '@mysten/sui.js/client';

const client = new SuiClient({ url: 'https://fullnode.mainnet.sui.io' });

async function fetchAllNFTs(address) {
  let hasNextPage = true;
  let cursor = null;
  const allObjects = [];

  while (hasNextPage) {
    const response = await client.getOwnedObjects({
      owner: address,
      cursor: cursor,
      limit: 50,
    });

    allObjects.push(...response.data);
    cursor = response.nextCursor;
    hasNextPage = response.hasNextPage;
  }

  return allObjects;
}

This loop keeps calling the API until all NFTs are collected. You can read more about how pagination works in the Sui API documentation.

0
Bình luận
.

Bạn có biết câu trả lời không?

Hãy đăng nhập và chia sẻ nó.