Tired of seeing NFTs with metadata pointing to a fragile AWS link? Let's fix that. In this tutorial, we'll use the Pinata SDK to upload a JSON metadata file to IPFS, the right way. No need to run your own IPFS Node.
Step 1: Get Your Pinata API Keys
Sign up for a free account on the Pinata website.
Navigate to the "API Keys" section in your account dashboard.
Create a new key. Make sure to copy the API Key and the API Secret somewhere safe.
Step 2: Set Up Your Node.js Project
In your project folder, install the Pinata SDK.
bash
npm install @pinata/sdk
Now, create a file (e.g., upload.js) and let's write some code.
Step 3: Write the Upload Script
This script will take a simple JSON object and "pin" it to IPFS.
JavaScript
const pinataSDK = require('@pinata/sdk');
const pinata = new pinataSDK('YOUR_API_KEY', 'YOUR_API_SECRET');
async function uploadMetadata() {
const metadata = {
name: "My Awesome NFT",
description: "This is a truly permanent NFT!",
image: "ipfs://CID_OF_YOUR_IMAGE_FILE" // <-- You'd upload the image first to get this!
};
try {
const result = await pinata.pinJSONToIPFS(metadata);
console.log("Successfully pinned metadata!");
console.log("IPFS Hash (CID):", result.IpfsHash);
console.log("View on Gateway:", `https://gateway.pinata.cloud/ipfs/${result.IpfsHash}`);
} catch (err) {
console.log(err);
}
}
uploadMetadata();
Run the script (node upload.js), and you'll get back an IPFS hash (a CID). That's the permanent link to your metadata. You now have a truly decentralized asset. The IPFS Protocol ensures its integrity, and Pinata ensures its availability.
This process is fundamental for building robust dApps. For a more in-depth walkthrough, the official community documentation is an excellent Guide that covers file uploads, folder management, and more.
Top comments (0)