Query For NFTs of a Contract With Some Condition?

Hi, suppose my contract for a erc721 nft is deployed on polygon mainnet at the address ā€œ0x123ā€.

Say that in the attributes for each NFT there is a item named ā€œsomeFlagā€ that can have a value of either true or falseā€¦

Now, I want to use Moralis API to do an API call for all NFTs in that collection where the flag is set to true.

Is this possible with the Moralis API? What API call can accomplish this?

Thanks!

If that information is available in metadata then you can try to use search nfts:
https://docs.moralis.io/moralis-dapp/web3-api/token#searchnfts

You can test it here https://admin.moralis.io/web3Api with addresses parameter

@cryptokid thank you for telling me to go rtfm. :+1:

I donā€™t see how it is possible to do the search that I want with this endpointā€¦

For example, this code will always return every NFT in the collection. not what I want

const options = { q: "someFlag", chain: "matic", filter: "metadata" };

const NFTs = await Moralis.Web3API.token.searchNFTs(options);

Can Moralis only handle very simple text search queries?

Is what I want to do not possible? @ivan

try to use the endpoint /nft/search from this interface: https://admin.moralis.io/web3Api
where you can test it directly in that interface, you will see addresses parameter there

1 Like

SearchNFTs will work for your use case.

const options = { addresses: ["0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d"], q: "Tuxedo Tee", filter: "attributes" }
(https://deep-index.moralis.io/api/v2/nft/search?chain=eth&format=decimal&q=Tuxedo%20Tee&filter=attributes&addresses=0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d)

If thereā€™s ā€œtrueā€ or ā€œfalseā€ anywhere else in attributes that arenā€™t related, it will pick these up as well. And for a larger collection, the query will take some time.

It probably would be better to sync the token data to your database and query from there.

@glad your post makes zero sense on so many levelsā€¦

  1. why are you querying for ā€œTuxedo Teeā€? wtf? this has nothing to do with my original question.

  2. no clue what you mean by ā€œIf thereā€™s ā€œtrueā€ or ā€œfalseā€ anywhere else in attributesā€

  3. As I said before, this query doesnā€™t do what it want- it just returns all the NFTs.

  4. LMAO so you are saying Moralis is pointless and we should all just rebuild it on our own? thx :joy:

I tried this, but it returns all NFTs.

Can you give an example?

I donā€™t this it is possible to do this search on Moralisā€¦

Hi, suppose my contract for a erc721 nft is deployed on polygon mainnet at the address ā€œ0x123ā€.

  1. It was just an example, just like your hypothetical, except the code I posted is against a real contract so you can actually test it.

  2. If thereā€™s more than one ā€œtrueā€ or ā€œfalseā€ found in say the attributes (if this is where someFlag will be), then it will count it in the results even if itā€™s not for the property that you want, e.g. you want all someFlag: true but the bottom will be included as well:

attributes: [
  "someFlag": false,
  "somethingElse": true
]
  1. You can use the example I posted, this searches for a query of ā€œTuxedo Teeā€ inside metadata attributes, filtered with the contract address of the Bored Ape Yacht Club collection. It does work somewhat with a few caveats.

  2. No, Iā€™m saying that you could sync the NFT data to your Moralis server and query it from there instead of needing to constantly call the API. Another alternative is to just filter the results out yourself after getting all the collection data. But donā€™t worry about this anymore.

  1. As I said before, this query doesnā€™t do what it want- it just returns all the NFTs.

What is an example of a query you have tried?

I posted an example you can try above from the Web3 API page.