Coderrob / pinata-ipfs-scripts-for-nft-projects

Node scripts using the Pinata SDK to upload files and download the CIDs of already uploaded files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pinata IPFS scripts for NFT projects

Buy Me A Coffee

Scripts

The scripts contained in this repository were created to help automate the import and processing of NFT profile pic projects. Each script serves a unique purpose and when combined will help import both NFT images and the associated image metadata. A brief description of the scripts is below:

  • calculate-cids.js - calculate the IPFS hash CID for every file in a specified folder
  • calculate-hashes.js - calculate the sha256 hash for every file in a specified folder
  • download-cids.js - downloads every pinned file from Pinata for an API account
  • upload-files.js - uploads the contents of a specified folder and pins each individual file in Pinata
  • upload-folder.js - uploads the contents of a specified folder and pins the folder container and its contents

Getting Started

Clone the repository.

git clone https://github.com/Coderrob/pinata-ipfs-scripts-for-nft-projects.git

Change directory to the pinata-ipfs-scripts-for-nft-projects folder.

cd pinata-ipfs-scripts-for-nft-projects

Install dependencies.

npm install

Some scripts require environment variables to connect with the Pinata API. These environment variables are needed to download pinned files, or to upload files and folders.

Environment Variables

PINATA_API_KEY - The Pinata API Key environment variable

PINATA_API_SECRET - The Pinata API Secret environment variable

The repo is setup with dotenv and configured to allow using an .env file to run the scripts.

If the env file does not already exist simply create a new .env file at the root of the repository.

The contents of the .env file should look similar to this:

PINATA_API_KEY="a1237a8dcd87766ff4"
PINATA_API_SECRET="fb8654309ca8777asdf7558758123456asdf817166927aknnk888877"

To generate these Pinata API keys you'll need to follow the Getting Started Pinata documentation

Calculate File IPFS CIDs

/src/calculate-cids.js

The calculate file CIDs script will iterate the contents of a specified folder, and for each file will compute the IPFS hash CID mapped to the file name.

Once complete the script will output the file name and CID mappings to a file.

Settings

var: OUTPUT_PATH - The relative output file path. Defaulted to ./output/file-cids.json.

var: FOLDER_PATH - The relative folder path containing the files to be processed. Each file will have its name and CID mapped. Defaulted to the files folder.

Command

node ./src/calculate-cids.js

Output

./output/file-cids.json

Contents

{
  "one.png": "QmZPnX4481toHABEtvKFoCWoVuzFFQRBiA5QR2Cij9pjon",
  "two.png": "QmazpAaWf3Bb4qhSW9PnQXfj2URbQwdNbZvDr77RbwH7xb"
}

Calculate File sha256 Hashes

/src/calculate-hashes.js

The calculate file hashes script will iterate the contents of a specified folder, and for each file will compute the sha256 hash mapped to the file name.

Once complete the script will output the file name and sha256 hash mappings to a file.

Settings

var: OUTPUT_PATH - The relative output file path. Defaulted to ./output/file-hashes.json.

var: FOLDER_PATH - The relative folder path containing the files to be processed. Each file will have its name and CIDsha256 hash mapped. Defaulted to the files folder.

Command

node ./src/calculate-hashes.js

Output

./output/file-hashes.json

Contents

{
  "one.png": "f8e50b5c45e6304b41f87686db539dd52138b873a3af98cc60f623d47a133df2",
  "two.png": "76d9c6f8dc113fff71a180195077526fce3d0279034a37f23860c1f519512e94"
}

Download Pinata Pinned CIDs

/src/download-cids.js

The download file CIDs script will iterate all pinned files associated with the Pinata API Key. The script will map each row's file name and IPFS hash CID.

Once complete the script will output the file name and CID mappings to a file.

Settings

var: OUTPUT_PATH - The relative output file path. Defaulted to ./output/downloaded-cids.json

env: PINATA_API_KEY - The Pinata API Key environment value

evn: PINATA_API_SECRET - The Pinata API Secret environment value

Command

node ./src/download-cids.js

Output

./output/downloaded-cids.json

Contents

{
  "one.png": "QmZPnX4481toHABEtvKFoCWoVuzFFQRBiA5QR2Cij9pjon",
  "two.png": "QmazpAaWf3Bb4qhSW9PnQXfj2URbQwdNbZvDr77RbwH7xb"
}

Upload Files

/src/upload-files.js

The upload files script will iterate the contents of a specified folder and will upload and pin each individual file to Pinata. After a successful upload the file name will be mapped to the IPFS hash CID from the response.

Once complete the script will output the file name and CID mappings to a file.

Settings

var: pinataCIDs - To prevent re-uploading already pinned files in Pinata. This variable is loaded with the json contents of the ./ouput/downloaded-cids.json file if one exists. These CID mappings will help prevent re-uploading a file that has already been pinned in Pinata.

var: OUTPUT_PATH - The relative output file path. Defaulted to ./output/uploaded-cids.json.

var: FOLDER_PATH - The relative folder path to read and upload all local files to be pinned with Pinata. Defaulted to the files folder.

env: PINATA_API_KEY - The Pinata API Key environment value

env: PINATA_API_SECRET - The Pinata API Secret environment value

Command

node ./src/upload-files.js

Output

./output/uploaded-cids.json

Contents

{
  "one.png": "QmZPnX4481toHABEtvKFoCWoVuzFFQRBiA5QR2Cij9pjon",
  "two.png": "QmazpAaWf3Bb4qhSW9PnQXfj2URbQwdNbZvDr77RbwH7xb"
}

Upload Folder

/src/upload-folder.js

The upload folder script will iterate the contents of a specified folder and will upload and pin each file under a folder container in Pinata. After a successful upload the folder name will be mapped to the IPFS hash CID from the response.

Once complete the script will output the folder name and CID mapping to a file.

Note - To support ipfs/<CID>/<TokenId> such as ipfs/QmR5m9zJDSmrLnYMawrySYu3wLgN5afo3yizevAaimjvmD/0 simply name the JSON files with numeric names and strip the file extensions. This will allow the files to be accessed by a numeric file name that can be easily mapped to the TokenId.

Pinata pinned file list

metadata folder container list

File 0 metadata json

Settings

var: OUTPUT_PATH - The relative output file path. Defaulted to ./output/folder-cid.json.

var: FOLDER_NAME - The folder name to use for the uploaded folder of json metadata. This can be changed to any name you'd like that identifies the collection of metadata files. Defaulted to metadata as the folder name.

var: FOLDER_PATH - The relative folder path to read and upload all local files to be pinned in Pinata as a folder container for the uploaded files. Defaulted to the metadata folder.

env: PINATA_API_KEY - The Pinata API Key environment value

env: PINATA_API_SECRET - The Pinata API Secret environment value

Command

node ./src/upload-folder.js

Output

./output/folder-cid.json

Contents

{ 
    "metadata": "QmR5m9zJDSmrLnYMawrySYu3wLgN5afo3yizevAaimjvmD" 
}

About

Node scripts using the Pinata SDK to upload files and download the CIDs of already uploaded files.

License:MIT License


Languages

Language:JavaScript 100.0%