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.
- Discover how you can save thousands in infra costs every month with our unbeatable pricing on the most complete Web3 development platform.
- Input your workload and see how affordable Chainstack is compared to other RPC providers.
- Connect to Ethereum, Polygon, BNB Smart Chain, Base, Avalanche, Arbitrum, zkSync Era, Polygon zkEVM, Optimism, Oasis Sapphire, NEAR, Aurora, Solana, Scroll, Aptos, Gnosis Chain, Cronos, Filecoin, Fantom, StarkNet, Harmony and Tezos mainnet or testnets through an interface designed to help you get the job done.
- To learn more about Chainstack, visit our Developer Portal or join our Discord server and Telegram group.
- Are you in need of testnet tokens? Request some from our faucets. Multi-chain faucet, Sepolia faucet, Goerli faucet, Holesky faucet, BNB faucet, zkSync faucet, Scroll faucet.
Have you already explored what you can achieve with Chainstack? Get started for free today.