Intro

Installable via npm install --save ipfs, it can also be used directly in the browser.

Download

The source is available for download from GitHub. Alternatively, you can install using npm:

$ npm install --save ipfs

You can then require() ipfs as normal:

const ipfs = require('ipfs')

In the Browser

Ipfs should work in any ES2015 environment out of the box.

Usage:

<script type="text/javascript" src="index.js"></script>

The portable versions of ipfs, including index.js and index.min.js, are included in the /dist folder. Ipfs can also be found on unpkg.com under

Parses chunker string into options used by DAGBuilder in ipfs-unixfs-engine

parseChunkerString
Parameters
chunker (String) Chunker algorithm supported formats: "size-{size}" "rabin" "rabin-{avg}" "rabin-{min}-{avg}-{max}"
Returns
Object: Chunker options for DAGBuilder

Parses rabin chunker string

parseRabinString
Parameters
chunker (String) Chunker algorithm supported formats: "rabin" "rabin-{avg}" "rabin-{min}-{avg}-{max}"
Returns
Object: rabin chunker options

Protobuf interface from go-ipfs/pin/internal/pb/header.proto

pin.proto

parseIpfsPath

src/core/utils.js

Break an ipfs-path down into it's hash and an array of links.

examples: b58Hash -> { hash: 'b58Hash', links: [] } b58Hash/mercury/venus -> { hash: 'b58Hash', links: ['mercury', 'venus']} /ipfs/b58Hash/links/by/name -> { hash: 'b58Hash', links: ['links', 'by', 'name'] }

parseIpfsPath(ipfsPath: String): Object
Parameters
ipfsPath (String) An ipfs-path
Returns
Object: { hash: base58 string, links: [string] , ?err: Error }
Throws
  • any: on an invalid @param ipfsPath

normalizePath

src/core/utils.js

Returns a well-formed ipfs Path. The returned path will always be prefixed with /ipfs/ or /ipns/.

normalizePath
Parameters
pathStr (String) An ipfs-path, or ipns-path or a cid
Returns
String: ipfs-path or ipns-path
Throws
  • any: on an invalid @param ipfsPath

Resolve various styles of an ipfs-path to the hash of the target node. Follows links in the path.

Accepts formats:

  • /link/to/venus
  • /ipfs//link/to/pluto
  • multihash Buffer
  • Arrays of the above
resolvePath
Parameters
dag (Dag) The IPFS dag api
ipfsPaths (Array<(CID | string)>) A single or collection of ipfs-paths
options (Object?) Optional options passed directly to dag.resolve
Returns
Promise<Array<CID>>:

Change file mode

chmod
Parameters
path (String) The path of the source to modify.
mode (Object) The mode to set the path
opts (Object?) Options for modification.
Name Description
opts.recursive boolean (default false) Whether to change modes recursively. (default: false)
opts.flush boolean (default true) Whether or not to immediately flush MFS changes to disk (default: true).
opts.shardSplitThreshold number? If the modified path has more than this many links it will be turned into a HAMT shard
Returns
Promise<string>:

Copy files

cp
Parameters
from ((String | Array<String>)) The path(s) of the source to copy.
to (String) The path of the destination to copy to.
opts (Object?) Options for copy.
Name Description
opts.parents boolean (default false) Whether or not to make the parent directories if they don't exist. (default: false)
opts.format String (default dag-pb) Format of nodes to write any newly created directories as. (default: dag-pb)
opts.hashAlg String (default sha2-256) Algorithm to use when creating CIDs for newly created directories. (default: sha2-256) The list of all possible values
opts.flush boolean (default true) Whether or not to immediately flush MFS changes to disk (default: true).
Returns
Promise<string>:

Make a directory

mkdir
Parameters
path (String) The path to the directory to make.
opts (Object?) Options for mkdir.
Name Description
opts.parents boolean (default false) Value to decide whether or not to make the parent directories if they don't exist. (default: false)
opts.format String (default dag-pb) Format of nodes to write any newly created directories as. (default: dag-pb).
opts.hashAlg String? Algorithm to use when creating CIDs for newly created directories. (default: sha2-256) The list of all possible values
opts.flush boolean (default true) Whether or not to immediately flush MFS changes to disk (default: true).
Returns
Promise<void>:
StatOutput

Type: Object

Properties
hash (String) : Output hash.
size (number) : File size in bytes.
cumulativeSize (number) : Integer with the size of the DAGNodes making up the file in Bytes.
type (string) : Output type either 'directory' or 'file'.
blocks (number) : If type is directory, this is the number of files in the directory. If it is file it is the number of blocks that make up the file.
withLocality (boolean) : Indicate if locality information is present.
local (boolean) : Indicate if the queried dag is fully present locally.
sizeLocal (number) : Integer indicating the cumulative size of the data present locally.

