0

I'm wanting to display the NFT count of a wallet on my website. I've gone through the blockfrost SDK and I don't see anything that relates to NFTs. Is there a method to return this value?

Here is my code which currently returns the ADA amount:

        const Http = new XMLHttpRequest();        
        const url = 'https://cardano-mainnet.blockfrost.io/api/v0/accounts/stake1u9r0sh6hl6csxt90k5ueh5pl404r6qs995kjnzmp8u3mr2csgg2q8';
        Http.open("GET", url);
        Http.setRequestHeader('project_id', 'mainnetxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
        Http.send();
        Http.onreadystatechange = (e) => {
            if (e.currentTarget.readyState == 4 && e.currentTarget.status == 200 && e.currentTarget.responseText != '') {
                var stake = (parseFloat(JSON.parse(Http.responseText).controlled_amount) / 1000000);
                console.log(stake)
            }
        }

Thanks for any help!

1
  • I recommend not sharing your project id publicly since it serves as API key so anyone can make requests in the name of your account using this id. May 14 at 8:16

1 Answer 1

0

You'll probably need two steps for this.

First, fetch all assets at the stake address using this endpoint:

https://cardano-mainnet.blockfrost.io/api/v0/accounts/{stake_address}/addresses/assets

(documented here: https://docs.blockfrost.io/#tag/Cardano-Accounts/paths/~1accounts~1{stake_address}~1addresses~1assets/get)

Then filter all but those with a quantity of "1" (technically all tokens are identical, but NFTs should only exist with a quantity of "1").

Just counting these could already be enough for a rough counter on a website.

But the next step would be to check that only one of each of those token is in existance and the metadata suggests it is actually an NFT.

So you can look up each of those assets using this endpoint:

https://cardano-mainnet.blockfrost.io/api/v0/assets/{asset}

(documented here: https://docs.blockfrost.io/#tag/Cardano-Assets/paths/~1assets~1{asset}/get)

If this lookup also shows a quantity of "1" exists and the "onchain_metadata" is populated (with fields from this NFT metadata standard https://cips.cardano.org/cips/cip25/) you could assume it's an NFT, and then just count them up.

2
  • Thanks Sean, I'll check out those links then. In order to parse that data and filter all but those with a quantity of "1", can you tell me what the data type is?
    – Murray
    May 15 at 6:04
  • It's a string as in: { "unit": "b0d07d45fe9514f80213f4020e5a61241458be626841cde717cb38a76e7574636f696e", "quantity": "125" }
    – sean
    May 15 at 11:54

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.