'Uploading metadata to IPFS using Pinata API for NFTs
I'm having an issue uploading my metadata to Pinata via their API. I've tried it in a test file and it works perfectly, but in my larger file, it's throwing an error. This is my Upload function below. I think the issue is when upload(metadata) calls createFile(data) it's saying that fs.writeFile is not a function (when I test that function directly). But for now the console.log(filehash) is logging undefined.
My guess is that I'll need to use the npm path module to solve this issue. But I'm not familiar with how to use it/how it works. But after console logging everywhere I could think of, the hangup seems to occur where the createFile is called. The file isn't being saved to the 'metadata' folder, so the upload function doesn't send the data to the API. Pinata authentication shows 'true' with no issues.
Upload.js:
const fs = require('fs')
const path = require('path')
const pinataSDK = require('@pinata/sdk')
require('dotenv').config()
const pinata = pinataSDK(
'0bb............e3',
'a0ad52...............446f79a0be'
)
const createFile = (data) => {
const jsonString = JSON.stringify(data)
fs.writeFile(`./metadata/${data.publishingHistory}.json`,
jsonString,
(err) => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('metadata file created successfully')
}
}
)
}
export const upload = async (metadata) => {
try {
const isAuth = await pinata.testAuthentication()
console.log(isAuth)
createFile(metadata)
const ipfsTransfer = await pinata.pinFileToIPFS(fs.createReadStream(`./metadata/${metadata.publishingHistory}.json`))
const filehash = `https://gateway.pinata.cloud/ipfs/${ipfsTransfer.IpfsHash}`
console.log(filehash)
return filehash
} catch (error) {}
}
My mintArtwork function:
const mintArtwork = async (data) => {
console.log(data)
const hashUrl = await upload(data)
console.log(hashUrl)
}
BTW, as you can see from my mintArtwork() function, there's no Web3 contract integration as of yet. One thing at a time. Just trying to get my ipfs to successfully connect to the Pinata API.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|