npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@chris.troutner/ipfs-http-client

v56.0.1

Published

A client library for the IPFS HTTP API

Downloads

5

Readme

A client library for the IPFS HTTP RPC API (/api/v0/*), implemented in JavaScript. This client library implements the IPFS Core API enabling applications to change between an embedded js-ipfs node and any remote IPFS node without having to change the code. In addition, this client library implements a set of utility functions.

Table of Contents

Getting Started

We've come a long way, but this project is still in Alpha, lots of development is happening, APIs might change, beware of 🐉..

Install

This module uses node.js, and can be installed through npm:

npm install --save ipfs-http-client

Both the Current and Active LTS versions of Node.js are supported. Please see nodejs.org for what these currently are.

Next Steps

  • Read the docs
  • Look into the examples to learn how to spawn an IPFS node in Node.js and in the Browser
  • Consult the Core API docs to see what you can do with an IPFS node
  • Visit https://dweb-primer.ipfs.io to learn about IPFS and the concepts that underpin it
  • Head over to https://proto.school to take interactive tutorials that cover core IPFS APIs
  • Check out https://docs.ipfs.io for tips, how-tos and more
  • See https://blog.ipfs.io for news and more
  • Need help? Please ask 'How do I?' questions on https://discuss.ipfs.io

Usage

create([options])

create an instance of the HTTP API client

Parameters

None

Options

options can be a String, a URL or a Multiaddr which will be interpreted as the address of the IPFS node we wish to use the API of.

Alternatively it can be an object which may have the following keys:

| Name | Type | Default | Description | | ---- | ---- | ------- | ----------- | | url | String or URL or Multiaddr | 'http://localhost:5001/api/v0' | A URL that resolves to a running instance of the IPFS HTTP RPC API | | protocol | String | 'http' | The protocol to used (ignored if url is specified) | | host | String | 'localhost' | The host to used (ignored if url is specified) | | port | number | 5001 | The port to used (ignored if url is specified) | | path | String | 'api/v0' | The path to used (ignored if url is specified) | | agent | http.Agent | http.Agent({ keepAlive: true, maxSockets: 6 }) | An http.Agent used to control client behaviour (node.js only) |

Returns

| Type | Description | | ---- | ----------- | | Object | An object that conforms to the IPFS Core API |

Example

import { create } from 'ipfs-http-client'

// connect to the default API address http://localhost:5001
const client = create()

// connect to a different API
const client = create('http://127.0.0.1:5002')

// connect using a URL
const client = create(new URL('http://127.0.0.1:5002'))

// call Core API methods
const { cid } = await client.add('Hello world!')

API

IPFS Core API Compatible

js-ipfs-http-client implements the IPFS Core API - please follow the previous link to see the methods available.

Additional Options

All core API methods take additional options specific to the HTTP API:

  • headers - An object or Headers instance that can be used to set custom HTTP headers. Note that this option can also be configured globally via the constructor options.
  • searchParams - An object or URLSearchParams instance that can be used to add additional query parameters to the query string sent with each request.

Instance Utils

  • ipfs.getEndpointConfig()

Call this on your client instance to return an object containing the host, port, protocol and api-path.

Static Types and Utils

Aside from the default export, ipfs-http-client exports various types and utilities that are included in the bundle:

These can be accessed like this, for example:

const { CID } = require('ipfs-http-client')
// ...or from an es-module:
import { CID } from 'ipfs-http-client'

Glob source

A utility to allow files on the file system to be easily added to IPFS.

globSource(path, pattern, [options])
  • path: A path to a single file or directory to glob from
  • pattern: A pattern to match files under path
  • options: Optional options
  • options.hidden: Hidden/dot files (files or folders starting with a ., for example, .git/) are not included by default. To add them, use the option { hidden: true }.

Returns an async iterable that yields { path, content } objects suitable for passing to ipfs.add.

Example
import { create, globSource } from 'ipfs'

const ipfs = await create()

for await (const file of ipfs.addAll(globSource('./docs', '**/*'))) {
  console.log(file)
}
/*
{
  path: 'docs/assets/anchor.js',
  cid: CID('QmVHxRocoWgUChLEvfEyDuuD6qJ4PhdDL2dTLcpUy3dSC2'),
  size: 15347
}
{
  path: 'docs/assets/bass-addons.css',
  cid: CID('QmPiLWKd6yseMWDTgHegb8T7wVS7zWGYgyvfj7dGNt2viQ'),
  size: 232
}
...
*/

URL source

A utility to allow content from the internet to be easily added to IPFS.

urlSource(url)
  • url: A string URL or URL instance to send HTTP GET request to

