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:

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: '',
user: 'ipfsuser',
timeout: 600, # Optional
payload: {
multipart: true,
file:"/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!