Skip to main content
Version: v1.3

Get NFTs of a Given Collection

This guide will show you how to get the L2 NFTs of a given collection owned by an account using the ISCAccounts.getL2NFTsInCollection function from the ISCAccounts magic contract.

Mint an NFT

Mint your first NFT following our how to mint an NFT guide.

Implement the getL2NFTsInCollection Function

Here’s a sample implementation to retrieve L2 NFTs within a specific collection owned by an account:

function getNFTsInCollection(NFTID memory collectionId) public view returns (NFTID[] memory) {
// Get the ISCAgentID
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();

// Call the getL2NFTsInCollection function from the ISCAccounts contract
NFTID[] memory nftsInCollection = ISC.accounts.getL2NFTsInCollection(agentID, collectionId);

return nftsInCollection;
}

Full Example Contract

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import "@iota/iscmagic/ISCAccounts.sol";
import "@iota/iscmagic/ISCSandbox.sol";

contract MyNFTContract {

function getNFTsInCollection(NFTID memory collectionId) public view returns (NFTID[] memory) {
// Get the ISCAgentID
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();

// Call the getL2NFTsInCollection function from the ISCAccounts contract
NFTID[] memory nftsInCollection = ISC.accounts.getL2NFTsInCollection(agentID, collectionId);

return nftsInCollection;
}
}