๐ง IPFS
IPFS (InterPlanetary File System) is a decentralized storage and content-addressed network. Files are identified by their content hash (CID), not by a server URL. Itโs widely used in Web3 for NFT metadata, static assets, and data that should be durable and censorship-resistant.
๐ฏ Why IPFS in Web3
- Content addressing โ Same content โ same CID; no broken links when hosts change
- Decentralized โ No single point of failure; anyone can pin and serve content
- NFT metadata โ ERC721 and Metaplex often point
tokenURI/ metadata to IPFS - dApp assets โ Frontend assets or data can be published on IPFS
๐ Core Concepts
| Term | Description |
|---|---|
| CID | Content Identifier โ hash of the content (e.g. QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco) |
| Pin | Store a copy of the content so it stays available; โpinningโ = keeping it |
| Gateway | HTTP endpoint that fetches IPFS content by CID (e.g. https://ipfs.io/ipfs/<CID>) |
| IPFS node | Client that can add, get, and pin content on the network |
๐ ๏ธ Using IPFS
Public gateways (read-only)
You can resolve CIDs via public HTTP gateways:
- ipfs.ioย โ
https://ipfs.io/ipfs/<CID> - dweb.linkย โ
https://dweb.link/ipfs/<CID> - Cloudflareย โ
https://cloudflare-ipfs.com/ipfs/<CID>
Example: https://ipfs.io/ipfs/QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco
Pinning services (upload & pin)
To add and pin content so it stays on the network, use a pinning service or run your own node:
- Pinataย โ Free tier, API, dashboard
- web3.storageย โ Free for developers, simple API
- NFT.Storageย โ Free for NFT metadata and assets
- IPFS documentationย
๐ Code: Upload and get CID (Node.js)
Using the official IPFS HTTP clientย or a pinning API:
// Example: Pinata API (replace with your key and secret)
const FormData = require("form-data");
const fs = require("fs");
const fetch = require("node-fetch");
async function uploadToPinata(filePath) {
const form = new FormData();
form.append("file", fs.createReadStream(filePath));
const res = await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.PINATA_JWT}`,
...form.getHeaders(),
},
body: form,
});
const data = await res.json();
return data.IpfsHash; // CID
}
// Usage
const cid = await uploadToPinata("./metadata.json");
console.log("CID:", cid);
console.log("URL:", `https://ipfs.io/ipfs/${cid}`);๐ NFT metadata on IPFS
A typical ERC721ย or Metaplexย metadata JSON is hosted on IPFS; the contract stores the URI (e.g. ipfs://Qm... or https://ipfs.io/ipfs/Qm...).
Example metadata.json:
{
"name": "My NFT",
"description": "Description",
"image": "ipfs://QmYourImageCID",
"attributes": [
{ "trait_type": "Background", "value": "Blue" }
]
}Upload this file to IPFS, then set your contractโs tokenURI to ipfs://<CID> or an HTTP gateway URL.