Metaplex Solana NFT metadata – How to get NFTs metadata?
You are a developer on Solana.
Your job is all about NFTs, especially Metaplex NFTs. Through intensive research, you find out the metadata of all Metaplex tokens is stored in an account created during minting.
You track down an address with a Metaplex NFT and send over the getAccountInfo RPC call to retrieve NFT metadata.
It returns a string that you have never seen before. It is long, it is random, but you just know this is what you are after.
Nervously, you open an online decoder for base64, paste the string into the text field, and you see:
This is so confusing.
A part of this string is correctly decoded, but the majority of it is still just black diamonds with a question mark. You wonder what format it is decoded from. Is it ASCII? UTF-8? You try all formats, but none of them works.
Well, you are at the right place. There are essentially two parts to this article:
- Why doesn’t the decoder work?
- Python code snippet on Google Colab to the rescue.
Hope this article can be the final piece of the puzzle.
Why doesn’t the decoder work?
Long story short, that is because the original object is not a string, JSON, or any kind of data that can be directly decoded into.
Take this STEPN shoe NFT as an example:
The original metadata can be found on the explorer tab. It is a JSON object.
When the original metadata is processed, it is first transformed into an array of bytes, and then into base64 text for storage purposes.
However, the encoding of the original fields is not unified. Some fields are base58 encoded, and others UTF-8 encoded.
Even though both methodologies are text-based, the mismatched decoding results in unidentified characters and misinterpretation.
Using the source account as an example.
To encode the source account, firstly it is decoded to byte data:
Then the byte data is encoded into a base64 text:
sku0a/f9w7D1gJ82cZokGXec75GzLhtEy2iTHJN0Uzc. The text piece can be found in the original text too—it starts from the 45th character.
If this string is decoded backward, but in a different format, for example, UTF-8, it throws an error. This is because UTF-8 does not have the mapping for the original byte data and is unable to interpret the context.
How to get NFTs metadata?
To decode the metadata information, the first thing we need to know is how the fields were encoded originally. Luckily, it can be found in the Metaplex Python API source code.
The source code was moved to this Jupyter notebook on Google Colab to demonstrate how decoding can be done.
For readers not familiar with a Jupyter notebook, you can either choose Run all in the Runtime tab to run the code:
Or press the run button to run it cell by cell.
By doing so, the kernel loads all necessary packages and functions. When it is done, scroll down to the last cell. Change the value of
rawdata and press the run button to run the code.
If everything goes well, you should see a decoded object displayed.
- Connect to the Ethereum, Polygon, BNB Smart Chain, Avalanche, Arbitrum, NEAR, Aurora, Fantom, Cronos, Solana, Gnosis Chain, Harmony, Tezos, Fuse and StarkNet mainnet or testnets through the interface designed to help you get the job done.
- Get access to the Ethereum, Polygon, BNB Smart Chain, Avalanche, Fantom, Cronos and Tezos archive nodes to query the entire history of the mainnet—starting at just $49 per month.
- Choose where you want to deploy, and we will provide you with the dedicated managed infrastructure that can handle high-volume, high-velocity read/write access to the network.
- To learn more about Chainstack, visit our Knowledge Center or join our Discord server and Telegram group.
Have you already explored what you can achieve with Chainstack? Get started for free today.