How to add internet content from a URL using the kubo-rpc-client and nodejs

Hello everyone,

You can use the kubo-rpc-client Javascript library to add internet content from a URL. In order to do so, please follow the below 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.
Add the content to your Infura IPFS project using the URL source of a file on the internet or the URL of an already uploaded file on IPFS.
Don’t forget to use your Project ID and Project Secret.

:bulb: Also keep in mind that the kubo client can only be imported as a module, so for simplicity save the below piece of code as an mjs file, index.mjs for example, before running it in node.

Still if you wish to use .js files instead .mjs you will need to add the top-level field "type" with a value of "module" in the nearest package.json file as explained here.
You can also create an empty package.json file in your project directory and add the below to get it working as a .js file.

import { urlSource, create } from 'kubo-rpc-client'
 
const projectId = '27…XXX';
const projectSecret = '20…XXX';
async function addUrl(){
  const auth =
    'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64')
 
    const client =  create({
        host: 'ipfs.infura.io',
        port: 5001,
        protocol: 'https',
        headers: {
          authorization: auth
        }
      })
 
     const file = await client.add(urlSource('https://ipfs.io/ipfs/QmTCparuvt3dub5U3uFocjrqXbGhKoYZmT5Kf6y9tjzhR9?filename=lime_cay.jpg'))
        console.log(file)
     
}
 
addUrl()
5 Likes