JSPM

  • Created
  • Published
  • Downloads 11947
  • Score
    100M100P100Q14893F
  • License MIT

A client library for the IPFS API

Package Exports

  • ipfs-api
  • ipfs-api/dist/ipfsapi.min.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ipfs-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

IPFS API wrapper library in JavaScript

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
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

Make the ipfsapi.min.js available through your server and load it using a normal <script> tag, 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()

Gotchas

When using the api from script tag for things that require buffers (ipfs.add, for example), you will have to use either the exposed ipfs.Buffer, that works just like a node buffer, or use this browser buffer.

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.