0

I'm trying to count the assets at a blockfrost api endpoint. It only shows 100 items on a page though. How do you loop through all the pages and extract the asset count? Thanks for any help.

const Http = new XMLHttpRequest(),
        method = "GET",
        url = 'https://cardano-mainnet.blockfrost.io/api/v0/accounts/stake1';
    
    const PAGE_SIZE = 100 /* 100 is the max Blockfrost page size */
    const stake_address = 'stake1'; 

    Http.open(method, url, true);
    Http.setRequestHeader('project_id', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    Http.send();
    Http.onreadystatechange = (e) => {
        if (e.currentTarget.readyState == 4 && e.currentTarget.status == 200 && e.currentTarget.responseText != '') {
async function count_assets() {
let total = 0;
try {
                let total = 0;
                let assets = [];

do {
    assets = Http.responseText; 
    total += assets.length;
    page++;
    } while (assets.length == page);
} catch (err) {
    console.log('error', err);
}
return total;
}

count_assets(stake_address).then(assets => {
console.log(stake_address, 'assets:', assets);
});
            }}

1 Answer 1

0

You probably want to use the "Assets associated with the account addresses" endpoint. You can add query params to the request as per specification.

E.g. to get 100 items on page 3: /accounts/{stake_address}/addresses/assets?count=100&page=3

Now simply create a loop that increments the page query param until you don't get 100 items in the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.