IPFS HTTP Client Lite
ipfs-http-client-lite is a lightweight JavaScript client library designed to interact with the IPFS HTTP API. Its primary differentiator is its small bundle size, aiming for less than 20KB, making it suitable for resource-constrained browser environments. The current stable version is 0.3.0. Based on its release history, which includes incremental feature additions like MFS commands and pubsub, the project demonstrates an active, albeit not rapid, release cadence. It provides a focused alternative to the more comprehensive `ipfs-http-client` for applications where bundle size and minimal footprint are critical, supporting core IPFS operations such as adding and retrieving files, and interacting with the Mutable File System.
Common errors
-
Access to fetch at 'http://localhost:5001/api/v0/...' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
cause The IPFS daemon is rejecting requests from your web application's origin due to CORS security policies.fixConfigure the IPFS daemon's CORS settings: `ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://localhost:3000", "http://127.0.0.1:3000"]'` and `ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "POST", "GET"]'`. Remember to restart the IPFS daemon after making configuration changes. -
Error: connect ECONNREFUSED 127.0.0.1:5001
cause The ipfs-http-client-lite library could not connect to the IPFS daemon at the specified address and port. This usually means the daemon is not running, or it's running on a different port/address.fixStart your IPFS daemon using `ipfs daemon`. If it's already running, verify its API address and port with `ipfs config Addresses.API` and ensure it matches the `apiUrl` configured in your `IpfsHttpClientLite` client. -
TypeError: ipfs.add is not a function
cause This error occurs if you are trying to call a method like `add` directly on the `ipfs` object obtained from `IpfsHttpClientLite` when you might be expecting specific method exports, or if the client wasn't initialized correctly.fixEnsure you are using the correct method signature. `ipfs.add` expects `Buffer`, `Uint8Array`, or `ReadableStream` as input. If you imported specific methods, ensure `add` was among them and correctly aliased, or use the `IpfsHttpClientLite` factory function which returns an object with all available API methods.
Warnings
- gotcha When using ipfs-http-client-lite in a web browser, Cross-Origin Resource Sharing (CORS) issues commonly arise. IPFS daemons by default reject requests from unknown domains, leading to 'origin not allowed' errors.
- breaking The library explicitly states support for Node.js Current and Active LTS versions. Using older Node.js versions (e.g., <10.0.0) may lead to unexpected errors or compatibility issues.
- gotcha When importing individual methods for smaller bundle sizes (e.g., `ipfs-http-client-lite/src/cat`), these 'src' imports are not part of the public API and may change paths or internal structures in minor or patch releases, potentially breaking applications.
- gotcha The library relies on an external IPFS daemon running. If the daemon is not running or is configured on a different port than specified in the client, network connection errors will occur.
Install
-
npm install ipfs-http-client-lite -
yarn add ipfs-http-client-lite -
pnpm add ipfs-http-client-lite
Imports
- IpfsHttpClientLite
const IpfsHttpClientLite = require('ipfs-http-client-lite')import IpfsHttpClientLite from 'ipfs-http-client-lite'
- cat
const cat = require('ipfs-http-client-lite').catimport cat from 'ipfs-http-client-lite/src/cat'
- add
import add from 'ipfs-http-client-lite/src/add'
Quickstart
import IpfsHttpClientLite from 'ipfs-http-client-lite';
// Configure the IPFS client. Ensure your IPFS daemon is running on port 5001.
// You can also add custom headers, e.g., for authorization.
const ipfs = IpfsHttpClientLite({
apiUrl: 'http://localhost:5001',
headers: {
Authorization: `Bearer ${process.env.IPFS_AUTH_TOKEN ?? ''}` // Example for authentication
}
});
async function runExample() {
try {
// Add content to IPFS
const content = Buffer.from('Hello from ipfs-http-client-lite!');
const addResult = await ipfs.add(content);
console.log('Added content CID:', addResult.cid.toString());
// Retrieve content from IPFS
const retrievedData = await ipfs.cat(addResult.cid.toString());
console.log('Retrieved content:', retrievedData.toString());
// Example of using MFS (Mutable File System) commands
const mfsPath = '/my-directory/my-file.txt';
await ipfs.files.mkdir('/my-directory', { parents: true });
await ipfs.files.write(mfsPath, content, { create: true });
console.log(`Wrote content to MFS path: ${mfsPath}`);
const mfsContent = await ipfs.files.read(mfsPath);
console.log('Content from MFS:', mfsContent.toString());
} catch (error) {
console.error('An error occurred:', error);
console.warn('Ensure your IPFS daemon is running and accessible at http://localhost:5001.');
console.warn('If in a browser, check CORS settings on your IPFS daemon.');
}
}
runExample();