Returns an async iterable that yields { path, content } objects suitable for passing to ipfs.add.

Example
import { create, urlSource } from 'ipfs-http-client'
const ipfs = create()

const file = await ipfs.add(urlSource('https://ipfs.io/images/ipfs-logo.svg'))
console.log(file)

/*
{
  path: 'ipfs-logo.svg',
  cid: CID('QmTqZhR6f7jzdhLgPArDPnsbZpvvgxzCZycXK7ywkLxSyU'),
  size: 3243
}
*/

Running the daemon with the right port

To interact with the API, you need to have a local daemon running. It needs to be open on the right port. 5001 is the default, and is used in the examples below, but it can be set to whatever you need.

# Show the ipfs config API port to check it is correct
> ipfs config Addresses.API
/ip4/127.0.0.1/tcp/5001
# Set it if it does not match the above output
> ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001
# Restart the daemon after changing the config

# Run the daemon
> ipfs daemon

Importing the module and usage

import { create } from 'ipfs-http-client'

// connect to ipfs daemon API server
const ipfs = create('http://localhost:5001') // (the default in Node.js)

// or connect with multiaddr
const ipfs = create('/ip4/127.0.0.1/tcp/5001')

// or using options
const ipfs = create({ host: 'localhost', port: '5001', protocol: 'http' })

// or specifying a specific API path
const ipfs = create({ host: '1.1.1.1', port: '80', apiPath: '/ipfs/api/v0' })

In a web browser

through Browserify

Same as in Node.js, you just have to browserify the code before serving it. See the browserify repo for how to do that.

See the example in the examples folder to get a boilerplate.

through webpack

See the example in the examples folder to get an idea on how to use js-ipfs-http-client with webpack.

from CDN

Instead of a local installation (and browserification) you may request a remote copy of IPFS API from jsDelivr.

To always request the latest version, use one of the following examples:

<!-- loading the minified version using jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/ipfs-http-client/dist/index.min.js"></script>

For maximum security you may also decide to:

  • reference a specific version of IPFS API (to prevent unexpected breaking changes when a newer latest version is published)
  • generate a SRI hash of that version and use it to ensure integrity. Learn more also at the jsdelivr website
  • set the CORS settings attribute to make anonymous requests to CDN

Example:

<script
  src="https://www.jsdelivr.com/package/npm/ipfs-http-client"
  integrity="sha384-5bXRcW9kyxxnSMbOoHzraqa7Z0PQWIao+cgeg327zit1hz5LZCEbIMx/LWKPReuB"
  crossorigin="anonymous"
></script>

CDN-based IPFS API provides the IpfsHttpClient constructor as a method of the global window object. Example:

const ipfs = window.IpfsHttpClient({ host: 'localhost', port: 5001 })

If you omit the host and port, the client will parse window.host, and use this information. This also works, and can be useful if you want to write apps that can be run from multiple different gateways:

const ipfs = window.IpfsHttpClient()

Custom Headers

If you wish to send custom headers with each request made by this library, for example, the Authorization header. You can use the config to do so:

const ipfs = create({
  host: 'localhost',
  port: 5001,
  protocol: 'http',
  headers: {
    authorization: 'Bearer ' + TOKEN
  }
})

Global Timeouts

To set a global timeout for all requests pass a value for the timeout option:

// Timeout after 10 seconds
const ipfs = create({ timeout: 10000 })
// Timeout after 2 minutes
const ipfs = create({ timeout: '2m' })
// see https://www.npmjs.com/package/parse-duration for valid string values

Development

Testing

We run tests by executing npm test in a terminal window. This will run both Node.js and Browser tests, both in Chrome and PhantomJS. To ensure that the module conforms with the interface-ipfs-core spec, we run the batch of tests provided by the interface module, which can be found here.

Contribute

The js-ipfs-http-client is a work in progress. As such, there's a few things you can do right now to help out:

  • Check out the existing issues!
  • Perform code reviews. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
  • Add tests. There can never be enough tests. Note that interface tests exist inside interface-ipfs-core.

Want to hack on IPFS?

Historical context

This module started as a direct mapping from the go-ipfs cli to a JavaScript implementation, although this was useful and familiar to a lot of developers that were coming to IPFS for the first time, it also created some confusion on how to operate the core of IPFS and have access to the full capacity of the protocol. After much consideration, we decided to create interface-ipfs-core with the goal of standardizing the interface of a core implementation of IPFS, and keep the utility functions the IPFS community learned to use and love, such as reading files from disk and storing them directly to IPFS.

License

MIT

FOSSA Status