Working with NFTs
Make sure to implement the prerequisite code, or else the examples in this page will not work!
Prerequisite: erc721ABI JSON file
When working with NFT, and for some use-cases described in this page (the ones that import erc721ABI), you will need the below ERC721Abi.json file saved in your project folder.
Make sure that you import that file in your code in order to achieve the wanted step (the samples already have the necessary import code).
Retrieve user NFT list
To retrieve all NFTs held in user wallet, you need a third-party tool or API.
In this example, we use Nansen API's GET /address/portfolio endpoint.
const options = {method: 'GET', headers: {accept: 'application/json'}};
fetch('https://api.nansen.ai/v1/address/portfolio', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));Retrieve user NFT details
Once you know the NFT's smart-contract address (using the code above), you can get all its metadata:
Retrieve user NFT balance
This code will allow you to check how many NFTs from a specific collection a user holds.
It is much lighted than the "Retrieve user NFT list" example that uses Nansen shown above.
Last updated