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.