|
| 1 | +# API |
| 2 | + |
| 3 | +## Usage |
| 4 | + |
| 5 | +We classify the API calls by 'core', 'extensions', 'tooling', and 'network', following the same API spec organization available at [ipfs/specs](https://github.com/ipfs/specs/tree/master/api). |
| 6 | + |
| 7 | +The tests folder also contains great examples that can be used to understand how this client library interacts with the HTTP-API. You can find the [tests here](tests/api). |
| 8 | + |
| 9 | +### Core |
| 10 | + |
| 11 | +##### version |
| 12 | + |
| 13 | +- [examples](https://github.com/ipfs/js-ipfs-api/blob/master/test/api/version.spec.js) |
| 14 | + |
| 15 | +##### node |
| 16 | + |
| 17 | +> node start and stop are not implemented in the API |
| 18 | +
|
| 19 | +- [examples](https://github.com/ipfs/js-ipfs-api/blob/master/test/api/id.spec.js) |
| 20 | + |
| 21 | +##### block |
| 22 | + |
| 23 | +- [examples](https://github.com/ipfs/js-ipfs-api/blob/master/test/api/block.spec.js) |
| 24 | + |
| 25 | +##### object |
| 26 | + |
| 27 | +*curl* |
| 28 | +```sh |
| 29 | +curl 'http://localhost:5001/api/v0/object/get?arg=QmYEqnfCZp7a39Gxrgyv3qRS4MoCTGjegKV6zroU3Rvr52&stream-channels=true' --compressed |
| 30 | +``` |
| 31 | + |
| 32 | +*response* |
| 33 | +```js |
| 34 | +{ |
| 35 | + Links: [{ |
| 36 | + Name: string, |
| 37 | + Hash: string, |
| 38 | + Size: number |
| 39 | + }, ...], |
| 40 | + Data: string |
| 41 | +} |
| 42 | +``` |
| 43 | +*Data is base64 encoded.* |
| 44 | + |
| 45 | +- [examples](https://github.com/ipfs/js-ipfs-api/blob/master/test/api/object.spec.js) |
| 46 | + |
| 47 | +##### pin |
| 48 | + |
| 49 | + |
| 50 | +------------------------------------------------------- |
| 51 | + |
| 52 | +### Extensions |
| 53 | + |
| 54 | + |
| 55 | +------------------------------------------------------- |
| 56 | + |
| 57 | +### Tooling |
| 58 | + |
| 59 | +##### add |
| 60 | + |
| 61 | +Add a file (where file is any data) to ipfs returning the hash and name. The |
| 62 | +name value will only be set if you are actually sending a file. A single or |
| 63 | +array of files can be used. |
| 64 | + |
| 65 | +*usage* |
| 66 | +```javascript |
| 67 | +ipfs.add(files, function(err, res) { |
| 68 | + if(err || !res) return console.error(err) |
| 69 | + |
| 70 | + res.forEach(function(file) { |
| 71 | + console.log(file.Hash) |
| 72 | + console.log(file.Name) |
| 73 | + }) |
| 74 | +}) |
| 75 | +``` |
| 76 | + |
| 77 | +`files` can be a mixed array of filenames or buffers of data. A single value is |
| 78 | +also acceptable. |
| 79 | + |
| 80 | +Example |
| 81 | +```js |
| 82 | +var files = ["../files/hello.txt", new Buffer("ipfs!")] |
| 83 | +var files = "../files/hello.txt" |
| 84 | +``` |
| 85 | + |
| 86 | +*curl* |
| 87 | +```sh |
| 88 | +curl 'http://localhost:5001/api/v0/add?stream-channels=true' \ |
| 89 | +-H 'content-type: multipart/form-data; boundary=a831rwxi1a3gzaorw1w2z49dlsor' \ |
| 90 | +-H 'Connection: keep-alive' \ |
| 91 | +--data-binary $'--a831rwxi1a3gzaorw1w2z49dlsor\r\nContent-Type: application/octet-stream\r\nContent-Disposition: file; name="file"; filename="Hello.txt"\r\n\r\nhello--a831rwxi1a3gzaorw1w2z49dlsor--' --compressed |
| 92 | +``` |
| 93 | + |
| 94 | +*response* |
| 95 | +```js |
| 96 | +[{ |
| 97 | + Hash: string, |
| 98 | + Name: string |
| 99 | +}, ...] |
| 100 | +``` |
| 101 | +*The name value will only be set for actual files.* |
| 102 | + |
| 103 | +##### cat |
| 104 | + |
| 105 | +Retrieve the contents of a single hash, or array of hashes. |
| 106 | + |
| 107 | +**usage** |
| 108 | + |
| 109 | +```javascript |
| 110 | +ipfs.cat(hashs, function(err, res) { |
| 111 | + if(err || !res) return console.error(err) |
| 112 | + |
| 113 | + if(res.readable) { |
| 114 | + // Returned as a stream |
| 115 | + res.pipe(process.stdout) |
| 116 | + } else { |
| 117 | + // Returned as a string |
| 118 | + console.log(res) |
| 119 | + } |
| 120 | +}) |
| 121 | +``` |
| 122 | + |
| 123 | +*curl* |
| 124 | + |
| 125 | +```sh |
| 126 | +curl "http://localhost:5001/api/v0/cat?arg=<hash>&stream-channels=true" |
| 127 | +``` |
| 128 | + |
| 129 | +*response* |
| 130 | + |
| 131 | +The response is either a readable stream, or a string. |
| 132 | + |
| 133 | +##### ls |
| 134 | +Get the node structure of a hash. Included in it is a hash and array to links. |
| 135 | + |
| 136 | +*Usage* |
| 137 | +```javascript |
| 138 | +ipfs.ls(hashs, function(err, res) { |
| 139 | + if(err || !res) return console.error(err) |
| 140 | + |
| 141 | + res.Objects.forEach(function(node) { |
| 142 | + console.log(node.Hash) |
| 143 | + console.log("Links [%d]", node.Links.length) |
| 144 | + node.Links.forEach(function(link, i) { |
| 145 | + console.log("[%d]", i, link) |
| 146 | + }) |
| 147 | + }) |
| 148 | +}) |
| 149 | +``` |
| 150 | + |
| 151 | +*Curl* |
| 152 | +```sh |
| 153 | +curl "http://localhost:5001/api/v0/ls?arg=<hash>&stream-channels=true" |
| 154 | +``` |
| 155 | + |
| 156 | +*Response* |
| 157 | +```js |
| 158 | +{ |
| 159 | + Objects: [ |
| 160 | + { |
| 161 | + Hash: string, |
| 162 | + Links: [{ |
| 163 | + Name: string, |
| 164 | + Hash: string, |
| 165 | + Size: number |
| 166 | + }, ...] |
| 167 | + }, |
| 168 | + .... |
| 169 | + ] |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +##### update |
| 174 | + |
| 175 | +------------------------------------------------------- |
| 176 | + |
| 177 | +### Network |
| 178 | + |
| 179 | + |
| 180 | +--------- |
| 181 | + |
| 182 | +#### Files |
| 183 | + |
| 184 | +##### mkdir |
| 185 | + |
| 186 | +```JavaScript |
| 187 | +ipfs.files.mkdir(<folderName>, function (err) {}) |
| 188 | +``` |
| 189 | + |
| 190 | +##### cp |
| 191 | + |
| 192 | +```JavaScript |
| 193 | +ipfs.files.cp([<pathSrc>, <pathDst>], function (err) {}) |
| 194 | +``` |
| 195 | + |
| 196 | +##### ls |
| 197 | + |
| 198 | +```JavaScript |
| 199 | +ipfs.files.ls(<path>, function (err, res) {}) |
| 200 | +``` |
| 201 | + |
| 202 | +##### stat |
| 203 | + |
| 204 | +```JavaScript |
| 205 | +ipfs.files.stat(<path>, function (err, res) {}) |
| 206 | +``` |
| 207 | + |
| 208 | +##### rm |
| 209 | + |
| 210 | +```JavaScript |
| 211 | +ipfs.files.rm(<path>, [<options>], function (err) {}) |
| 212 | +``` |
| 213 | + |
| 214 | +For `rm -r` pass a options obj with `r: true` |
| 215 | + |
| 216 | +##### read |
| 217 | + |
| 218 | +```JavaScript |
| 219 | +ipfs.files.read(<path>, function (err, res) { |
| 220 | + if(res.readable) { |
| 221 | + // Returned as a stream |
| 222 | + res.pipe(process.stdout) |
| 223 | + } else { |
| 224 | + // Returned as a string |
| 225 | + console.log(res) |
| 226 | + } |
| 227 | +}) |
| 228 | +``` |
| 229 | + |
| 230 | +##### write |
| 231 | + |
| 232 | +##### mv |
| 233 | + |
| 234 | +```JavaScript |
| 235 | +ipfs.files.mv([<pathSrc>, <pathDst>], function (err) {}) |
| 236 | +``` |
| 237 | + |
| 238 | +response: (it returns empty when successful) |
| 239 | + |
| 240 | +##### cp |
| 241 | + |
| 242 | +```JavaScript |
| 243 | +ipfs.files.cp([<pathSrc>, <pathDst>], function (err) {}) |
| 244 | +``` |
| 245 | + |
| 246 | +##### ls |
| 247 | + |
| 248 | +```JavaScript |
| 249 | +ipfs.files.ls(<path>, function (err, res) {}) |
| 250 | +``` |
| 251 | + |
| 252 | +##### stat |
| 253 | + |
| 254 | +```JavaScript |
| 255 | +ipfs.files.stat(<path>, function (err, res) {}) |
| 256 | +``` |
| 257 | + |
| 258 | +##### rm |
| 259 | + |
| 260 | +```JavaScript |
| 261 | +ipfs.files.rm(<path>, [<options>], function (err) {}) |
| 262 | +``` |
| 263 | + |
| 264 | +For `rm -r` pass a options obj with `r: true` |
| 265 | + |
| 266 | +##### read |
| 267 | + |
| 268 | +```JavaScript |
| 269 | +ipfs.files.read(<path>, function (err, res) { |
| 270 | + if(res.readable) { |
| 271 | + // Returned as a stream |
| 272 | + res.pipe(process.stdout) |
| 273 | + } else { |
| 274 | + // Returned as a string |
| 275 | + console.log(res) |
| 276 | + } |
| 277 | +}) |
| 278 | +``` |
| 279 | + |
| 280 | +##### write |
| 281 | + |
| 282 | +##### mv |
| 283 | +curl "http://localhost:5001/api/v0/files/mkdir?arg=%2Ffolder4" |
| 284 | + |
| 285 | +```JavaScript |
| 286 | +ipfs.files.mv([<pathSrc>, <pathDst>], function (err) {}) |
0 commit comments