dabit3 / ipfs-pinata-example

Example function showing how to pin a file to Pinata from a JavaScript client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

An example of how to pin a file to IPFS with Pinata in a JavaScript app

If you're like me and had no luck finding any examples or code to show how this is done, then hopefully this will help you.

Pinning a string

async function save() {
  let ipfs = await IPFS.create({
    url: "https://api.pinata.cloud/psa",
    repo: 'file-path' + Math.random()
  })
  const { cid } = await ipfs.add('hello world')
  const url = `https://gateway.pinata.cloud/ipfs/${cid.string}`
  console.log(url)
}

Pinning a file

async function onChange(e) {
  const file = e.target.files[0]
  let ipfs = await IPFS.create({
    url: "https://api.pinata.cloud/psa",
    repo: 'file-path' + Math.random()
  })
  const { cid } = await ipfs.add(file)
  const url = `https://gateway.pinata.cloud/ipfs/${cid.string}`
  console.log(url)
}

About

Example function showing how to pin a file to Pinata from a JavaScript client