Cursor pagination with Moralis API
For paginating API results you can use cursor parameter. You get cursor from current request and you use it for the next request until there are no more results returned.
Example of using cursor in NodeJS with a Moralis Server:
import Moralis from 'moralis';
import { EvmChain } from '@moralisweb3/common-evm-utils';
const chain = EvmChain.ETHEREUM;
const address = '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB'; //Cryptopunks contract address
await Moralis.start({
apiKey: 'YOUR_API_KEY',
// ...and any other configuration
});
let cursor = null;
let owners = {};
do {
const response = await Moralis.EvmApi.nft.getNFTOwners({
address,
chain,
limit: 100,
cursor: cursor,
});
console.log(console.log(
`Got page ${response.page} of ${Math.ceil(
response.total / response.page_size
)}, ${response.total} total`
);
for (const owner of response.result) {
owners[owner.owner_of] = {
amount: owner.amount,
owner: owner.owner_of,
tokenId: owner.token_id,
tokenAddress: owner.token_address,
};
}
cursor = response.cursor;
} while (cursor != "" && cursor != null);
console.log("owners:", owners, "total owners:", Object.keys(owners).length);
}
โ
Example of using cursor parameter in Python:
import requests
import time
def get_nft_owners(offset, cursor):
print("offset", offset)
url = 'https://deep-index.moralis.io/api/v2/nft/<address_here>/owners?chain=polygon&format=decimal'
if cursor:
url = url + "&cursor=%s" % cursor
print("api_url", url)
headers = {
"Content-Type": "application/json",
"X-API-Key": "API_KEY_HERE"
}
statusResponse = requests.request("GET", url, headers=headers)
data = statusResponse.json()
print("HTTP headers:", statusResponse.headers)
try:
print("nr results", len(data['result']))
except:
print(repr(data))
print("exiting")
raise SystemExit
cursor = data['cursor']
print(data['page'], data['total'])
return cursor
cursor = None
for j in range(0, 10):
cursor = get_nft_owners(j*500, cursor)
print()
time.sleep(1.1)
LIMIT VALUE FOR CURSOR
The limit param value for cursor can be set at page 1 and the same limit value will be used through the following pages, you cannot change the limit value after page 1.