3

How do I get all the NFTs (ERC-721 & 1155) and Tokens (ERC-20) for a wallet address? Is there an API for this?

Irwing Tello

2
  • If you are affiliated with QuickNode you should add a disclaimer to the question. It isn't against SE policies to ask and answer the question yourself, but you should add a disclaimer for your affiliation.
    – Ismael
    Jul 8, 2023 at 22:31
  • Hi @Ismael, I am not affiliated with QuickNode, I marked QuickNode's answer as valid because it was the one that solved my problem quickly and it was what I was looking for Jul 25, 2023 at 20:20

2 Answers 2

1

You can get this data using the balanceOf function of the token smart contracts. But doing this for each token and NFT is not a nice experience.

Using a solution like QuickNode, which provides two solutions for this would be way easier.

The First option is using QuickNode custom RPCs

To use this, enable the Token and NFT API bundle Add-on on your QuickNode Ethereum endpoint.

Here's an example in ethersv6:

const ethers = require("ethers");
(async () => {
  const provider = new ethers.JsonRpcProvider("QUICKNODE_RPC_URL");
  const walletAddress = "ADDRESS_TO_BE_QUERIED";
  const coins = await provider.send("qn_getWalletTokenBalance", [
    {
      wallet: walletAddress,
    },
  ]);

  const nfts = await provider.send("qn_fetchNFTs", [
    {
      wallet: walletAddress,
      page: 1,
      perPage: 10,
    },
  ]);

  console.log(coins,nfts);
})();

The second option is QuickNode Graph API

By using the following GraphQL query:

query Query($address: String!) {
  ethereum {
    walletByAddress(address: $address) {
      tokenBalances {
        edges {
          node {
            contract {
              address
              decimals
              name
            }
            totalBalance
          }
        }
      }
      walletNFTs {
        edges {
          node {
            nft {
              externalUrl
              name
              metadata
            }
          }
        }
      }
    }
  }
}

With the wallet address as a variable:

{
  "address": "WALLET_ADDRESS_GOES_HERE"
}
2

You can get all the information you are looking for using Moralis API.

Here is an example of getting all the NFTs using moralis.

const Moralis = require("moralis").default;

const runApp = async () => {
  const { EvmChain } = Moralis.EvmUtils;

  await Moralis.start({
    apiKey: "YOUR_API_KEY",
    // ...and any other configuration
  });

  const address = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";

  const chain = EvmChain.ETHEREUM;

  const response = await Moralis.EvmApi.nft.getWalletNFTs({
    address,
    chain,
  });

  console.log(response.raw);
};

runApp();

And this is what the results will look like.

    {
      "token_address": "0xc84ac7bf8b4cec60d82fb93dfb61b03d5e4acd39",
      "token_id": "1953",
      "amount": "1",
      "owner_of": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
      "token_hash": "28d043249272e630cf598fe80b789491",
      "block_number_minted": "17481420",
      "block_number": "17482230",
      "contract_type": "ERC721",
      "name": "BoringApes",
      "symbol": "BORING",
      "token_uri": "https://ipfs.moralis.io:2053/ipfs/QmSapUjjycSnYXg2RS8HopNQX1pkDzYoQnECGwjPeASMZp/1953.json",
      "metadata": null,
      "last_token_uri_sync": "2023-06-14T23:25:30.517Z",
      "last_metadata_sync": "2023-06-14T23:26:17.905Z",
      "minter_address": null,
      "possible_spam": false
    },
    {
      "token_address": "0x4b59cc984b5ad15515d19d65aa39bd3e1dc98d59",
      "token_id": "7",
      "amount": "1",
      "owner_of": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
      "token_hash": "592d4b5d222b9c926df5c1fe3aa131e0",
      "block_number_minted": "17476789",
      "block_number": "17481819",
      "contract_type": "ERC1155",
      "name": "Uniswap Airdrop Recipient",
      "symbol": "Noox Distribution Protocol",
      "token_uri": "https://ipfs.moralis.io:2053/ipfs/QmXTawe96xK99kL3mA7RrzUmAuWT15QY3BTsi4WgH1SDWT/7",
      "metadata": "{\"name\":\"Noox : Uniswap Airdrop Receipent\",\"description\":\"Owning this badge indicates that the user has received uniswap airdrop. Each badges are non-transferable NFTs. For more information, visit https://www.noox.fund\",\"external_url\":\" https://www.noox.fund\",\"image\":\"ipfs://bafybeifftr7cbh6rkssevgvprvwea32b6asw7m4tthxirkouj5fn6fvsce\",\"attributes\":[{\"trait_type\":\"category\",\"value\":\"DeFi\"},{\"trait_type\":\"website\",\"value\":\"noox.fund\"}]}",
      "last_token_uri_sync": "2023-06-14T07:46:29.024Z",
      "last_metadata_sync": "2023-06-19T12:56:45.947Z",
      "minter_address": "ERC1155 tokens don't have a single minter",
      "possible_spam": false
    },

You can find a guide here: How to get all the NFTs owned by an address

For the ERC20 Tokens Balance, you can use the following example

const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");

const runApp = async () => {
  await Moralis.start({
    apiKey: "YOUR_API_KEY",
    // ...and any other configuration
  });

  const address = "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d";

  const chain = EvmChain.ETHEREUM;

  const response = await Moralis.EvmApi.token.getWalletTokenBalances({
    address,
    chain,
  });

  console.log(response.raw);
};

runApp();

And this is the expected result

  {
    "token_address": "0x709b5a3c7a22250981869a8080a354de919f821e",
    "symbol": "DMON",
    "name": "DogeMon",
    "logo": null,
    "thumbnail": null,
    "decimals": 9,
    "balance": "500000000000000",
    "possible_spam": true
  },
  {
    "token_address": "0x2df495dfc4a24064acc573e9ec9f5bd509b42e40",
    "symbol": "GAMEBOY",
    "name": "Nintendo Gameboy",
    "logo": null,
    "thumbnail": null,
    "decimals": 18,
    "balance": "1500000000000000000000000000",
    "possible_spam": true
  },
  {
    "token_address": "0xb2ea9527bf05bc3b73320a1ec18bd4f2fe88d952",
    "symbol": "OP",
    "name": "Optimism",
    "logo": null,
    "thumbnail": null,
    "decimals": 18,
    "balance": "767387143779415173800484933",
    "possible_spam": true
  },

You can find a guide for erc 20 token balance here: How to get all ERC20 tokens owned by an address

Disclosure: I work at Moralis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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