Adding a file to IPFS remotely via the API with rails and rest-client

I have a remote IPFS node running that is protected with HTTP basic auth that I set up by following my own tutorial available here: https://medium.com/@cvcassano/protecting-an-ipfs-node-with-nginx-reverse-proxy-on-ubuntu-18-04-e56685a10bcc

Now I need to add a file to IPFS from a rails backend. Luckily, the IPFS HTTP API is really great, and I can just use the rest-client gem to add the file to IPFS.

Below is how I did it. Note that I’m using HTTP basic auth, which is why the user and password keys are set in the request. You may omit these if your IPFS api is not protected with HTTP basic auth.

    response = RestClient::Request.execute(
method: :post,
url: 'https://ipfs.deco.network/api/v0/add',
user: 'ipfsuser',
password: ENV['DECONET_IPFS_API_PASSWORD'],
timeout: 600, # Optional
payload: {
multipart: true,
file: File.new("/path/to/image.jpg", 'rb')
}
)
parsed = JSON.parse(response.body)
ipfs_hash = parsed['Hash']

After this code is run, the ipfs_hash variable will contain the ipfs hash of the object you uploaded.

Good luck!