# Working with NFTs

{% hint style="warning" %}
Make sure to implement the [prerequisite code](/interact-with-chiliz-chain/prerequisites.md), 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="/files/06Qy8xKMqR0mnvrZXqru" %}

## 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://connect.socios.com/interact-with-chiliz-chain/working-with-nfts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
