To fetch NFTs from a contract and current them to customers is a primary but important job builders should know the right way 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 handle, together with metadata for all accessible NFTs. Should you’re an skilled dev eager to get going straight away, first ensure to enroll with Moralis, and you may then go to the documentation web page and get going straight away! Nonetheless, if you wish to see a step-by-step strategy of implementing and finishing your complete file for the above code snippet, learn on!

Overview
Over the past bull market, quite a few NFT-related initiatives emerged, together with NFT-gated web sites, marketplaces, and so forth. Now, there may be an rising variety of use instances and varied utility options builders have found for these tokens. As such, now’s one of the best time to discover ways to simply construct all kinds of decentralized purposes (dapps) round these sorts of tokens. With that stated, an important place to begin is a neat NFT explorer – a dapp that allows you to get all NFTs from contract. Whereas this job may sound difficult, it’s fairly a simple course of whenever you use the fitting instruments.
Because of Moralis’ final NFT API and its “getContractNFTs” endpoint, you possibly can have a completely practical NFT explorer up and working in minutes, which is precisely what you’ll study within the remaining stage of in the present day’s article. Nonetheless, earlier than we will deal with displaying you the right way to get all NFTs from contract the simple manner, we have to cowl some fundamentals. Thus, we’ll first guarantee you understand what NFTs and sensible contracts are and the way they’re associated. Subsequent, we’ll look intently at an important NFT growth 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 exhausting give you the results you want within the backend when you determine to finish this tutorial. Furthermore, in that case, you’ll additionally want your free Moralis account.
Exploring NFTs and Good Contracts
Non-fungible tokens, or NFTs, are distinctive sorts of crypto property. Because the “non-fungible” phrase illustrates, these tokens usually are not interchangeable. In any case, every NFT has distinctive encryption (on-chain signature). Consequently, this property makes NFTs nice for all kinds of use instances that contain uniqueness. Moreover, for this reason digital artwork and digital collectibles are the obvious purposes of non-fungible tokens. Nonetheless, NFTs have a lot better potential – they will help create a extra clear, simply, and trustless future. In any case, NFTs are good for all kinds of possession proofs and certifications. Therefore, they will revolutionize each the digital and the true world when carried out correctly.
As with fungible tokens, sensible contracts additionally play a significant function in creating (minting) NFTs. These items of software program with predefined situations and actions current on-chain are important items of the puzzle. As such, there’s a sensible contract behind each NFT. In fact, it depends upon the kind of NFT and what number of NFTs are literally ruled by a selected sensible contract. As an example, on the Ethereum community and different EVM-compatible chains, ERC-721 and ERC-1155 NFT sensible contracts run the present.
Good contracts additionally embrace important and non-compulsory particulars in regards to the NFT(s). These particulars come within the type of metadata. As well as, hyperlinks to NFT recordsdata (similar to PNGs, JPEGs, MP3s, PDFs, and so forth.) are additionally contained contained in the metadata recordsdata.
Lastly, as soon as the NFT sensible contracts are deployed, they will robotically provoke the minting strategy of NFTs or provide a “chilly” minting performance. The latter permits minting at later occasions. To study extra about completely different NFT requirements and the mining processes, ensure to discover the Moralis weblog.
NFT Improvement Instruments
As talked about above, digital recordsdata, similar to pictures, are recordsdata represented by NFTs. It’s necessary to take into account that the recordsdata themselves aren’t any completely different than another recordsdata. The actual worth of NFTs is of their encryption, hidden within the backend. Thus, once we speak about NFT growth instruments, we deal with the minting course of and the instruments that allow the event of NFT dapps.
So, when you have been to mint your individual NFTs, you’d have to have “NFT-representing” recordsdata and corresponding metadata recordsdata (JSON) prepared. Furthermore, you possibly can at all times use NFT growth instruments to construct dapps using current NFTs. That stated, it’s necessary to create a correct plan whenever you wish to create an NFT venture.
The next are essentially the most crucial NFT growth instruments that may make it easier to sort out all kinds of NFT initiatives:
- Constructing and Testing NFT Dapps:
- Legacy dev instruments
- Moralis (Web3 APIs, together with the NFT API) – that is the only approach 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 sensible contracts
- OpenZeppelin to get verified sensible contract templates
- The Remix IDE and Hardhat platform to compile, deploy, and confirm sensible contracts
- NFT Minting on Solana:
- Metaplex’s Sweet Machine
- Rust for initiatives which 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’ll have an opportunity to get your fingers soiled and create your individual NFT explorer dapp. Since Moralis’ “getContractNFTs” endpoint will play the primary function, you need to 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 sensible contract. Furthermore, because the endpoint is proscribed to presenting 100 outcomes per web page, it’s essential use the cursor parameter to get greater than 100 NFTs.
The above screenshot reveals 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:
- “handle“ – Right here, it’s essential present an NFT sensible contract handle. That is the one required parameter (all different parameters are non-compulsory).
- “chain“ – You possibly can decide which programmable chain to deal with (Moralis helps all main Web3 networks).
- “format“ – You possibly can decide what kind of token ID format you wish to work with.
- “restrict“ – You possibly can set the specified web page dimension 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.
Learn how to Get All NFTs from Contract – Step-by-Step Information
In in the present day’s tutorial, we’ll deal with utilizing NodeJS. As such, we’ll create a easy NodeJS backend dapp that can get all NFTs from contract utilizing the “getContractNFTs” endpoint. Nonetheless, earlier than we take 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
Should you take a look at the above picture, you possibly can see all of the enter fields our instance NFT explorer dapp presents. As such, you possibly can see that with the intention to use the “Get NFTs” button, we have to present a wise contract handle and choose the matching chain. Listed here 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 hundreds 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 individual occasion of such an important NFT explorer dapp, observe the steps under.
Step 1: Create a NodeJS Backend Dapp
Observe: If that is your first time making a NodeJS utility, ensure to make use of the “Utilizing NodeJS” web page in Moralis’ docs. That is the place you’ll discover ways to set up the required dependencies and arrange and run an Categorical server that can begin the “index.js” file.
At this level, it is best to have a primary NodeJS utility prepared and working on an Categorical server. Therefore, you possibly can deal with creating a correct “index.js” script that can 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 strains 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 strains of code that may even test for the cursor parameter in case customers wish 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({ handle: question.handle, chain: question.chain, cursor: question.cursor, restrict: 20, }); } else { NFTs = await Moralis.EvmApi.nft.getContractNFTs({ handle: question.handle, 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 unsuitable"); return res.standing(400).json(); } });
As you possibly can see, the above strains of code additionally make sure that the “getContractNFTs” endpoint will get its parameters from the shopper facet (as demonstrated beforehand). So far as the cursor goes, the latter will probably be related to the “Load Extra” button.
Step 2: Receive Your Moralis API Key
Each time you run your “index.js” file, you wish to begin your dapp. Nonetheless, earlier than you try this, you wish to provoke Moralis. These are the strains of code that can care for that:
Moralis.begin({ apiKey: "MORALIS_API_KEY", }).then(() => { app.hear(port, () => { console.log(`Listening for API Calls`); }); });
By wanting on the strains of code above, you possibly can see the “MORALIS_API_KEY” placeholder. That you must exchange the latter together 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 replica your API key:
By pasting your Web3 API key in place, it is best to have your backend prepared:
Nonetheless, you possibly can entry the entire “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 shopper facet. Moreover, it’s the “fetchNFTs” perform that fetches the entered sensible contract handle and chosen chain:
async perform fetchNFTs() { let res; if (cursor) { res = await axios.get(`http://localhost:3000/allNft`, { params: { handle: handle, chain: chain, cursor: cursor }, }); } else { res = await axios.get(`http://localhost:3000/allNft`, { params: { handle: handle, chain: chain }, }); }
Final however not least, you possibly can achieve 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 knowledgeable first covers the gist of the backend. Then, beginning at 5:51, you possibly can evaluate the “App.js” file. Nonetheless, you may also see the right way to discover the performance of your completed dapp utilizing your browser’s console (7:02). Lastly, beginning at 9:24, you possibly can discover ways to effortlessly change the variety of NFTs displayed per question.
Learn how to Get All NFTs from Contract – Abstract
In in the present day’s article, you had a possibility to study in regards to the fundamentals of NFTs and sensible contracts. As such, you now know that sensible contracts are liable for minting NFTs. We additionally lined the primary NFT growth instruments. Then, you discovered in regards to 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 the right way to simply get all NFTs from contract.
Should you loved in the present day’s tutorial, we encourage you to make use of the Moralis docs and sort out different instance initiatives. In case you wish to proceed your NFT programming journey, it is best to deal with the “NFT API” part. However, you possibly can discover different potentialities that the Moralis Web3 API offers. Additionally, ensure to make use of the Moralis YouTube channel and our weblog to increase your blockchain growth data. A few of our newest articles revolve round what a Web3 supplier is, how Web3 for enterprise works, an illustration of a Web3 JS tutorial, constructing a Web3 Unity multiplayer recreation, and rather more.
Moreover, chances are you’ll be interested by touchdown an unimaginable 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’ll find many blockchain growth programs, together with the one that can educate you the right way to construct an NFT market. Furthermore, Moralis Academy can be the place to get knowledgeable mentorship, a personalised research path, and turn into a member of one of many trade’s most advancing communities.