令牌元数据
tokenURI 与 JSON
令牌元数据 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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
常见问题解答
「令牌元数据」课时是免费的吗?
是的 — 「令牌元数据」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「令牌元数据」这节课中我会学到什么?
tokenURI 与 JSON 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「令牌元数据」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- ERC-721 标准
- 铸造 NFT
- 令牌元数据
- 市场