Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Latest commit

 

History

History
120 lines (80 loc) · 4.63 KB

README.md

File metadata and controls

120 lines (80 loc) · 4.63 KB

IPFS API wrapper library in JavaScript

Coverage Status Dependency Status Travis CI Circle CI

A client library for the IPFS API.

Usage

Installing the module

In Node.js Through npm

$ npm install --save ipfs-api

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

var ipfsAPI = require('ipfs-api')

// connect to ipfs daemon API server
var ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'}) // leaving out the arguments will default to these values

// or connect with multiaddr
var ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/5001')

// or using options
var ipfs = ipfsAPI({host: 'localhost', port: '5001', procotol: 'http'})

In the 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.

In the Browser through <script> tag

You can use npmcdn to get the latest built version, like this

<script src="https://npmcdn.com/ipfs-api/dist/index.js"></script>

This will export the IpfsApi constructor on the window object, such that:

var ipfs = window.IpfsApi('localhost', '5001')

If you omit the host and port, the api 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:

var ipfs = window.IpfsApi()

CORS

If are using this module in a browser with something like browserify, then you will get an error saying that the origin is not allowed. This would be a CORS ("Cross Origin Resource Sharing") failure. The ipfs server rejects 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\"]"

Usage

See API.md and tests/api for details on available methods.

Callbacks and promises

If you do not pass in a callback all API functions will return a Promise. For example:

ipfs.id()
  .then(function (id) {
    console.log('my id is: ', id)
  })
  .catch(function(err) {
  	console.log('Fail: ', err)
  })

This relies on a global Promise object. If you are in an environment where that is not yet available you need to bring your own polyfill.

Contribute

The js-ipfs API 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.
  • 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.

License

MIT.

Want to hack on IPFS?