Constructing NFT-related dapps (decentralized functions) requires the perfect instruments. Because of the last word NFT API, you may simply combine particular options to your dapps, akin to getting all NFTs owned by an handle. To get all NFTs owned by handle, you should use the next script that features the “getWalletNFTs” endpoint:
const Moralis = require(“moralis”).default;
const { EvmChain } = require(“@moralisweb3/evm-utils”);
const runApp = () => {
await Moralis.begin({
apiKey: “YOUR_API_KEY”,
// …and some other configuration
});
const handle = “0xd8da6bf26964af9d7eed9e03e53415d37aa96045”;
const chain = EvmChain.ETHEREUM;
const response = await Moralis.EvmApi.nft.getWalletNFTs({
handle,
chain,
});
console.log(response);
}
runApp();
If you happen to’ve labored with Moralis earlier than, you most likely know the right way to use the above traces of code. Therefore, merely use the hyperlink beneath to get to the documentation web page to get began instantly. Nonetheless, if that is your first rodeo with Moralis and you prefer to a step-by-step course of on the right way to get all NFTs owned by handle, make sure that to finish right this moment’s three-step tutorial!
Overview
Transferring ahead, we’ll first do a correct overview of the subject to make sure you all know what NFTs and Web3 addresses are. Then, we’ll take a better have a look at the “getWalletNFTs” endpoint, the place we’ll additionally discover its parameters. Lastly, we’ll take you thru the three steps you must full to get all NFTs owned by handle.
Moreover, we’ll concentrate on the Ethereum chain on this article. Nonetheless, we’ll additionally clarify the right way to tweak a single snippet of code to sort out different main programmable chains. In spite of everything, Moralis is all about cross-chain interoperability. Because of this, Moralis allows you to develop on main blockchain networks, together with BNB Good Chain, Avalanche, Polygon, and extra! With that stated, if you wish to discover ways to get all NFTs owned by handle the simple manner, create your free Moralis account and comply with our lead!
What are NFTs and Web3 Addresses?
Non-fungible tokens are particular sorts of crypto belongings. Not like fungible tokens, NFTs usually are not interchangeable. In spite of everything, every NFT has its distinctive token ID (on-chain signature) that makes it totally different from some other NFT. This property makes non-fungible tokens nice for digital artwork and digital collectibles. Additionally, since NFTs can symbolize all kinds of recordsdata, they’re beginning to revolutionize real-world use instances. Furthermore, NFTs could function proof of possession, certifications, and extra. After all, NFTs are additionally altering the gaming business. Gamers can really personal their sport progress by tokenizing (changing to NFTs) in-game belongings. Consequently, they’ll commerce their NFTs and switch their in-game belongings into the world financial system.
Moreover, it’s price declaring that NFTs should be minted. That is the method of executing an on-chain transaction that shops an NFT’s metadata on the blockchain. Furthermore, minting NFTs is finished by deploying sensible contracts (on-chain items of software program), which comprise predefined situations and actions relating to the NFT(s) they concentrate on. Additionally, to make sure that issues comply with established tips, there are totally different sensible contracts for various NFT requirements. The most typical ones are ERC-721 and ERC-1155. Keep in mind that each NFT is assigned to the sensible contract used to mint it. Accordingly, it’s also possible to get all NFTs from contract.
Nonetheless, as soon as customers purchase or achieve possession of an NFT, the latter is transferred to their Web3 pockets. Furthermore, a Web3 pockets can are available in many types, however basically, it’s a mixture of personal and public keys. The general public keys are often known as blockchain addresses. Therefore, we will get all NFTs owned by handle.
NFT API to Get All NFTs Owned by Deal with
This part takes a better have a look at Moralis’ “getWalletNFTs” endpoint. The latter permits devs to get all NFTs owned by handle with out breaking a sweat. Additional, this NFT API endpoint returns the response that features the “[SYNCED/SYNCING]” standing based mostly on the contracts that the code is indexing. This endpoint’s outcomes embrace all listed NFTs. Any request that features the “token_adress” parameter will begin the indexing course of for that NFT assortment the primary time the script requests it. As well as, the “getWalletNFTs” endpoint contains a number of elective question parameters, which provide help to additional refine your search.
In the end, listed below are all of the parameters you should use when working with the above-stated NFT API endpoint:
“handle” – That is the NFT proprietor’s blockchain handle and the one required parameter. “chain“ – This parameter allows you to decide the chain you need to question. You possibly can select amongst all supported chains. “format“ – You possibly can select between the decimal or HEX format of the token ID.“restrict“ – This parameter lets you set the specified web page dimension of the outcome.“token_addresses” – You should utilize an array of strings representing totally different addresses you need to get balances for.“cursor“ – The cursor parameter comes within the earlier response and serves for getting the following web page.
How you can Discover the “getWalletNFTs” Endpoint Your self
If you wish to discover the “getWalletNFTs” endpoint and even view its response for particular parameters, make sure that to go to the Moralis documentation:
Because the above screenshot signifies, you need to enter a pockets handle first. After all, it’s also possible to add any of the elective parameters. Subsequent, you need to choose the programming language within the top-right a part of the web page. Lastly, you may hit the “Attempt It!” button and look at the response beneath. Nonetheless, to see the actual energy of this Moralis’ NFT API endpoint, you’ll want to stick in your Moralis Web3 API key. You possibly can discover ways to receive that key with two clicks within the “Step 1: Set Up Moralis” part beneath.
3-Step Tutorial – How you can Get All NFTs Owned By Deal with
Earlier than we take you thru the main points of this three-step tutorial, it’s price declaring that with the ability to get all NFTs owned by handle is extraordinarily helpful. In spite of everything, you already know that NFTs can play important roles in lots of dapps. Devs use most of these tokens to symbolize possession of a singular merchandise – both digital or bodily – on the blockchain. A few of the most typical NFT utility use instances embrace NFT portfolio trackers, NFT-gated web sites, DAOs, and social graphs. As such, all of those require the backend code to get all NFTs owned by handle. Therefore, by mastering the upcoming three steps, you’ll be capable of construct all kinds of NFT dapps.
Moreover, there are some stipulations you must deal with earlier than you can begin implementing the code:
It is best to have Node v.14 or increased prepared.Have your favourite code editor or IDE put in and arrange. We desire to make use of Visible Studio Code (VSC).Additionally, be sure to have your favourite package deal supervisor (“npm“, “yarn“, or “pnpm“) put in.
Step 1: Set Up Moralis
If you happen to haven’t performed so, create your free Moralis account now. You should utilize the hyperlink said on the outset of this text or go to the Moralis homepage and click on on the “Begin for Free” button:
Whichever path you select, you’ll land on the register web page, the place you must enter your particulars:
When you enter your full title, e-mail handle, and password and settle for the phrases and situations, you’ll be capable of hit the “Signal Up” button. Then, you simply want to substantiate your account by clicking on the hyperlink that can arrive in your e-mail inbox.
Along with your Moralis account prepared, you may receive your Moralis Web3 API key. To do that, choose the “Web3 APIs” possibility in your admin space. Then, use the copy icon subsequent to your API key:
We’ll present you the place to stick your API key shortly. Nonetheless, first, you need to set up the Moralis SDK in your venture. To do that, use your terminal and enter one of many following instructions (relying on the package deal supervisor that you’re utilizing):
npm set up moralisyarn add moralispnpm add moralis
Step 2: Get All NFTs Owned By Deal with
With the Moralis SDK put in, you should use the traces of code offered on the prime of this text. If you happen to keep in mind, the “getWalletNFTs” endpoint is the script’s core. It makes use of the required “handle” and elective “chain” parameters. As such, you need to resolve which chain you’ll concentrate on and which handle you’ll discover. After all, you may comply with our lead and use the instance handle within the “index” script and concentrate on the Ethereum chain.
By wanting on the traces of code beneath, you may see that we use “EvmChain.ETHEREUM” to outline the chain we need to concentrate on. So, when you shift your consideration to some other chain, you’d want to alter that line of code.
Listed here are the supported EVM-compatible chains and the snippets of code you must exchange “ETHEREUM” with:
Ethereum Görli: “GOERLI”Ethereum Sepolia: “SEPOLIA”Polygon Mainnet: “POLYGON”Polygon Mumbai Testnet: “MUMBAI”BNB Good Chain Mainnet: “BSC”BNB Good Chain Testnet: “BSC_TESTNET”Avalanche C-Chain: “AVALANCHE”Avalanche Fuji Testnet: “FUJI”Fantom: “FANTOM”Cronos Mainnet: “CRONOS” Cronos Testnet: “CRONOS_TESTNET”
Notice: Along with the above chains, you should use the Moralis Solana API. Nonetheless, since Solana is in a non-EVM-compatible chain, you must tweak different traces of code and use a special API endpoint.
Use JavaScript or TypeScript
Beneath is identical code as within the intro. Nonetheless, we optimized it beneath for TypeScript (TS) whereas it was within the JavaScript (JS) format on the outset. So, that is what the “index.ts” script that lets you get all NFTs owned by handle on Ethereum appears like:
import Moralis from “moralis”;
import { EvmChain } from “@moralisweb3/evm-utils”;
const runApp = () => {
await Moralis.begin({
apiKey: “YOUR_API_KEY”,
// …and some other configuration
});
const handle = “0xd8da6bf26964af9d7eed9e03e53415d37aa96045”;
const chain = EvmChain.ETHEREUM;
const response = await Moralis.EvmApi.nft.getWalletNFTs({
handle,
chain,
});
console.log(response);
}
runApp();
Whether or not you utilize JS or TS, don’t forget to switch the “YOUR_API_KEY” placeholder along with your Web3 API key. Additionally, be at liberty to make use of different addresses and chains, as defined above.
Step 3: Execute the Program and Discover the Outcomes
Along with your “index.js” or “index.ts” file prepared, it’s time to execute this system. Therefore, use one of many following instructions:
node index.jsnode index.ts
After operating the suitable command, you’ll see the leads to your terminal. Right here’s what the outcomes appear like for the “0xd8da6bf26964af9d7eed9e03e53415d37aa96045” handle on Ethereum:
{
“complete”: 1456,
“web page”: 1,
“page_size”: 100,
“cursor”: “eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjdXN0b21QYXJhbXMiOnsid2FsbGV0QWRkcmVzcyI6IjB4ZDhkYTZiZjI2OTY0YWY5ZDdlZWQ5ZTAzZTUzNDE1ZDM3YWE5NjA0NSJ9LCJrZXlzIjpbIjE2NjMyMzgxNzUuMDc3Il0sIndoZXJlIjp7Im93bmVyX29mIjoiMHhkOGRhNmJmMjY5NjRhZjlkN2VlZDllMDNlNTM0MTVkMzdhYTk2MDQ1In0sImxpbWl0IjoxMDAsIm9mZnNldCI6MCwib3JkZXIiOltdLCJ0b3RhbCI6MTQ1NiwicGFnZSI6MSwidGFpbE9mZnNldCI6MSwiaWF0IjoxNjY2NjgyNTUyfQ.E5DkWYvRTaFnVhgedRuT3IW-rb2V-ikFKwP2cg2Qf78”,
“outcome”: [
{
“token_address”: “0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85”,
“token_id”: “4765809967066625256798886812262830659450023020194524584471225959000376492819”,
“amount”: “1”,
“owner_of”: “0xd8da6bf26964af9d7eed9e03e53415d37aa96045”,
“token_hash”: “ba6d44b5f16be94283cecffeb784b7ca”,
“block_number_minted”: “15572796”,
“block_number”: “15573017”,
“contract_type”: “ERC721”,
“name”: “Ethereum Name Service”,
“symbol”: “ENS”,
“token_uri”: null,
“metadata”: null,
“last_token_uri_sync”: null,
“last_metadata_sync”: “2022-09-20T06:06:08.153Z”,
“minter_address”: null
},
{
“token_address”: “0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85”,
“token_id”: “84453794918345416145331514647027903846664455083247396107154093349515123913389”,
“amount”: “1”,
“owner_of”: “0xd8da6bf26964af9d7eed9e03e53415d37aa96045”,
“token_hash”: “7c0212cd3daf1b6b64f193c6dc102fb4”,
“block_number_minted”: “15572811”,
“block_number”: “15573017”,
“contract_type”: “ERC721”,
“name”: “Ethereum Name Service”,
“symbol”: “ENS”,
“token_uri”: null,
“metadata”: null,
“last_token_uri_sync”: null,
“last_metadata_sync”: “2022-09-20T06:09:09.838Z”,
“minter_address”: null
},
{
“token_address”: “0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85”,
“token_id”: “16476931145019337030786748713476010946621971075817308111460324192065814192354”,
“amount”: “1”,
“owner_of”: “0xd8da6bf26964af9d7eed9e03e53415d37aa96045”,
“token_hash”: “627ffc76405a90ee940cb91f7e90b294”,
“block_number_minted”: “15572818”,
“block_number”: “15573017”,
“contract_type”: “ERC721”,
“name”: “Ethereum Name Service”,
“symbol”: “ENS”,
“token_uri”: null,
“metadata”: null,
“last_token_uri_sync”: null,
“last_metadata_sync”: “2022-09-20T06:11:34.545Z”,
“minter_address”: null
},
],
“standing”: “SYNCED”
}
We encourage you to run the above program for numerous addresses on totally different chains and discover the outcomes returned by your terminal. Then, put your new means to additional use by creating an precise dapp that can both show customers’ NFTs or grant them entry based mostly on their possession of particular NFTs. If that sounds fascinating, make sure that to tackle our “NFT-gated web site” tutorial in our documentation.
Nonetheless, in case you have an interest in blockchain gaming growth, our Web3 Unity content material is simply what you want. The latter awaits you on the Moralis YouTube channel and the Moralis weblog.
How you can Get All NFTs Owned by an Deal with – 3-Step Course of – Abstract
We lined lots of floor in right this moment’s article. First, we did a fast overview of NFTs and Web3 wallets, the place you additionally realized what blockchain addresses are. Subsequent, we seemed carefully on the Moralis NFT API endpoint – “getWalletNFTs“. That is the place you came upon that you may check this endpoint (and all others) utilizing the Moralis docs. You additionally realized the right way to simply choose the programming language and replica the suitable traces of code. Lastly, we took you thru our three-step tutorial illustrating the right way to get all NFTs owned by handle effortlessly. Accordingly, you had an opportunity to finish the next three steps:
Set Up MoralisCreate a Script to Get All NFTs Owned by AddressExecute the Program and Discover the Outcomes
Through the use of the information and expertise obtained herein, you may simply begin creating some killer dapps. With the Moralis docs at your facet, it is best to be capable of do this utilizing your favourite programming framework. Moreover, you may stick with NFT utility or shift your focus to different forms of dapps. In both case, you will get extra steerage from different Moralis sources. Each our YouTube channel and weblog are nice retailers to help your ongoing crypto training free of charge. A few of our newest subjects cowl the perfect crypto pockets API, the perfect Web3 supplier, Web3 for enterprise, a Web3 JS tutorial, constructing a Web3 Unity multiplayer sport, and far more.
After all, you may moreover pace up your means of changing into a blockchain professional by enrolling in Moralis Academy. There you get to discover the total scope of blockchain growth. In case you are new to the crypto area, we advocate beginning with blockchain and Bitcoin fundamentals.