Skip to content
This repository has been archived by the owner on Apr 16, 2019. It is now read-only.

IPFS RPC Basic Usage

Herman Junge edited this page Nov 18, 2016 · 1 revision

Getting the list of RPC commands

We can find the comprehensive list of RPC commands, with working examples in this Apiary link.

API routes are mapped just as the IPFS console commands!

That's right, if you know the command in console. You can easily map it into an API route.

We will shamelessly take the examples from the IPFS Docs API.

> ipfs swarm peers
/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx
/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z

> curl http://127.0.0.1:5001/api/v0/swarm/peers
{
  "Strings": [
    "/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
    "/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx",
    "/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z",
  ]
}

Also, you can add flags

> curl "http://127.0.0.1:5001/api/v0/object/get?arg=QmaaqrHyAQm7gALkRW8DcfGX3u8q9rWKnxEMmf7m9z515w&encoding=json"
{
  "Links": [
    {
      "Name": "index.html",
      "Hash": "QmYftndCvcEiuSZRX7njywX2AGSeHY2ASa7VryCq1mKwEw",
      "Size": 1700
    },
    {
      "Name": "static",
      "Hash": "QmdtWFiasJeh2ymW3TD2cLHYxn1ryTuWoNpwieFyJriGTS",
      "Size": 2428803
    }
  ],
  "Data": "CAE="
}

And arguments:

> curl "http://0.0.0.0:5001/api/v0/swarm/disconnect?arg=/ip4/54.93.113.247/tcp/48131/ipfs/QmUDS3nsBD1X4XK5Jo836fed7SErTyTuQzRqWaiQAyBYMP"
{
  "Strings": [
    "disconnect QmUDS3nsBD1X4XK5Jo836fed7SErTyTuQzRqWaiQAyBYMP success",
  ]
}

Adding a file

As simple as

$ curl -F "image=@/home/bar.jpg" http://127.0.0.1:5001/api/v0/add

You'd get something like

{"Name":"bar.txt","Hash":"QmeANYk2o7z5VmS6Dkq68UcC8J2kFgTYS3PQrjYdj3zqvL"}

Getting the contents of a file

Even simpler, let's use the gotten hash above (QmeANYk2o7z5VmS6Dkq68UcC8J2kFgTYS3PQrjYdj3zqvL)

$ curl http://0.0.0.0:5001/api/v0/cat?arg=QmeANYk2o7z5VmS6Dkq68UcC8J2kFgTYS3PQrjYdj3zqvL

Will print the contents of the file on console.

CORS

Here in this wiki page, you can find information on how to configure CORS in your daemon, and/or, how to test whether CORS is available in your or another IPFS daemon.