# Working with NFTs

{% hint style="warning" %}
Make sure to implement the [prerequisite code](https://connect.socios.com/interact-with-chiliz-chain/prerequisites), or else the examples in this page will not work!
{% endhint %}

## Prerequisite: erc721ABI JSON file

{% hint style="success" %}
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.&#x20;

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).
{% endhint %}

{% file src="<https://31329255-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMDcaoJLMj5Y3jzFMTONF%2Fuploads%2FawzJaTFYrRkttoSUNgGZ%2FERC721Abi.json?alt=media&token=8de7dc1f-655b-448d-b66c-a6c43322107a>" %}

## 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](https://api-docs.nansen.ai/reference/get_address-portfolio-1).

{% code lineNumbers="true" fullWidth="true" %}

```javascript
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));
```

{% endcode %}

## Retrieve user NFT details

Once you know the NFT's smart-contract address (using the code above), you can get all its metadata:

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```javascript
import erc721ABI from './ERC721ABI.json';

const contractAddress = '0xYourNFTAddress'; // Replace with the actual contract address

async function getNFTMetadata(tokenId) {
    const tokenURI = await client.readContract({
        address: contractAddress, // Contract address of the NFT
        abi: erc721ABI,
        functionName: 'tokenURI', // Standard ERC-721 function to get metadata uri
        args: [tokenId], // User's address whose balance you want to check
    });

    const metadataResponse = await fetch(tokenURI);
    const metadata = await metadataResponse.json();
    console.log(metadata);
}

// Example usage: Get metadata for a specific token (e.g., token ID 1)
getNFTMetadata(1);
```

{% endcode %}

## 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.

{% code lineNumbers="true" fullWidth="true" %}

```javascript
import erc721ABI from './ERC721ABI.json';

async function getNFTBalance() {
  const balance = await client.readContract({
    address: '0xYourNFTAddress', // Replace with the actual NFT contract address
    abi: erc721ABI,
    functionName: 'balanceOf', // Standard ERC-721 function to get balance
    args: ['0xUserWalletAddress'], // User's address whose NFT balance you want to check
  });
  return balance.toString();
}

// Example usage
getNFTBalance().then(console.log);
```

{% endcode %}
