npm install ipfs-http-client
Initialise the client object
const projectId = process.env.INFURA_PROJECTID
const projectSecret = process.env.INFURA_PROJECTSECRET
const auth =
'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64')
const client = create({
host: 'ipfs.infura.io',
protocol: 'https',
port: 5001,
headers: {
authorization: auth,
},
})
function uploadToIpfs = async (data: any) => {
try {
const { cid } = await client.add({ content: JSON.stringify(data) })
} catch (error: any) {
console.log('error uploading data:', error)
}
}