Hello everyone,
The kubo-rpc-client
JavaScript library can be used to list the objects pinned to your Infura IPFS account. In order to do so follow the next steps:
Install the library from the official kubo-rpc-client repository. Note that if you’ve previously used the ipfs-http-client
to upload content to IPFS, it was deprecated and we recommend using the kubo
client going forward.
Declare the IPFS client and use the below piece of code to list the pinned objects to your local storage.
Don’t forget to use your Project ID and Project Secret.
Also keep in mind that the kubo
client can only be imported as a module, so for simplicity save the below as an mjs
file, index.mjs for example, before running it in node
.
import { create, CID} from 'kubo-rpc-client'
const projectId = '27h....xxxx....Rsj';
const projectSecret = '205....xxxx....15b';
async function listCids(){
const auth =
'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64')
const client = await create({
host: 'ipfs.infura.io',
port: 5001,
protocol: 'https',
headers: {
authorization: auth
}
})
for await (const { cid, type } of client.pin.ls()) {
console.log({ cid, type })
}
}
listCids()
The output of the code run will display the pinned CIDs, including their type: recursive, direct or indirect.
The pin.ls
api supports the type
object, which can be used to filter the pins by their type: “recursive”, “direct” or “indirect”.
So, if you’d like to get just the recursive pins, use the type
object with the recursive
key as per the below example:
for await (const { cid, type } of client.pin.ls({type: 'recursive'})) {
console.log({ cid, type })
}
Another useful feature supported by the pin.ls
api is the paths
object, which comes in handy when searching for a CID in the pinset, especially if you have a multitude of CIDs pinned to your local storage and you aren’t sure if a particular one is pinned or not. See the below example for this:
for await (const { cid, type } of client.pin.ls({paths: [ CID.parse('QmehKEbtk4wHihtfB48VF2WC6Sis3u1Ad2tbUsSUg5mmWH') ]})) {
console.log({ cid, type })
}
The above code will return the CID together with its type, if the object is pinned to your local pinset.
{
cid: CID(QmehKEbtk4wHihtfB48VF2WC6Sis3u1Ad2tbUsSUg5mmWH),
type: 'recursive'
}
Or it will return an error similar to the below one if the object is not pinned to your account:
HTTPError: path 'QmehKEbtk4wHihtfBs8VF2WC6Sis3u1Ad2tbUsSUg5mmWH' is not pinned
You can use
node
to write the results of your pin.ls
directly to a file. Let’s assume that the name of the file containing your code is pinls.js
and that you want to create a log file containing all your recursive pins. Use the {type: ‘recursive’} filter, as explained earlier, change directory to the folder where pinls.js
is located and then run the below command from your terminal window.
node pinls.js > recursivepins.log 2>&1
The results will be written in the recursivepins.log
file, while if you hit an error this file will contain the error details.