How to view NFT metadata on hedera block chain
I recently created an nft on the testnet of the hedera blochain and its showing in my testnet hashpack wallet but within my code the meta data for the nfts being returned from my GET request shows a string different from the original CID i.e it shows this "UW1kWEZuUkIzdFN1Y3dYR2RUaE10NlVZNHUybkoxOXJjaVBWQlFuU25reHdhRA==". How can i generate back the ipfs link for the meta-data.
The string returned in the metadata field of your api response is encoded in base64, to get the correct metadata CID you have to decode it. I'll explain how using an NFT token that I created on hedera testnet. Currently from my API request I am getting the string below in my metadata field
UW1Oc0JSbkEySHY2M0tnUkNWTmhlV2piQ0xwWjFkQXFrUnBCclJSbzVUd3dpUg==
In other to convert this to its CID value all you have to do is use an awesome method "Buffer.from(str, base64)" provided by Node JS for decoding base 64 strings as shown below.
// current value of meta data
const currentValue = "UW1Oc0JSbkEySHY2M0tnUkNWTmhlV2piQ0xwWjFkQXFrUnBCclJSbzVUd3dpUg=="
const newValue = Buffer.from(currentValue, 'base64')
//create a representation in local sensitive format
const CIDVALUE = newValue.toLocaleString()
console.log("CID value", CIDVALUE)
After getting the CID value you can get the meta data of the NFT by making a get request as shown below
const res = await fetch(`https://ipfs.io/ipfs/${CIDVALUE}`)
const metaData = await res.json()
console.log("meta data", metaData)
You should see a response similar to the one below in the console;
meta data {
name: 'SALLAD',
creator: 'maesterzak',
description: 'Maester Chef Collection',
image: 'ipfs://QmUNiQgB1BJ7QB4Xy8ywQ44rnXNpYrQZgio122UJ9PBefm',
type: 'image/jpeg',
properties: { type: 'sallad', color: 'green', stripes: 'red', total: 10 },
files: [
{
uri: 'ipfs://QmSDiDU4Fe9EXCiW5ZuUvWP6dzXGhDyGB76K9UHeWsKD2H',
type: 'image/jpeg',
is_default_file: true
}
]
}
Our final code should like this ;
// current value of meta data
const currentValue = "UW1Oc0JSbkEySHY2M0tnUkNWTmhlV2piQ0xwWjFkQXFrUnBCclJSbzVUd3dpUg=="
const newValue = await Buffer.from(currentValue, 'base64')
//create a representation in local sensitive format
const CIDVALUE = newValue.toLocaleString()
console.log("CID value", CIDVALUE)
const res = await fetch(`https://ipfs.io/ipfs/${CIDVALUE}`)
const metaData = await res.json()
console.log("meta data", metaData)
That's all for now feel free to contribute to thread if you have any questions or suggestions.
Similar Content
Privacy Policy
By using our website,
you agree that devmaesters can store cookies on your device and disclose information in accordance with our privacy policy.