Kubo RPC Client
The `kubo-rpc-client` package provides a JavaScript client library for interacting with the Kubo RPC API, allowing developers to programmatically control and query an IPFS Kubo node. It enables functionalities such as adding and retrieving data, managing pins, and interacting with the IPFS swarm. Currently at version 6.1.0, the library maintains a regular release cadence, incorporating new features like `provide.stat` and `pin.update`, and ensuring interoperability with recent Kubo versions (e.g., Kubo 0.38). It differentiates itself as the official client for Kubo's HTTP RPC API, offering a direct interface to the underlying IPFS daemon's capabilities, contrasting with higher-level IPFS client libraries that might embed or abstract away the RPC communication. It supports both Node.js (Current and Active LTS versions) and browser environments, providing a consistent API across platforms.
Common errors
-
Error: connect ECONNREFUSED 127.0.0.1:5001
cause The Kubo IPFS daemon is not running or is not accessible at the specified address and port.fixStart your IPFS Kubo daemon (e.g., `ipfs daemon`) and ensure its API address matches the `url` option provided to `kubo-rpc-client.create()`. -
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax in a JavaScript module that is treated as an ES module (e.g., in a package with `"type": "module"` or a `.mjs` file).fixRefactor your imports to use ES module syntax: `import { create } from 'kubo-rpc-client'`. If running in Node.js, ensure your environment supports ESM, or configure your project to transpile to CJS if necessary. -
TypeError: client.add is not a function
cause The `create` function did not successfully return a client instance, or the client instance is not correctly typed/handled.fixVerify that the `create` call is not throwing an error and that `client` is indeed the object returned by `create`. Ensure you are using `await` with `client.add()` and other async operations, as they return Promises.
Warnings
- breaking Version 6.0.0 introduced breaking changes related to its internal dependencies. It now requires compatibility with the latest versions of `@libp2p/*` and `@multiformats/multiaddr` packages.
- gotcha The `create` function defaults to `http://localhost:5001/api/v0`. If your Kubo node is running on a different address or port, or is not running at all, the client will fail to connect.
- gotcha When running `kubo-rpc-client` in a browser environment, be aware of browser security policies (e.g., CORS). Direct connections to a local Kubo RPC API might be blocked if the client is served from a different origin.
Install
-
npm install kubo-rpc-client -
yarn add kubo-rpc-client -
pnpm add kubo-rpc-client
Imports
- create
const { create } = require('kubo-rpc-client')import { create } from 'kubo-rpc-client' - IPFSClient
import type { IPFSClient } from 'kubo-rpc-client' - KuboRpcClient
import { KuboRpcClient } from 'kubo-rpc-client'<!-- In browser HTML --> <script src="https://unpkg.com/kubo-rpc-client/dist/index.min.js"></script> <!-- Then use global --> KuboRpcClient.create()
Quickstart
import { create } from 'kubo-rpc-client'
async function run() {
try {
// Connect to the local Kubo RPC API (default: http://localhost:5001)
const client = create({
url: process.env.IPFS_API_URL ?? 'http://localhost:5001/api/v0'
})
// Get the IPFS node ID
const { id, agentVersion, protocolVersion } = await client.id()
console.log('Connected to IPFS node:')
console.log(` ID: ${id}`)
console.log(` Agent Version: ${agentVersion}`)
console.log(` Protocol Version: ${protocolVersion}`)
// Add a simple string to IPFS
const data = 'Hello from kubo-rpc-client!'
const result = await client.add(data)
console.log(`Added data to IPFS: ${result.path}`)
console.log(`Content ID (CID): ${result.cid}`)
// Retrieve the data by its CID
const retrievedBytes = client.cat(result.path)
let retrievedData = ''
for await (const chunk of retrievedBytes) {
retrievedData += new TextDecoder().decode(chunk)
}
console.log(`Retrieved data: "${retrievedData}" (CID: ${result.cid})`)
} catch (error) {
console.error('Error interacting with IPFS:', error)
}
}
run()