To fetch NFTs from a contract and current them to customers is a fundamental but important activity builders should know tips on how to do. With the next endpoint from Moralis, a developer can simply get all NFTs from contract:
const response = await Moralis.EvmApi.nft.getContractNFTs({
With the endpoint above, you’ll get non-fungible tokens for a given contract tackle, together with metadata for all accessible NFTs. In the event you’re an skilled dev desirous to get going immediately, first ensure that to enroll with Moralis, and you may then go to the documentation web page and get going immediately! Nonetheless, if you wish to see a step-by-step technique of implementing and finishing your entire file for the above code snippet, learn on!

Overview
Over the last bull market, quite a few NFT-related tasks emerged, together with NFT-gated web sites, marketplaces, and many others. Now, there’s an growing variety of use circumstances and numerous utility options builders have found for these tokens. As such, now’s the most effective time to learn to simply construct all kinds of decentralized purposes (dapps) round these sorts of tokens. With that mentioned, an ideal place to begin is a neat NFT explorer – a dapp that allows you to get all NFTs from contract. Whereas this activity would possibly sound difficult, it’s fairly a simple course of whenever you use the correct instruments.
Because of Moralis’ final NFT API and its “getContractNFTs” endpoint, you possibly can have a totally purposeful NFT explorer up and operating in minutes, which is precisely what you’ll study within the ultimate stage of at present’s article. Nonetheless, earlier than we will concentrate on displaying you tips on how to get all NFTs from contract the simple means, we have to cowl some fundamentals. Thus, we’ll first guarantee you already know what NFTs and good contracts are and the way they’re associated. Subsequent, we’ll look carefully at an important NFT improvement instruments. Lastly, earlier than inviting you to roll up your sleeves, we’ll additionally make sure you get acquainted with Moralis’ “getContractNFTs” endpoint. In any case, the latter will do all of the onerous give you the results you want within the backend in case you determine to finish this tutorial. Furthermore, in that case, you’ll additionally want your free Moralis account.
Exploring NFTs and Sensible Contracts
Non-fungible tokens, or NFTs, are distinctive sorts of crypto belongings. Because the “non-fungible” phrase illustrates, these tokens should not interchangeable. In any case, every NFT has distinctive encryption (on-chain signature). Consequently, this property makes NFTs nice for all kinds of use circumstances that contain uniqueness. Moreover, this is the reason digital artwork and digital collectibles are the obvious purposes of non-fungible tokens. Nonetheless, NFTs have a lot better potential – they might help create a extra clear, simply, and trustless future. In any case, NFTs are excellent for all kinds of possession proofs and certifications. Therefore, they’ll revolutionize each the digital and the actual world when applied correctly.
As with fungible tokens, good contracts additionally play a significant function in creating (minting) NFTs. These items of software program with predefined circumstances and actions current on-chain are important items of the puzzle. As such, there’s a good contract behind each NFT. In fact, it will depend on the kind of NFT and what number of NFTs are literally ruled by a specific good contract. For example, on the Ethereum community and different EVM-compatible chains, ERC-721 and ERC-1155 NFT good contracts run the present.
Sensible contracts additionally embrace important and elective particulars concerning the NFT(s). These particulars come within the type of metadata. As well as, hyperlinks to NFT recordsdata (resembling PNGs, JPEGs, MP3s, PDFs, and many others.) are additionally contained contained in the metadata recordsdata.
Lastly, as soon as the NFT good contracts are deployed, they’ll routinely provoke the minting technique of NFTs or supply a “chilly” minting performance. The latter allows minting at later occasions. To study extra about totally different NFT requirements and the mining processes, ensure that to discover the Moralis weblog.
NFT Growth Instruments
As talked about above, digital recordsdata, resembling pictures, are recordsdata represented by NFTs. It’s necessary to remember the fact that the recordsdata themselves are not any totally different than some other recordsdata. The actual worth of NFTs is of their encryption, hidden within the backend. Thus, after we discuss NFT improvement instruments, we concentrate on the minting course of and the instruments that allow the event of NFT dapps.
So, in case you have been to mint your personal NFTs, you’d have to have “NFT-representing” recordsdata and corresponding metadata recordsdata (JSON) prepared. Furthermore, you possibly can at all times use NFT improvement instruments to construct dapps using current NFTs. That mentioned, it’s necessary to create a correct plan whenever you need to create an NFT venture.
The next are probably the most needed NFT improvement instruments that may allow you to sort out all kinds of NFT tasks:
- Constructing and Testing NFT Dapps:
- Legacy dev instruments
- Moralis (Web3 APIs, together with the NFT API) – that is the only strategy to get all NFTs from contract
- Web3 wallets
- NFT Recordsdata and Metadata Storing:
- Decentralized storage options (e.g., IPFS)
- NFT Minting on EVM-Suitable Chains:
- EVM-compatible Web3 wallets
- Solidity or Viper to create EVM-compatible good contracts
- OpenZeppelin to get verified good contract templates
- The Remix IDE and Hardhat platform to compile, deploy, and confirm good contracts
- NFT Minting on Solana:
- Metaplex’s Sweet Machine
- Rust for tasks that will require distinctive on-chain Solana packages
- A Solana pockets (e.g., Phantom)
Get All NFTs from Contract – The “getContractNFTs” Endpoint
Within the subsequent part, you should have an opportunity to get your arms soiled and create your personal NFT explorer dapp. Since Moralis’ “getContractNFTs” endpoint will play the principle function, you could study extra about it earlier than really utilizing it. Moreover, this endpoint allows you to get all NFTs from contract, together with metadata for all NFTs for a given good contract. Furthermore, because the endpoint is proscribed to presenting 100 outcomes per web page, you should use the cursor parameter to get greater than 100 NFTs.
The above screenshot exhibits the “Get NFTs by contract” documentation web page. That is the place the place you possibly can discover this endpoint’s particulars and even copy the code you want. Furthermore, the “getContractNFTs” endpoint takes within the following parameters:
- “tackle“ – Right here, you should present an NFT good contract tackle. That is the one required parameter (all different parameters are elective).
- “chain“ – You possibly can decide which programmable chain to concentrate on (Moralis helps all main Web3 networks).
- “format“ – You possibly can decide what sort of token ID format you need to work with.
- “restrict“ – You possibly can set the specified web page measurement of the end result.
- “totalRange“ – You possibly can decide the variety of subranges to separate the outcomes into.
- “vary“ – You possibly can set the specified subrange to question.
- “cursor“ – This parameter allows you to get to the subsequent web page.
The best way to Get All NFTs from Contract – Step-by-Step Information
In at present’s tutorial, we’ll concentrate on utilizing NodeJS. As such, we’ll create a easy NodeJS backend dapp that may get all NFTs from contract utilizing the “getContractNFTs” endpoint. Nonetheless, earlier than we have a look at the code, let’s present you what you’ll be constructing.
An Instance Dapp Enabling You to Get All NFTs from Contract – Demo
In the event you have a look at the above picture, you possibly can see all of the enter fields our instance NFT explorer dapp affords. As such, you possibly can see that with a view to use the “Get NFTs” button, we have to present a wise contract tackle and choose the matching chain. Listed below are the outcomes we see for an instance NFT assortment (“Cool Cats”) on Ethereum:
Wanting on the above screenshot, you possibly can see that the “Get NFTs” button returns twenty NFTs. Nonetheless, our dapp additionally contains the “Load Extra” button that masses an extra twenty NFTs each time we click on on it.
Moreover, right here’s one other instance (“Voxies” NFTs) on a distinct chain (Polygon):
If you wish to construct your personal occasion of such an ideal NFT explorer dapp, observe the steps under.
Step 1: Create a NodeJS Backend Dapp
Word: If that is your first time making a NodeJS utility, ensure that to make use of the “Utilizing NodeJS” web page in Moralis’ docs. That is the place you’ll learn to set up the required dependencies and arrange and run an Categorical server that may begin the “index.js” file.
At this level, you must have a fundamental NodeJS utility prepared and operating on an Categorical server. Therefore, you possibly can concentrate on creating a correct “index.js” script that may do the heavy lifting on the backend. So, begin by defining the required constants on the high of the file:
const specific = require("specific"); const Moralis = require("moralis").default; const app = specific(); const cors = require("cors"); const port = 3000;
Subsequent, add these two traces of code to make use of “cors” and “specific”:
app.use(cors()); app.use(specific.json());
Lastly, you get to create the “get” endpoint for all NFTs, and that is the place you’ll use the “getContractNFTs” endpoint to get all NFTs from contract. Finally, these are the traces of code that will even verify for the cursor parameter in case customers need to load extra NFTs:
app.get("/allNft", async (req, res) => { strive { const { question } = req; let NFTs; if (question.cursor) { NFTs = await Moralis.EvmApi.nft.getContractNFTs({ tackle: question.tackle, chain: question.chain, cursor: question.cursor, restrict: 20, }); } else { NFTs = await Moralis.EvmApi.nft.getContractNFTs({ tackle: question.tackle, chain: question.chain, restrict: 20, }); } const end result = NFTs.uncooked; return res.standing(200).json({ end result }); } catch (e) { console.log(e); console.log("one thing went incorrect"); return res.standing(400).json(); } });
As you possibly can see, the above traces of code additionally make sure that the “getContractNFTs” endpoint will get its parameters from the consumer aspect (as demonstrated beforehand). So far as the cursor goes, the latter might be linked to the “Load Extra” button.
Step 2: Acquire Your Moralis API Key
Each time you run your “index.js” file, you need to begin your dapp. Nonetheless, earlier than you do this, you need to provoke Moralis. These are the traces of code that may care for that:
Moralis.begin({ apiKey: "MORALIS_API_KEY", }).then(() => { app.pay attention(port, () => { console.log(`Listening for API Calls`); }); });
By trying on the traces of code above, you possibly can see the “MORALIS_API_KEY” placeholder. It’s good to substitute the latter along with your precise Moralis Web3 API key. As such, log in to your Moralis account to entry your admin space. From there, you’ll get to go to the “Web3 APIs” web page and duplicate your API key:
By pasting your Web3 API key in place, you must have your backend prepared:
Nonetheless, you possibly can entry the whole “index.js” script to your backend on GitHub.
Step 3: Create a ReactJS Frontend Dapp
Because you in all probability have some expertise with creating JavaScript purposes, you shouldn’t have any issues creating the above-demonstrated frontend. Nonetheless, to make sure that you get it proper, we made the required scripts accessible on GitHub. As such, use our repo to have your frontend prepared in document time. Then, you’ll have the ability to discover the small print of the “App.js” file, which is the core of our dapp’s consumer aspect. Moreover, it’s the “fetchNFTs” operate that fetches the entered good contract tackle and chosen chain:
async operate fetchNFTs() { let res; if (cursor) { res = await axios.get(`http://localhost:3000/allNft`, { params: { tackle: tackle, chain: chain, cursor: cursor }, }); } else { res = await axios.get(`http://localhost:3000/allNft`, { params: { tackle: tackle, chain: chain }, }); }
Final however not least, you possibly can acquire extra perception into the code and functionalities of our instance dapp within the video under, beginning at 4:10. That is the place our in-house skilled first covers the gist of the backend. Then, beginning at 5:51, you possibly can evaluate the “App.js” file. Nonetheless, you can too see tips on how to discover the performance of your completed dapp utilizing your browser’s console (7:02). Lastly, beginning at 9:24, you possibly can learn to effortlessly change the variety of NFTs displayed per question.
The best way to Get All NFTs from Contract – Abstract
In at present’s article, you had a chance to study concerning the fundamentals of NFTs and good contracts. As such, you now know that good contracts are answerable for minting NFTs. We additionally coated the principle NFT improvement instruments. Then, you realized concerning the energy of Moralis’ “getContractNFTs” endpoint. Lastly, you had an opportunity to roll up your sleeves and observe our lead constructing an instance NFT explorer dapp. Accordingly, you now know tips on how to simply get all NFTs from contract.
In the event you loved at present’s tutorial, we encourage you to make use of the Moralis docs and sort out different instance tasks. In case you need to proceed your NFT programming journey, you must concentrate on the “NFT API” part. Alternatively, you possibly can discover different potentialities that the Moralis Web3 API gives. Additionally, ensure that to make use of the Moralis YouTube channel and our weblog to increase your blockchain improvement data. A few of our newest articles revolve round what a Web3 supplier is, how Web3 for enterprise works, an indication of a Web3 JS tutorial, constructing a Web3 Unity multiplayer sport, and far more.
Moreover, it’s possible you’ll be fascinated with touchdown an unbelievable job within the crypto sector. In that case, changing into blockchain-certified can considerably enhance your odds. That is the place Moralis Academy enters the image. There you could find many blockchain improvement programs, together with the one that may educate you tips on how to construct an NFT market. Furthermore, Moralis Academy can also be the place to get skilled mentorship, a customized research path, and develop into a member of one of many business’s most advancing communities.