Get file or directory status.

stat
Parameters
path (String) Path to the file or directory to stat.
opts (Object?) Options for stat.
Name Description
opts.hash boolean (default false) Return only the hash. (default: false)
opts.size boolean (default false) Return only the size. (default: false)
opts.withLocal boolean (default false) Compute the amount of the dag that is local, and if possible the total size. (default: false)
Returns
Promise<StatOutput>:

Remove a file or directory.

rm
Parameters
paths ((String | Array<String>)) One or more paths to remove.
opts (Object?) Options for remove.
Name Description
opts.recursive boolean (default false) Whether or not to remove directories recursively. (default: false)
Returns
Promise<void>:
ReadOptions

Type: Object

Read a file into a Buffer.

read
Parameters
path (string) Path of the file to read and must point to a file (and not a directory).
opts (ReadOptions?) Object for read.
Returns
AsyncIterable<Buffer>:

Update modification time

touch
Parameters
path (String) The path of the source to modify.
mtime (number) Time to use as the new modification time in seconds since (+ve) or before (-ve) the Unix Epoch
opts (Object?) Options for touch.
Name Description
opts.parents boolean (default false) Whether or not to make the parent directories if they don't exist. (default: false)
opts.cidVersion number (default 0) CID version to use with the newly updated node
opts.shardSplitThreshold number? If the modified path has more than this many links it will be turned into a HAMT shard
Returns
Promise<string>:

Write to a file.

write
Parameters
path (string) Path of the file to write.
content ((Buffer | PullStream | ReadableStream | Blob | string)) Content to write.
opts (Object) Options for write.
Name Description
opts.offset number (default 0) Integer with the byte offset to begin writing at. (default: 0)
opts.create boolean (default false) Indicate to create the file if it doesn't exist. (default: false)
opts.truncate boolean (default false) Indicate if the file should be truncated after writing all the bytes from content. (default: false)
opts.parents boolena (default false) Value to decide whether or not to make the parent directories if they don't exist. (default: false)
opts.length number? Maximum number of bytes to read. (default: Read all bytes from content)
opts.rawLeaves boolean (default false) If true, DAG leaves will contain raw file data and not be wrapped in a protobuf. (default: false)
opts.cidVersion number (default 0) The CID version to use when storing the data (storage keys are based on the CID, including its version). (default: 0)
Returns
Promise<void>:

If from has multiple values then to must be a directory.

If from has a single value and to exists and is a directory, from will be moved into to.

If from has a single value and to exists and is a file, from must be a file and the contents of to will be replaced with the contents of from otherwise an error will be returned.

If from is an IPFS path, and an MFS path exists with the same name, the IPFS path will be chosen.

All values of from will be removed after the operation is complete unless they are an IPFS path.

mv
Parameters
from ((string | Array<string>)) Path(s) of the source to move.
to (string) Path of the destination to move to.
opts (Object) Options for mv.
Name Description
opts.parents boolean (default false) Value to decide whether or not to make the parent directories if they don't exist. (default: false)
opts.format String (default dag-pb) Format of nodes to write any newly created directories as. (default: dag-pb).
opts.hashAlg String? Algorithm to use when creating CIDs for newly created directories. (default: sha2-256) The list of all possible values
opts.flush boolean (default true) Value to decide whether or not to immediately flush MFS changes to disk. (default: true)
Returns
Promise<void>:

Flush a given path's data to the disk.

flush
Parameters
paths ((string | Array<string>)?) String paths to flush. (default: /)
Returns
Promise<void>:
ListOutputFile

Type: Object

Properties
name (string) : Which is the file's name.
type (string) : Which is the object's type (directory or file).
size (number) : The size of the file in bytes.
hash (string) : The hash of the file.
ListOptions

Type: Object

Properties
long (boolean?) : Value to decide whether or not to populate type, size and hash. (default: false)
sort (boolean?) : If true entries will be sorted by filename. (default: false)

List directories in the local mutable namespace.

ls
Parameters
path (string = "/") String to show listing for. (default: /)
opts (ListOptions?) Options for list.
Returns
AsyncIterable<ListOutputFile>:

Time Aware Least Recent Used Cache

new TLRU()
Static Members
get(key)
set(key, value, ttl)
has(key)
remove(key)
clear()

Put a value to the local datastore indexed by the received key properly encoded.

put(key: Buffer, value: Buffer, callback: function (Error)): void
Parameters
key (Buffer) identifier of the value.
value (Buffer) value to be stored.
callback (function (Error))
Returns
void:

Get a value from the local datastore indexed by the received key properly encoded.

