IPFS

Sample code for integrating IPFS into a Typescript frontend client.

Install dependencies

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,
  },
})
 

Upload data to IPFS

function uploadToIpfs = async (data: any) => {
    try {
      const { cid } = await client.add({ content: JSON.stringify(data) })
    } catch (error: any) {
      console.log('error uploading data:', error)
    }
 } 

Last updated