Suppose, I have ERC721 NFT collection. And I sell it via my website.

When a user comes, logs in via MetaMask, how will I be able to retrieve all of the NFTs of my collection that he owns? Will I at all?

Without the likes of Moralis, Alchemy. Only in pure ethers.js

2 Answers 2


In order to do it without using the likes of Moralis. You need:

  1. An RPC node
  2. The contract address
  3. The wallet address
  4. The contract ABI

Then you can write a function like this:

async function getNFTsOfOwner() {
  const contract = new ethers.Contract(contractAddress, erc721ABI, provider);
  const balance = await contract.balanceOf(ownerAddress); //Returns the number of tokens owned by the address

  let tokenIds = [];

  for(let i = 0; i < balance.toNumber(); i++) {
    const tokenId = await contract.tokenOfOwnerByIndex(ownerAddress, i); //Get the token ID based on the index from the balanceOf call

  console.log(`Token IDs owned by ${ownerAddress}: ${tokenIds}`);

At the end of this, you will have all token IDs in the tokenIds array. If you want the metadata for the tokens you're going to need to call the tokenURI() function for each token ID, call the URI and retrieve the metadata for each token.

It's quite a lot of work but definitely doable. If you do change your mind here is how you would do it with the Moralis getWalletNFTs endpoint.

const response = await Moralis.EvmApi.nft.getWalletNFTs({
    "chain": "0x1",
    "format": "decimal",
    "tokenAddresses": [
    "mediaItems": false,
    "address": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"

This would give you all NFTs with all the metadata, attributes and images directly in the response.

Disclaimer, I work at Moralis.

  • Ok, I'll use Alchemy.com then
    – DankenN
    Jul 7, 2023 at 21:23

ethers.js simply queries the calling contract, and only gets the status of a known contract in a certain block. The token type owned by an account must be provided by etherscan or a third-party query to record your transaction records.

  • What "token type"?
    – DankenN
    Jul 3, 2023 at 15:09
  • But how does your post answer my question?
    – DankenN
    Jul 5, 2023 at 3:03

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.