get(key: Buffer, callback: function (Error, Buffer)): void
Parameters
key (Buffer) identifier of the value to be obtained.
callback (function (Error, Buffer))
Returns
void:
publish($0: Object)
Parameters
$0 (Object)
Name Description
$0.ipns any
$0.dag any
$0.peerInfo any
$0.isOnline any
$0.keychain any
$0.options any

IPNS - Inter-Planetary Naming System

publish(self: IPFS): Object
Parameters
self (IPFS)
Name Description
self.ipns any
self.dag any
self.peerInfo any
self.isOnline any
self.keychain any
self.options any
Returns
Object:

IPNS is a PKI namespace, where names are the hashes of public keys, and the private key enables publishing new (signed) values. In both publish and resolve, the default name used is the node's own PeerID, which is the hash of its public key.

publish
Parameters
value (String) ipfs path of the object to be published.
options (Object) ipfs publish options.
Name Description
options.resolve boolean resolve given path before publishing.
options.lifetime String time duration that the record will be valid for. This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are "ns", "ms", "s", "m", "h". Default is 24h.
options.ttl String time duration this record should be cached for (NOT IMPLEMENTED YET). This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are "ns", "ms", "s", "m", "h" (caution: experimental).
options.key String name of the key to be used, as listed by 'ipfs key list -l'.
callback (function (Error)?)
Returns
(Promise | void):

Cancel a name subscription.

cancel
Parameters
name (String) subscription name.
callback (function (Error)?)
Returns
Promise<{canceled: boolean}>:

Query the state of IPNS pubsub.

state
Returns
Promise<boolean>:

Put a value to the pubsub datastore indexed by the received key properly encoded.

put(key: Buffer, value: Buffer, callback: function (Error)): void
Parameters
key (Buffer) identifier of the value.
value (Buffer) value to be stored.
callback (function (Error))
Returns
void:

Get a value from the pubsub datastore indexed by the received key properly encoded. Also, the identifier topic is subscribed to and the pubsub datastore records will be updated once new publishes occur.

get(key: Buffer, callback: function (Error, Buffer)): void
Parameters
key (Buffer) identifier of the value to be obtained.
callback (function (Error, Buffer))
Returns
void:

Get pubsub subscriptions related to ipns.

getSubscriptions(callback: function (Error, Object)): Array<Object>
Parameters
callback (function (Error, Object))
Returns
Array<Object>:

Cancel pubsub subscriptions related to ipns.

cancel(name: String, callback: function (Error, Object)): void
Parameters
name (String) ipns path to cancel the pubsub subscription.
callback (function (Error, Object))
Returns
void:

Show current name subscriptions.

subs
Parameters
callback (function (Error)?)
Returns
Promise<Array<string>>:
resolve($0: Object)
Parameters
$0 (Object)
Name Description
$0.dns any
$0.ipns any
$0.peerInfo any
$0.isOnline any
$0.options any

IPNS - Inter-Planetary Naming System

resolve(self: IPFS): Object
Parameters
self (IPFS)
Name Description
self.dns any
self.ipns any
self.peerInfo any
self.isOnline any
self.options any
Returns
Object:

Given a key, query the DHT for its best value.

resolve
Parameters
name (String) ipns name to resolve. Defaults to your node's peerID.
options (Object) ipfs resolve options.
Name Description
options.nocache boolean do not use cached entries.
options.recursive boolean resolve until the result is not an IPNS name.
callback (function (Error)?)
Returns
(Promise | void):
resolve($0: Object)
Parameters
$0 (Object)
Name Description
$0.ipld any
$0.name any

IPFS Resolve factory

resolve(config: Object): Resolve
Parameters
config (Object)
Name Description
config.ipld IPLD An instance of IPLD
config.name NameApi? An IPFS core interface name API
Returns
Resolve:

cidToString

src/utils/cid.js

Stringify a CID encoded in the requested base, upgrading to v1 if necessary.

Setting upgrade to false will disable automatic CID upgrading from v0 to v1 which is necessary if the multibase is something other than base58btc. Note that it will also not apply the encoding (since v0 CIDs can only be encoded as base58btc).

cidToString(cid: (CID | Buffer | String), options: Object?): String
Parameters
cid ((CID | Buffer | String)) The CID to encode
options (Object?) Optional options
Name Description
options.base String? Name of multibase codec to encode the CID with
options.upgrade Boolean? Automatically upgrade v0 CIDs to v1 when necessary. Default: true.
Returns
String:

If the repo has been initialized, report the current version. Otherwise report the version that would be initialized.

version
Returns
number:
ResolveOptions

Type: Object

Properties
cidBase (string) : Multibase codec name the CID in the resolved path will be encoded with
recursive (boolean?) : Resolve until the result is an IPFS name