Skip to content

A NodeJS package that makes interacting with IPFS easier

License

Notifications You must be signed in to change notification settings

MakerXStudio/node-ipfs

Repository files navigation

Node IPFS (node-ipfs)

A NodeJS package that makes interacting with IPFS easier

Build Status Issues Semantic Release

Install

npm install @makerx/node-ipfs @makerx/node-cache

Note

This package is only compatible with Node 18 and above, as it uses the native node fetch.

Usage

The primary purpose of this package is to make reading and writing files on the IPFS network easier. Unfortunately IPFS can be slow, so to mitigate this some cache implementations have also been supplied.

import { S3 } from '@aws-sdk/client-s3'
import { PinataStorageWithCache } from '@makerx/node-ipfs'
import { S3ObjectCache } from '@makerx/node-cache'

const s3Cache = new S3ObjectCache(
  new S3({
    region: process.env.AWS_REGION,
  }),
  'CACHE_BUCKET_NAME',
  'cache/ipfs/',
)

const ipfs = new PinataStorageWithCache('PINATA_STORAGE_API_JWT', s3Cache)

// Put blob to IPFS
const myImage = new Uint8Array([104, 101, 108, 108, 111])
const { cid } = await ipfs.putBlob(myImage, 'image/png', 'my_file.png')

// Put json to IPFS
const { cid } = await ipfs.put({ hello: 'world' }, 'my_file.json')

// Get blob by cid from cache with pass through to IPFS
const myImage = await ipfs.getBlob('cid')

// Get json by cid from cache with pass through to IPFS
const json = await ipfs.get('cid')

// Get cid of blob
const myImage = new Uint8Array([104, 101, 108, 108, 111])
const cid = await ipfs.getCID(myImage)

// Get cid of json
const cid = await ipfs.getCID({ hello: 'world' })

Why Pinata?

We use Pinata to upload and pin files on the IPFS network. We previously used web3.storage, however they decided to implement a more complex upload API and sunset their old API, which makes integration a lot more complex.

In order to use this library you'll need to signup for a free account on Pinata and generate a JWT to access the API.