IPFS HTTP Client Lite

0.3.0 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the IPFS client, add content, retrieve it using `cat`, and interact with the Mutable File System (MFS) by creating a directory and writing/reading a file, including error handling.

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();

view raw JSON →