0Pricing
Web3 & DApp Development Fundamentals · Aula

Metadados de tokens

tokenURI e JSON

Metadados de tokens é uma aula grátis de Web3 & DApp Development Fundamentals no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Web3 & DApp Development Fundamentals, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Web3 & DApp Development Fundamentals inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

What Is Token Metadata?

Metadata describes what an NFT actually represents: its name, description, image, and attributes. While ownership is stored on-chain, the descriptive data usually lives off-chain in a JSON file.

The link between them is the tokenURI.

The tokenURI Function

The metadata extension defines tokenURI(tokenId), which returns a URL pointing to the JSON metadata for that specific token.

<code>function tokenURI(uint256 tokenId) external view returns (string memory);</code>

The Metadata JSON Schema

The standard JSON has a few well-known fields. Marketplaces like OpenSea read these to display the NFT.

<code>{
  'name': 'Cool Cat #42',
  'description': 'A very cool cat.',
  'image': 'ipfs://Qm.../42.png',
  'attributes': [
    { 'trait_type': 'Color', 'value': 'Blue' }
  ]
}</code>

Attributes and Traits

The attributes array lists traits as objects with trait_type and value. These power rarity rankings and filtering on marketplaces.

<code>'attributes': [
  { 'trait_type': 'Background', 'value': 'Sky' },
  { 'trait_type': 'Level', 'value': 5 }
]</code>

Per-Token URIs

One approach stores a unique URI per token in a mapping. This is flexible but uses more storage.

<code>mapping(uint256 => string) private _tokenURIs;

function tokenURI(uint256 id) public view returns (string memory) {
    return _tokenURIs[id];
}</code>

Base URI Pattern

A cheaper approach stores a single base URI and appends the token id. This works when files are named sequentially like 1.json, 2.json, and so on.

<code>string private baseURI = 'ipfs://QmFolder/';

function tokenURI(uint256 id) public view returns (string memory) {
    return string(abi.encodePacked(baseURI, _toString(id), '.json'));
}</code>

Why IPFS?

Hosting metadata on a normal server is risky: if the server goes down or the file changes, the NFT breaks. IPFS uses content-addressed hashes, so a URI like ipfs://Qm... always points to the exact same immutable content.

Content Addressing

An IPFS hash (CID) is derived from the file's content. Change one byte and the hash changes. This guarantees the metadata cannot be silently swapped, preserving the NFT's integrity.

<code>// image: 'ipfs://bafybeih.../art.png'
// the CID itself proves the content is unaltered</code>

On-Chain Metadata

For fully decentralized NFTs, you can store metadata entirely on-chain by returning a base64-encoded data URI. This costs more gas but removes any off-chain dependency.

<code>// returns: data:application/json;base64,<encoded JSON>
// fully on-chain, no IPFS or server needed</code>

Reveal Mechanics

Many drops mint with a placeholder URI, then switch the base URI after sale to reveal the real art. This prevents buyers from sniping rare traits before mint.

<code>bool public revealed = false;

function setRevealed() public onlyOwner {
    baseURI = 'ipfs://QmRealArt/';
    revealed = true;
}</code>

How Marketplaces Read It

A marketplace calls tokenURI(id), fetches the returned JSON, then loads the image and renders attributes. Following the standard schema is what makes your NFT display correctly everywhere.

Quick Check

Test your understanding of token metadata.

Recap

You learned about NFT metadata:

  • tokenURI(id) returns a link to the token's JSON metadata
  • The JSON holds name, description, image, and attributes
  • Use per-token URIs or a base URI + id pattern
  • IPFS content addressing keeps metadata immutable; on-chain metadata removes dependencies entirely
  • Reveal patterns hide art until after mint

Perguntas Frequentes

A aula “Metadados de tokens” é grátis?

Sim — o texto completo de “Metadados de tokens” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Web3 & DApp Development Fundamentals, atualize para CoddyKit PRO. O curso de Web3 & DApp Development Fundamentals inclui 4 aulas no total.

O que vou aprender em “Metadados de tokens”?

tokenURI e JSON Você pratica Web3 & DApp Development Fundamentals com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Web3 & DApp Development Fundamentals?

Nenhuma experiência prévia é necessária. Web3 & DApp Development Fundamentals no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.

Quanto tempo leva a aula “Metadados de tokens”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Web3 & DApp Development Fundamentals?

Sim. Cada aula de Web3 & DApp Development Fundamentals inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. O padrão ERC-721
  2. Emissão de NFTs
  3. Metadados de tokens
  4. Mercados
← Voltar para Web3 & DApp Development Fundamentals