Connecting Bitcoin Nodes Via JSON RPC

Blockdaemon Bitcoin nodes expose an RPC interface for connecting and interactions.

Connecting:

btc_dedicated_cURL_connect.jpg

terminal.jpg

Command Line cURL

The following describes how to run a simple cURL command that prints the current blockchain information from the node.

The cURL command below can be used to retrieve the blockchain status of your node. Replace  <node endpoint> with your node’s endpoint plus authorization token from the Connect page.

btc_core_connect.jpg

You can get this address from your node dashboard,  connect when logged in at blockdaemon.com.

curl --user blockdaemon:blockdaemon --data 
'{"method":"getblockchaininfo","params":[],"id":1,"jsonrpc":"2.0"}' -H 
"Content-Type: application/json" -X POST https://<node endpoint>

Connecting via Python script with Python

The following describes how to run a simple Python application that prints the current best block.

The following text assumes a basic knowledge of Python 3 and pip.

Install Python libraries

Install the Python library python-bitcoinrpc. We will use this library to communicate with the RPC interface.

pip3 install python-bitcoinrpc

Copy script

Copy the following Python program into a file called bitcoin_rpc.py

import pprint from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

rpc_user = "blockdaemon" 
rpc_password = "blockdaemon" 
rpc_connection = AuthServiceProxy("<your node endpoint>" .format(rpc_user, rpc_password)) 
best_block_hash = rpc_connection.getbestblockhash() 
best_block = rpc_connection.getblock(best_block_hash) 
pprint.pprint(best_block)

Start the Python script

python3 bitcoin_rpc.py

Noteupdate 2.2021.

Bitcoin does not require an extra auth header (blockdaemon:blockdaemon)

  • Can still use extra auth user in header (existing curl example)
  • can use basic auth
  • can use X-Auth-Token in header without auth user
  • can use bearer token in header without auth user
  • can pass auth as url param

Further Reading

Bitcoin RPC Protocol

Python-bitcoinrpc . -> Documentation

Allowed JSON RPC Commands