Hi,
For the video: How To Get User NFTs? All NFTs Owned by Address
Create a Moralis Mainnet server with all 3 chains (Eth, Polygon, Bsc)
In the code : replace the “–APPLICATION ID–” and “–SERVERURL–” with those of your Moralisserver.
Example code (extended as in video):
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<!-- Moralis SDK code -->
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
</head>
<body>
<h1>NFT Explorer</h1>
<p>
Address: <input type="text" id="address" size="64" maxlength='64' /><br />
Chain: <select id="chain">
<option value="">Make choice...</option>
<option value="eth">ETH</option>
<option value="bsc">BSC</option>
<option value="polygon">Polygon</option>
</select><br />
<button id="btnUpdate">Update</button>
</p>
<h3>Content</h3>
<div id="content"></div>
<script>
// connect to Moralis server
Moralis.initialize("--APPLICATION ID--");
Moralis.serverURL = "--SERVERURL--";
async function getNFTs(chain, address) {
// get polygon NFTs for address
const options = { chain: chain, address: address };
var maxnr = 5;
const nftCount = await Moralis.Web3.getNFTsCount(options);
$("#content").html("<p>Items count: " + nftCount + " (max " + maxnr + " listed)</p>");
if (nftCount > 0) {
const allNFTs = await Moralis.Web3.getNFTs(options);
//console.log(allNFTs);
allNFTs.forEach( (nft) => {
if (maxnr > 0) {
fetch(fixURL(nft.token_uri))
.then(response => response.json())
.then(data => {
$("#content").html($("#content").html()
+ "<div><img width='100' align='left' src='" + fixURL(data.image) + "' />"
+ "<h2>" + data.name + "</h2>"
+ "<p>" + data.description + "</p></div><br clear='all' />");
});
}
maxnr--;
});
}
}
fixURL = (url) => {
if (url.startsWith("ipfs")) {
return "https://ipfs.moralis.io:2053/ipfs/" + url.split("ipfs://ipfs/")[1];
} else {
return url + "?format=json";
}
}
document.getElementById("btnUpdate").onclick = () => {
console.log("Update!");
let chain = $("#chain").val();
let address = $("#address").val();
console.log("Update! chain="+chain+" address="+address);
if (typeof chain !== 'undefined' && typeof address != 'undefined') {
getNFTs(chain, address);
}
}
</script>
</body>
</html>
Have a nice day !