0
0
Blockchain / Solidityprogramming~5 mins

Token metadata and URI in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Token metadata and URI help describe what a token is and where to find its details. This makes tokens easy to understand and use.

When creating a new digital token like an NFT or cryptocurrency.
When you want to show details about a token on a website or app.
When you need to link a token to images, descriptions, or other info.
When building marketplaces that display token information.
When verifying token authenticity and properties.
Syntax
Blockchain / Solidity
tokenURI = "https://example.com/api/token/123"

metadata = {
  "name": "Token Name",
  "description": "What this token represents",
  "image": "https://example.com/images/token123.png",
  "attributes": [
    {"trait_type": "Color", "value": "Red"},
    {"trait_type": "Size", "value": "Large"}
  ]
}

The tokenURI is a link to a JSON file with token details.

Metadata is usually a JSON object describing the token's properties.

Examples
This URI points to the metadata JSON for token #1.
Blockchain / Solidity
tokenURI = "https://mytokens.com/metadata/1.json"
Basic metadata with name, description, and image link.
Blockchain / Solidity
{
  "name": "Cool NFT",
  "description": "A unique digital art piece.",
  "image": "https://images.com/coolnft.png"
}
Metadata with custom attributes to describe token traits.
Blockchain / Solidity
{
  "name": "Rare Token",
  "attributes": [
    {"trait_type": "Rarity", "value": "Legendary"},
    {"trait_type": "Power", "value": 9000}
  ]
}
Sample Program

This program creates a token object with an ID and base URI. It then prints the full URI where the token's metadata JSON can be found.

Blockchain / Solidity
class Token:
    def __init__(self, token_id, base_uri):
        self.token_id = token_id
        self.base_uri = base_uri

    def token_uri(self):
        return f"{self.base_uri}/{self.token_id}.json"

# Create a token with ID 42 and base URI
my_token = Token(42, "https://tokens.example.com/metadata")

# Print the full URI to the token metadata
print(my_token.token_uri())
OutputSuccess
Important Notes

Token metadata is often stored off-chain (outside the blockchain) to save space.

URIs usually point to web servers or decentralized storage like IPFS.

Always ensure metadata links are stable and accessible to avoid broken tokens.

Summary

Token metadata describes what a token is and its details.

Token URI is a link to the metadata JSON file.

Metadata helps apps and users understand and display tokens properly.