on-chain metadata
On-chain metadata means the NFT's data, and often the artwork itself, are stored directly in the smart contract rather than pointed to by an external link. When tokenURI is called, the contract builds the JSON (and sometimes an SVG image) on the fly in code and returns it as a data: URI with the content base64-encoded inline. Nothing lives on a web server or even on IPFS; the picture is literally reconstructed from bytes the blockchain stores, so it survives exactly as long as the chain does.
The appeal is permanence and self-sufficiency. A fully on-chain NFT cannot 'rug' by having its images deleted, its hosting expire, or its IPFS pin dropped, because there is no off-chain dependency to break. This is why projects that prize longevity — Autoglyphs, Chromie Squiggle, Nouns, Loot — generate their art deterministically from on-chain code or seed data. It also enables genuinely composable and dynamic art, since other contracts can read and remix the underlying data.
The cost is, quite literally, gas and size. Storing bytes on-chain is expensive and blocks have limits, so on-chain art tends to be generative, vector (SVG), or highly compressed rather than a multi-megabyte photograph. Techniques like SSTORE2 (writing data as contract bytecode to read more cheaply) and procedural generation make it feasible. The trade-off is stark: off-chain storage is cheap but fragile, on-chain storage is costly but as durable as the network itself.
function tokenURI(uint256 id) public view returns (string memory) {
string memory svg = _renderSVG(id); // build art in code
string memory json = string.concat(
'{"name":"Glyph #', id.toString(),
'","image":"data:image/svg+xml;base64,', Base64.encode(bytes(svg)), '"}');
return string.concat('data:application/json;base64,', Base64.encode(bytes(json)));
}tokenURI assembling JSON and SVG entirely on-chain, no external link.
'On-chain' is a spectrum: some NFTs put only the JSON on-chain but the image off-chain, others put both on-chain. Truly tamper-proof art needs the image itself, not just its metadata, stored on-chain.