javascriptipfsjs-ipfs

How to pin a hash for IPFS through Infura's gateway using ipfs-http-client API


The documentation shows the request to be in the following format:

curl -X POST "https://ipfs.infura.io:5001/api/v0/pin/add?arg=&progress="

I'm currently using JavaScript's API ipfs-http-client to make Http calls.

The add function from the source code doesn't seem to provide a way to indicate pinning:

module.exports = (options) => {
  const all = addAll(options)

  return configure(() => {
    return async function add (path, options = {}) { // eslint-disable-line require-await
      return last(all({
        path,
        ...options
      }, options))
    }
  })(options)
}

Update

I should've been clearer with my question. I mainly want to be able to "pin" my hashes on the IPFS node. There seem to be two methods that allow you to add your hash to the node (one without pinning, and the other with pinning), both of which can be invoked by:

const result = await ipfs.add(data)

I'm wonder how I should choose to add the hash and pin it.


Solution

  • You can add and pin in one operation:

    await ipfs.add(buf, {
      pin: true  // <-- this is the default
    })
    

    Or two:

    const { cid } = ipfs.add(buf, {
      pin: false
    })
    
    await ipfs.pin.add(cid)
    

    In a nutshell ipfs.add adds content to the repo of an IPFS node and returns the CID of that content and can pin during the add.

    ipfs.pin.add just pins a CID, which is useful if you don't have the content, but maybe want to help replicate it on the network. It will first check the repo to see if the content is present - if not it will find it on the network and add it to the repo then prevent it being garbage collected (a manual operation during which all non-pinned content is deleted from your repo).

    See the docs for ipfs.add and ipfs.pin.add for more information.