A client library for the IPFS HTTP API, implemented in JavaScript. This client library implements the interface-ipfs-core 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.
This module uses node.js, and can be installed through npm:
npm install --save ipfs-http-client
We support both the Current and Active LTS versions of Node.js. Please see nodejs.org for what these currently are.
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
var ipfsClient = require('ipfs-http-client')
// connect to ipfs daemon API server
var ipfs = ipfsClient('localhost', '5001', { protocol: 'http' }) // leaving out the arguments will default to these values
// or connect with multiaddr
var ipfs = ipfsClient('/ip4/127.0.0.1/tcp/5001')
// or using options
var ipfs = ipfsClient({ host: 'localhost', port: '5001', protocol: 'http' })
// or specifying a specific API path
var ipfs = ipfsClient({ host: '1.1.1.1', port: '80', 'api-path': '/ipfs/api/v0' })
const bitswap = require('ipfs-http-client/src/bitswap')('/ip4/127.0.0.1/tcp/5001')
bitswap.wantlist(key, (err) => {
// ...
})
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 unpkg CDN.
To always request the latest version, use the following:
<!-- loading the minified version -->
<script src="https://unpkg.com/ipfs-http-client/dist/index.min.js"></script>
<!-- loading the human-readable (not minified) version -->
<script src="https://unpkg.com/ipfs-http-client/dist/index.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
- set the CORS settings attribute to make anonymous requests to CDN
Example:
<script src="https://unpkg.com/[email protected]/dist/index.js"
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('localhost', '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()
In a web browser IPFS HTTP client (either browserified or CDN-based) might encounter an error saying that the origin is not allowed. This would be a CORS ("Cross Origin Resource Sharing") failure: IPFS servers are designed to reject requests from unknown domains by default. You can whitelist the domain that you are calling from by changing your ipfs config like this:
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://example.com"]'
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "POST", "GET"]'
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 = ipfsClient({
host: 'localhost',
port: 5001,
protocol: 'http',
headers: {
authorization: 'Bearer ' + TOKEN
}
})
js-ipfs-http-client
follows the spec defined byinterface-ipfs-core
, which concerns the interface to expect from IPFS implementations. This interface is a currently active endeavor. You can use it today to consult the methods available.
-
ipfs.add(data, [options], [callback])
ipfs.addPullStream([options])
ipfs.addReadableStream([options])
ipfs.addFromStream(stream, [callback])
ipfs.addFromFs(path, [options], [callback])
ipfs.addFromURL(url, [options], [callback])
ipfs.cat(ipfsPath, [options], [callback])
ipfs.catPullStream(ipfsPath, [options])
ipfs.catReadableStream(ipfsPath, [options])
ipfs.get(ipfsPath, [options], [callback])
ipfs.getPullStream(ipfsPath, [options])
ipfs.getReadableStream(ipfsPath, [options])
ipfs.ls(ipfsPath, [callback])
ipfs.lsPullStream(ipfsPath)
ipfs.lsReadableStream(ipfsPath)
-
MFS (mutable file system) specific
ipfs.files.cp([from, to], [callback])
ipfs.files.flush([path], [callback])
ipfs.files.ls([path], [options], [callback])
ipfs.files.mkdir(path, [options], [callback])
ipfs.files.mv([from, to], [callback])
ipfs.files.read(path, [options], [callback])
ipfs.files.readPullStream(path, [options])
ipfs.files.readReadableStream(path, [options])
ipfs.files.rm(path, [options], [callback])
ipfs.files.stat(path, [options], [callback])
ipfs.files.write(path, content, [options], [callback])
-
ipfs.object.data(multihash, [options], [callback])
ipfs.object.get(multihash, [options], [callback])
ipfs.object.links(multihash, [options], [callback])
ipfs.object.new([template], [callback])
ipfs.object.patch.addLink(multihash, DAGLink, [options], [callback])
ipfs.object.patch.appendData(multihash, data, [options], [callback])
ipfs.object.patch.rmLink(multihash, DAGLink, [options], [callback])
ipfs.object.patch.setData(multihash, data, [options], [callback])
ipfs.object.put(obj, [options], [callback])
ipfs.object.stat(multihash, [options], [callback])
-
refs
ipfs.refs.local()
-
log
ipfs.log.level(subsystem, level, [options], [callback])
ipfs.log.ls([callback])
ipfs.log.tail([callback])
Currently, the PubSub API only works in Node.js environment
We currently don't support pubsub when run in the browser, and we test it with separate set of tests to make sure if it's being used in the browser, pubsub errors.
More info: ipfs-inactive#518
This means:
- You can use pubsub from js-ipfs-http-client in Node.js
- You can use pubsub from js-ipfs-http-client in Electron (when js-ipfs-http-client is ran in the main process of Electron)
- You can't use pubsub from js-ipfs-http-client in the browser
- You can't use pubsub from js-ipfs-http-client in Electron's renderer process
- You can use pubsub from js-ipfs in the browsers
- You can use pubsub from js-ipfs in Node.js
- You can use pubsub from js-ipfs in Electron (in both the main process and the renderer process)
- See https://github.com/ipfs/js-ipfs for details on pubsub in js-ipfs
A set of data types are exposed directly from the IPFS instance under ipfs.types
. That way you're not required to import/require the following.
ipfs.types.Buffer
ipfs.types.PeerId
ipfs.types.PeerInfo
ipfs.types.multiaddr
ipfs.types.multibase
ipfs.types.multihash
ipfs.types.CID
Adding to the methods defined by interface-ipfs-core
, js-ipfs-http-client
exposes a set of extra utility methods. These utility functions are scoped behind the ipfs.util
.
Complete documentation for these methods is coming with: ipfs-inactive#305
ipfs.util.getEndpointConfig()
This returns an object containing the host
and the port
ipfs.util.crypto
This contains an object with the crypto primitives
ipfs.util.isIPFS
This contains an object with the is-ipfs utilities to help identifying IPFS resources
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.
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
. - Contribute to the FAQ repository with any questions you have about IPFS or any of the relevant technology. A good example would be asking, 'What is a merkledag tree?'. If you don't know a term, odds are, someone else doesn't either. Eventually, we should have a good understanding of where we need to improve communications and teaching together to make IPFS and IPN better.
Want to hack on IPFS?
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.