Uploading Metadata to IPFS
Learn how to upload your NFT media and metadata to a decentralized storage platform (IPFS) for secure and reliable storage.
This guide uses JavaScript examples, but the process works with any programming language. By the end, you'll have your NFT media and metadata securely hosted on IPFS, ready to be referenced in your future mints.
Prerequisites
- An IPFS pinning service (e.g., Pinata).
- Your NFT media files (images, videos, etc.) and metadata descriptions in JSON format ready.
Full Code Example
The code corresponding to this example, which uses Pinata as IPFS service, can be found and executed here.
Running the example
- Create a
.envfile with your Pinata API key and secret:
PINATA_API_KEY=your_api_key_here
PINATA_API_SECRET=your_api_secret_here
Keep this info secure and never share it or commit it to version control.
- Install dependencies (if not done before):
$ npm ci
- Define the image and metadata for your NFT. The example provided in the code is:
const imagePath = 'imgs/weapon.jpg';
const imageHash = await uploadToIPFS(imagePath);
const metadata = {
name: 'Your NFT Name',
description: 'Description of your NFT',
image: `ipfs://${imageHash}`,
attributes: [
{ trait_type: 'Attr 1', value: 'Value 1' },
{ trait_type: 'Attr 2', value: 'Value 2' },
],
};
- Run the script to upload files:
$ node ipfs-uploader.js
- Verify your uploads:
-
Check the IPFS hashes (CIDs) returned by the script. You will see:
-
Confirm that both CIDs are propagated across the decentralized IPFS network. For example, they should eventually be accessible using the popular public
ipfs.iogateway at:https://ipfs.io/ipfs/<YOUR_CID>.
Next Steps
With the data already hosted on IPFS, everything is ready to send the mint transaction, where the minted NFTs’ tokenURI will point to the IPFS metadata CID.