I’m working on displaying NFT data in a table but having trouble getting the actual image URLs. My code loops through NFT data and shows the metadata URI, but I need to fetch the image from that URI.
Here’s what I’m working with:
for (let j=0; j < nftList.length; j++){
console.log(nftList[j].metadata_uri)
let tableRow = `<tr>
<td>${nftList[j].id}</td>
<td>${nftList[j].type}</td>
<td><h4>${nftList[j].contract_addr}</h4></td>
<td>${nftList[j].metadata_uri}</td>
</tr>`
nftTable.innerHTML += tableRow
}
The metadata URI gives me something like: ipfs://bafkreic47njf53ueqgsrtuqh4jtqzcxfheiifilnypfjckvqtyiwefjr2y
But when I access that URL, I get JSON metadata like:
{"name": "CryptoArt #123", "description": "Digital artwork collection", "image": "https://example.com/artwork123.png", "attributes": {"rarity": "rare"}}
I want to extract just the image URL and display it in my table instead of the metadata URI. How can I fetch this data and show the actual image?
Here’s my current setup:
async function loadNFTs(){
const tokens = await Web3API.wallet.getTokens({chain: 'polygon'}).then(createNFTTable);
}
function createNFTTable(response){
let nftList = response.result;
document.getElementById("nftContainer").innerHTML = `<table class="table table-striped" id="tokenTable"></table>`;
const nftTable = document.getElementById("tokenTable");
const headerRow = `<thead><tr><th>Token ID</th><th>Contract Type</th><th>Address</th><th>Image</th></tr></thead>`
nftTable.innerHTML += headerRow;
for (let j=0; j < nftList.length; j++){
let tableRow = `<tr>
<td>${nftList[j].id}</td>
<td>${nftList[j].type}</td>
<td><h4>${nftList[j].contract_addr}</h4></td>
<td>${nftList[j].metadata_uri}</td>
</tr>`
nftTable.innerHTML += tableRow
}
}