|
| 1 | +# API |
| 2 | + |
| 3 | +* [IpfsHttpClientLite](#ipfshttpclientlite) |
| 4 | +* [add](#add) |
| 5 | +* [addPullStream](#addpullstream) TODO: add docs |
| 6 | +* [addFromStream](#addfromstream) TODO: add docs |
| 7 | +* [addFromURL](#addfromurl) TODO: add docs |
| 8 | +* [bitswap.stat](#bitswapstat) TODO: add docs |
| 9 | +* [bitswap.wantlist](#bitswapwantlist) TODO: add docs |
| 10 | +* [block.get](#blockget) TODO: add docs |
| 11 | +* [cat](#cat) |
| 12 | +* [catPullStream](#catpullstream) TODO: add docs |
| 13 | +* [ls](#ls) TODO: add docs |
| 14 | +* [lsPullStream](#lspullstream) TODO: add docs |
| 15 | +* [id](#id) TODO: add docs |
| 16 | +* [ping](#ping) TODO: add docs |
| 17 | +* [pingPullStream](#pingpullstream) TODO: add docs |
| 18 | +* [swarm.connect](#swarmconnect) TODO: add docs |
| 19 | +* [swarm.peers](#swarmpeers) TODO: add docs |
| 20 | +* [version](#version) TODO: add docs |
| 21 | + |
| 22 | +Note: All API methods are documented using Promises/async/await but they also accept a callback as their last parameter. |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## `IpfsHttpClientLite` |
| 27 | + |
| 28 | +Construct a new IPFS client. |
| 29 | + |
| 30 | +### `IpfsHttpClientLite([url])` |
| 31 | + |
| 32 | +#### Parameters |
| 33 | + |
| 34 | +* `url` (optional) - URL of the API endpoint to use. |
| 35 | + * Type: `String` |
| 36 | + * Default: `http://localhost:5001` in Node.js or `window.location.origin` in the browser. |
| 37 | + |
| 38 | +### `IpfsHttpClientLite([options])` |
| 39 | + |
| 40 | +#### Parameters |
| 41 | + |
| 42 | +* `options` (optional) |
| 43 | + * Type: `Object` |
| 44 | + * Default: `null` |
| 45 | +* `options.apiPath` (optional) - The API path from the root. |
| 46 | + * Type: `String` |
| 47 | + * Default: `/api/v0` |
| 48 | +* `options.apiUrl` (optional) - URL of the API endpoint to use. |
| 49 | + * Type: `String` |
| 50 | + * Default: `http://localhost:5001` in Node.js or `window.location.origin` in the browser. |
| 51 | +* `options.fetch` (optional) - Custom `fetch` implementation. |
| 52 | + * Type: `Function` |
| 53 | + * Default: `node-fetch` in Node.js and `window.fetch` in the browser. |
| 54 | +* `options.headers` (optional) - Custom HTTP headers to send with _every_ request. |
| 55 | + * Type: `Object` |
| 56 | + * Default: `{ 'User-Agent': 'ipfs-http-client-lite/1.0.0' }` |
| 57 | + |
| 58 | +## `add` |
| 59 | + |
| 60 | +Add/import files and directories to IPFS and retrieve their CID(s). |
| 61 | + |
| 62 | +### `add(input, [options]): Promise<Object[]>` |
| 63 | + |
| 64 | +#### Parameters |
| 65 | + |
| 66 | +* `input` - File contents or (for multiple files), description objects specifying file path and contents to add. |
| 67 | + * Type (one of): |
| 68 | + * `Buffer`, or "buffer like": `ArrayBuffer`/`TypedArray` |
| 69 | + * `Blob`/`File` (browser only) |
| 70 | + * `{ path, content: Buffer }` |
| 71 | + * `{ path, content: Iterable<Number> }` |
| 72 | + * `{ path, content: AsyncIterable<Buffer> }` |
| 73 | + * `{ path, content: PullStream<Buffer> }` |
| 74 | + * `Iterable<Number>` (e.g. array of bytes) |
| 75 | + * `Iterable<{ path, content: Buffer }>` |
| 76 | + * `Iterable<{ path, content: Iterable<Number> }>` |
| 77 | + * `Iterable<{ path, content: AsyncIterable<Buffer> }>` |
| 78 | + * `Iterable<{ path, content: PullStream<Buffer> }>` |
| 79 | + * `AsyncIterable<Buffer>` (e.g. a Node.js Stream) |
| 80 | + * `AsyncIterable<{ path, content: Buffer }>` |
| 81 | + * `AsyncIterable<{ path, content: Iterable<Number> }>` |
| 82 | + * `AsyncIterable<{ path, content: AsyncIterable<Buffer> }>` |
| 83 | + * `AsyncIterable<{ path, content: PullStream<Buffer> }>` |
| 84 | + * `PullStream<Buffer>` |
| 85 | +* `options` (optional) |
| 86 | + * Type: `Object` |
| 87 | + * Default: `null` |
| 88 | +* `options.chunker` (optional) - Chunking algorithm used to build IPFS DAGs. |
| 89 | + * Type: `String`. Available formats: |
| 90 | + - size-{size} |
| 91 | + - rabin |
| 92 | + - rabin-{avg} |
| 93 | + - rabin-{min}-{avg}-{max} |
| 94 | + * Default: `size-262144` |
| 95 | +* `options.cidVersion` (optional) - The CID version to use when storing the data (storage keys are based on the CID, including its version). |
| 96 | + * Type: `Number` (0 or 1) |
| 97 | + * Default: `0` |
| 98 | +* `options.cidBase` (optional) - Number base to display CIDs in. [Available values](https://github.com/multiformats/js-multibase/blob/master/src/constants.js). |
| 99 | + * Type: `String` |
| 100 | + * Default: `base58btc` for v0 CIDs or `base32` for v1 CIDs |
| 101 | +* `options.enableShardingExperiment` (optional) - Allows to create directories with an unlimited number of entries currently size of UnixFS directories is limited by the maximum block size. Note this is an experimental feature. |
| 102 | + * Type: `Boolean` |
| 103 | + * Default: `false` |
| 104 | +* `options.hashAlg` (optional) - Hashing algorithm to use when creating the CID(s). [Available values]( https://github.com/multiformats/js-multihash/blob/master/src/constants.js#L5-L343). |
| 105 | + * Type: `String` |
| 106 | + * Default: `sha2-256` |
| 107 | +* `options.onlyHash` (optional) - Do not add the file(s) to IPFS, only calculate the CID(s). |
| 108 | + * Type: `Boolean` |
| 109 | + * Default: `false` |
| 110 | +* `options.pin` (optional) - Pin the file(s) when adding. Setting this option to `false` will mean added files will be removed from your IPFS node when garbage collection is run. |
| 111 | + * Type: `Boolean` |
| 112 | + * Default: `true` |
| 113 | +* `options.progress` (optional) - A function that receives progress updates as data is added to IPFS. It is called with the byte length of chunks as a file is added to IPFS. |
| 114 | + * Type: `Function(bytes<Number>)` |
| 115 | + * Default: `null` |
| 116 | +* `options.quiet` (optional) - Return a minimal output. |
| 117 | + * Type: `Boolean` |
| 118 | + * Default: `false` |
| 119 | +* `options.quieter` (optional) - Return only the final CID. |
| 120 | + * Type: `Boolean` |
| 121 | + * Default: `false` |
| 122 | +* `options.rawLeaves` (optional) - If `true`, DAG leaves will contain raw file data and not be wrapped in a protobuf. |
| 123 | + * Type: `Boolean` |
| 124 | + * Default: `false` for v0 CIDs or `true` for v1 CIDs |
| 125 | +* `options.shardSplitThreshold` (optional) - Specifies the maximum size of unsharded directory that can be generated. |
| 126 | + * Type: `Number` |
| 127 | + * Default: `1000` |
| 128 | +* `options.signal` (optional) - A signal that can be used to abort the request |
| 129 | + * Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) |
| 130 | + * Default: `null` |
| 131 | +- `options.silent` (optional) - Return no output. |
| 132 | + * Type: `Boolean` |
| 133 | + * Default: `false` |
| 134 | +- `options.trickle` (optional) - If `true` will use the trickle DAG format for DAG generation. |
| 135 | + [Trickle definition from go-ipfs documentation](https://godoc.org/github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-unixfs/importer/trickle). |
| 136 | + * Type: `Boolean` |
| 137 | + * Default: `false` |
| 138 | +- `options.wrapWithDirectory` (optional) - Add a directory node that contains all the added files. |
| 139 | + * Type: `Boolean` |
| 140 | + * Default: `false` |
| 141 | + |
| 142 | +#### Returns |
| 143 | + |
| 144 | +An array of objects with details for each file and directory that was created. |
| 145 | + |
| 146 | +* Type: `Promise<Object[]>` |
| 147 | + |
| 148 | +Each `Object` in the array has the following properties: |
| 149 | + |
| 150 | +* `name` - Path for the file/directory that was added (will be equal to `hash` if no path was given) |
| 151 | + * Type: `String` |
| 152 | +* `hash` - CID of the file or directory |
| 153 | + * Type: `String` |
| 154 | +* `size` - File size in bytes |
| 155 | + * Type: `String` |
| 156 | + |
| 157 | +#### Examples |
| 158 | + |
| 159 | +##### Single file |
| 160 | + |
| 161 | +A buffer: |
| 162 | + |
| 163 | +```js |
| 164 | +const data = Buffer.from('hello world!') |
| 165 | +const res = await ipfs.add(data) |
| 166 | +console.log(res) |
| 167 | +/* |
| 168 | +[ |
| 169 | + { |
| 170 | + name: 'QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j', |
| 171 | + hash: 'QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j', |
| 172 | + size: '20' |
| 173 | + } |
| 174 | +] |
| 175 | +*/ |
| 176 | +``` |
| 177 | + |
| 178 | +A stream: |
| 179 | + |
| 180 | +```js |
| 181 | +const fs = require('fs') |
| 182 | +const res = await ipfs.add(fs.createReadStream('./package.json')) |
| 183 | +console.log(res) |
| 184 | +/* |
| 185 | +[ |
| 186 | + { |
| 187 | + name: 'package.json', |
| 188 | + hash: 'QmPoPzFTHedjR4TJcQwx1rPv8YSqAnzzFT35194vC8UShH', |
| 189 | + size: '1919' |
| 190 | + } |
| 191 | +] |
| 192 | +*/ |
| 193 | +``` |
| 194 | + |
| 195 | +##### Multiple files |
| 196 | + |
| 197 | +```js |
| 198 | +const fs = require('fs') |
| 199 | +const res = await ipfs.add([ |
| 200 | + { path: 'my-package.json', content: fs.createReadStream('./package.json') }, |
| 201 | + { path: 'hello.txt', content: Buffer.from('hello world!') } |
| 202 | +]) |
| 203 | +console.log(res) |
| 204 | +/* |
| 205 | +[ |
| 206 | + { |
| 207 | + name: 'my-package.json', |
| 208 | + hash: 'QmPoPzFTHedjR4TJcQwx1rPv8YSqAnzzFT35194vC8UShH', |
| 209 | + size: '1919' |
| 210 | + }, |
| 211 | + { |
| 212 | + name: 'hello.txt', |
| 213 | + hash: 'QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j', |
| 214 | + size: '20' |
| 215 | + } |
| 216 | +] |
| 217 | +*/ |
| 218 | +``` |
| 219 | + |
| 220 | +Wrap multiple files in a directory: |
| 221 | + |
| 222 | +In this example, the last item in the array is the wrapping directory, it allows you to access `hello.txt` using `ipfs.cat` with the following path `/ipfs/QmVY4sGYjQ4RNmJNBaJiWocTYAkzi4CkHfT16cXGouMdN7/hello.txt` but it can also be accessed using `ipfs.cat` with `/ipfs/QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j` (note the former uses the directory CID and the latter uses the file CID). |
| 223 | + |
| 224 | +```js |
| 225 | +const fs = require('fs') |
| 226 | +const res = await ipfs.add([ |
| 227 | + { path: 'my-package.json', content: fs.createReadStream('./package.json') }, |
| 228 | + { path: 'hello.txt', content: Buffer.from('hello world!') } |
| 229 | +], { |
| 230 | + wrapWithDirectory: true |
| 231 | +}) |
| 232 | +console.log(res) |
| 233 | +/* |
| 234 | +[ |
| 235 | + { |
| 236 | + name: 'my-package.json', |
| 237 | + hash: 'QmPoPzFTHedjR4TJcQwx1rPv8YSqAnzzFT35194vC8UShH', |
| 238 | + size: '1919' |
| 239 | + }, |
| 240 | + { |
| 241 | + name: 'hello.txt', |
| 242 | + hash: 'QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j', |
| 243 | + size: '20' |
| 244 | + }, |
| 245 | + { |
| 246 | + name: '', |
| 247 | + hash: 'QmVY4sGYjQ4RNmJNBaJiWocTYAkzi4CkHfT16cXGouMdN7', |
| 248 | + size: '2052' |
| 249 | + } |
| 250 | +] |
| 251 | +*/ |
| 252 | +``` |
| 253 | + |
| 254 | +## `cat` |
| 255 | + |
| 256 | +Read files from IPFS. |
| 257 | + |
| 258 | +### `cat(ipfsPath, [options]): Promise<Buffer>` |
| 259 | + |
| 260 | +#### Parameters |
| 261 | + |
| 262 | +* `ipfsPath` - The IPFS path or CID of the file to read. |
| 263 | + * Type: `String` |
| 264 | +* `options` (optional) |
| 265 | + * Type: `Object` |
| 266 | + * Default: `null` |
| 267 | +* `options.offset` (optional) - Byte offset to start reading from |
| 268 | + * Type: `Number` |
| 269 | + * Default: `0` |
| 270 | +* `options.length` (optional) - Number of bytes to read |
| 271 | + * Type: `Number` |
| 272 | + * Default: `null` (read to the end of the file) |
| 273 | + |
| 274 | +#### Returns |
| 275 | + |
| 276 | +A buffer containing the bytes for the file. |
| 277 | + |
| 278 | +* Type: `Promise<Buffer>` |
| 279 | + |
| 280 | +#### Examples |
| 281 | + |
| 282 | +An IPFS path: |
| 283 | + |
| 284 | +```js |
| 285 | +const data = await ipfs.cat('/ipfs/QmVY4sGYjQ4RNmJNBaJiWocTYAkzi4CkHfT16cXGouMdN7/hello.txt') |
| 286 | +console.log(data.toString('utf8')) |
| 287 | +/* |
| 288 | +hello world! |
| 289 | +*/ |
| 290 | +``` |
| 291 | + |
| 292 | +A CID: |
| 293 | + |
| 294 | +```js |
| 295 | +const data = await ipfs.cat('QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j') |
| 296 | +console.log(data.toString('utf8')) |
| 297 | +/* |
| 298 | +hello world! |
| 299 | +*/ |
| 300 | +``` |
0 commit comments