[RESOLVED] Efficient check if user owns token id using API

Is there a more direct way of asking the NFT api if a user address owns a specific token id? You can use:

  1. getNFTs() to get all the NFTs owned by an address then do linear search potentially across multiple pages and requests

  2. getTokenIdOwners(): which will only be 1 for ERC721 but could be thousands for ERC1155

Ideally you’d be able to further filter getTokenIdOwners with a user address. Is this possible? (and not documented)

Otherwise you’re left with queries that return more data than needed. I’d imagine checking ownership of a specific token is a common use case- especially useful for authentication and authorization- and worthy of a direct solution in the API.

With the current functions:

  • For ERC721: getTokenIdOwners() is optimal as there can be only 1 owner
  • For ERC1155 it’s a toss up
    • If the user doesn’t have many NFTs then getNFTs() could be less results, but it’s a gamble
    • If you have foreknowledge of the number of minted 1155 tokens for that id and the number is low then getTokenIdOwners() could be the better choice
  • The most simple solution I suppose is just to use getNFTs() and hope they don’t have that many

There is a hidden feature where you can use token_id parameter (not available in documentation).

You can check this forum post for examples: [SOLVED] getNFTsForContract(options) with token_id not working anymore

Thanks for the reply. It doesn’t appear that getNFTs supports the token_id param. When I call the endpoint directly I still get the full set of results:

GET https://deep-index.moralis.io/api/v2/0x3f2b7EEa68F90E1c1FAF602D8535b6239fD512AC/nft?chain=eth&token_id=1772930

https://etherscan.io/address/0x3f2b7eea68f90e1c1faf602d8535b6239fd512ac#tokentxnsErc721

You will also have to know the token address in that particular case

1 Like

Yah that was a silly mistake but it still returns the whole result set even with both the token_address and token_id params:

can you paste that info? address, token_address, token_id

try token_addresses instead of token_address

if still doesn’t work, try the endpoint specific to getNFTsForContract

https://deep-index.moralis.io/api/v2/0x3f2b7EEa68F90E1c1FAF602D8535b6239fD512AC/nft?chain=eth&token_address=0x06012c8cf97BEaD5deAe237070F9587f8E7A266d&token_id=1390773

I tried token_addresses instead and it returns 22 results instead of 24 with token_address

Will try getNFTsForContract

OK getNFTsForContract() does work and I suppose since it’s an “account” endpoint it’s also filtering by user address so this would work for ERC1155s as well. Will do some more testing. Thanks!

1 Like