<p>you will communicate over RPC/JSON</p>

<p>There is an example in <a href="https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)" rel="nofollow noreferrer">the official doc</a> :</p>

<pre><code>require 'net/http'
require 'uri'
require 'json'

class BitcoinRPC
  def initialize(service_url)
    @uri = URI.parse(service_url)
  end

  def method_missing(name, *args)
    post_body = { 'method' =&gt; name, 'params' =&gt; args, 'id' =&gt; 'jsonrpc' }.to_json
    resp = JSON.parse( http_post_request(post_body) )
    raise JSONRPCError, resp['error'] if resp['error']
    resp['result']
  end

  def http_post_request(post_body)
    http    = Net::HTTP.new(@uri.host, @uri.port)
    request = Net::HTTP::Post.new(@uri.request_uri)
    request.basic_auth @uri.user, @uri.password
    request.content_type = 'application/json'
    request.body = post_body
    http.request(request).body
  end

  class JSONRPCError &lt; RuntimeError; end
end

if $0 == __FILE__
  h = BitcoinRPC.new('http://user:password@127.0.0.1:8332')
  p h.getbalance
  p h.getinfo
  p h.getnewaddress
  p h.dumpprivkey( h.getnewaddress )
  # also see: https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list
end
</code></pre>

CC BY-SA 3.0
2018-04-27T05:53:49.007Z
This answer helped me. People who asked questions and leave after they get it without any acknowledgement are whats wrong with this world.
Arun Satyarth
CC BY-SA 3.0
2017-08-21T15:39:57.327Z
2017-08-21T15:39:57.327Z
Badr Bellaj
<p>I have started <code>bitcoind</code>. It is working fine but how can I connect to it from rails on localhost?</p>

<bitcoind><client><json-rpc>
How to connect to bitcoind from ruby rpc client?
sss
# How to connect to bitcoind from ruby rpc client?

(Asked by: username_0 on 2017-08-21)

I have started `bitcoind`. It is working fine but how can I connect to it from rails on localhost?

**Tags:** bitcoind, client, json-rpc


---

## 1 answers

### Answer by username_1 on 2017-08-21. Score: 2

you will communicate over RPC/JSON

There is an example in [the official doc](https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)) :

```
require 'net/http'
require 'uri'
require 'json'

class BitcoinRPC
  def initialize(service_url)
    @uri = URI.parse(service_url)
  end

  def method_missing(name, *args)
    post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
    resp = JSON.parse( http_post_request(post_body) )
    raise JSONRPCError, resp['error'] if resp['error']
    resp['result']
  end

  def http_post_request(post_body)
    http    = Net::HTTP.new(@uri.host, @uri.port)
    request = Net::HTTP::Post.new(@uri.request_uri)
    request.basic_auth @uri.user, @uri.password
    request.content_type = 'application/json'
    request.body = post_body
    http.request(request).body
  end

  class JSONRPCError < RuntimeError; end
end

if $0 == __FILE__
  h = BitcoinRPC.new('http://user:password@127.0.0.1:8332')
  p h.getbalance
  p h.getinfo
  p h.getnewaddress
  p h.dumpprivkey( h.getnewaddress )
  # also see: https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list
end

```

> **Comments:**
> **username_2**: This answer helped me. People who asked questions and leave after they get it without any acknowledgement are whats wrong with this world.  

---

