Bitcoin RPC Commands and Methods | Bitcoin Core RPC

Bitcoin RPC Commands and Methods | Bitcoin Core RPC

TL;DR

  • Bitcoin RPC is the JSON-based protocol to talk to your node (port 8332).
  • You need ~2TB SSD 4GB+ RAM.
  • Never expose `rpcallowip=0.0.0.0` to the internet; you will get hacked.
  • Use bitcoin-cli getblockchaininfo to check sync status.

Table of Contents

Introduction to RPC Methods

Bitcoin Remote Procedure Call (RPC) methods are for developers and anyone looking to interact with the Bitcoin blockchain. These commands form a communication bridge, enabling applications to interact directly with the Bitcoin network. Through RPC, developers can initiate transactions, retrieve blockchain data, manage wallets, and perform node operations. The flexibility of RPC commands makes them a powerful tool for creating decentralized applications (DApps) and integrating blockchain capabilities into other software.

In the world of Bitcoin, RPC methods empower developers to access node data, create automated scripts, and implement various Bitcoin functionalities. Whether you're looking to build a Bitcoin wallet, manage transactions, or dive into blockchain data, understanding Bitcoin RPC methods is essential.

How to Run Your Own Bitcoin Node

Running a Bitcoin node brings complete data control and directly contributes to the network's decentralization. As a node operator, you not only gain full autonomy over your transactions but also help support the robustness of the Bitcoin network.

Requirements:

  • Hardware: A computer with at least 2GB of RAM and a modern processor.
  • Storage: Minimum 800GB of storage to accommodate the blockchain. Ideally 1TB+ as of now.
  • Internet Speed: Stable and high-speed internet is vital to keep your node synced with the network. I'd recommend 100mbps
  • Setup: Bitcoin Core, the primary client for Bitcoin, can be installed on Linux, Windows, or macOS. Each OS has specific setup requirements, but generally, the process involves downloading Bitcoin Core, adjusting configuration settings, and letting it sync.
Note: I currently run a 2TB NVMe SSD and the blockchain data is consuming that storage quicker then I'd like. If you can, aim for 4TB+ NVME SSD upfront.

If you are stuck with smaller drives, take a look into 
LVM (Logical Volume Manager). It allows you to use multiple HDD/SSD as a single virtual volume.

Common challenges include maintaining synchronization with the blockchain, which can be resource-intensive, mostly during the initial sync with the network.

Setting Up Bitcoin RPC Access in bitcoin.conf

The bitcoin.conf file is the main configuration file for Bitcoin Core. It’s typically located in the Bitcoin data directory (~/.bitcoin on Unix-based systems, %APPDATA%\ Bitcoin on Windows) and can be edited to configure your node’s behavior, including enabling and securing RPC access.

Basic bitcoin.conf Settings for RPC Access

To enable and secure RPC access, add the following configurations to bitcoin.conf:

Enable the RPC Server

server=1

This line activates the RPC server on your Bitcoin node, making it possible to send commands to the node over RPC.

Define RPC User and Password

rpcuser=yourusername
rpcpassword=yourpassword

Set unique and complex values for rpcuser and rpcpassword to secure RPC access. These credentials will be required for any remote connections to the node.

Define RPC User and Password

rpcallowip=127.0.0.1

Restricting access to 127.0.0.1 (localhost) prevents external access to the RPC server. If you need remote access from specific IPs, add additional rpcallowip entries (e.g., rpcallowip=192.168.1.100), but avoid exposing the node to the entire internet for security unless you know what you're doing.

Specify the RPC Port (Optional) 

rpcport=8332

By default, the Bitcoin RPC server listens on port 8332. You can change this port if needed, but make sure any firewalls or security rules are updated accordingly.

Endpoint Services as Alternatives

For developers with limited resources, using endpoint services can provide easy access to the blockchain without the need to maintain a full node. These services offer scalable, no-maintenance solutions with reliable access to the blockchain.

Popular endpoint services like BlockCypher and Infura offer reliable connectivity, scalability, and reduced operational demands, allowing developers to focus on their applications without worrying about node management.

HTTP Bitcoin RPC Endpoint

There are services that provide HTTP JSON RPC endpoints as a service (QuickNode). This service provides consistent and high-performance access to Bitcoin data, ideal for developers building personal projects or scalable applications.

This solution provides developers with a hassle-free way to interact with Bitcoin data, eliminating the overhead of managing a node. It allows easy integration into existing projects, making it a valuable asset for both individuals and teams looking to streamline their development process.

Overview of Common Bitcoin RPC Methods

Here are some frequently used Bitcoin RPC methods and their primary functions:

  1. getblockchaininfo - Retrieves comprehensive information about the current state of the blockchain, including chain, block count, difficulty, and best block hash.
  2. getblockcount - Returns the current number of blocks in the longest blockchain, often used to check synchronization status.
  3. getbestblockhash - Provides the hash of the most recent block, useful for verifying the latest block status.
  4. getblock - Retrieves detailed information about a block by its hash, often used for exploring specific block details and transaction data.
  5. getrawtransaction - Returns detailed information about a particular transaction by its TXID, commonly used to fetch and display transaction data.

RPC Command Cheat Sheet

Command Category Description Example Use Case
getblockchaininfo Blockchain Returns chain state: height, difficulty, verification progress Dashboard health checks
getblockcount Blockchain Current block height (integer) Sync progress monitoring
getbestblockhash Blockchain Hash of the tip block Confirming latest block
getblock <hash> [verbosity] Blockchain Block data; verbosity 0=hex, 1=JSON, 2=JSON+tx details Block explorers
getblockhash <height> Blockchain Get block hash at specific height Historical lookups
getrawtransaction <txid> [verbose] Transactions Raw or decoded transaction data (requires -txindex) Transaction inspection
sendrawtransaction <hex> Transactions Broadcast a signed transaction Pushing TX to network
estimatesmartfee <conf_target> Util Fee estimate in BTC/kvB for N-block confirmation Fee calculation
getmempoolinfo Mempool Mempool size, bytes, fee thresholds Congestion monitoring
getrawmempool [verbose] Mempool List of unconfirmed TXIDs Pending TX tracking
getbalance Wallet Wallet balance in BTC Balance queries
listunspent Wallet Available UTXOs for spending UTXO selection
getnewaddress [label] [type] Wallet Generate receive address (bech32m default in v24+) Receiving payments
sendtoaddress <addr> <amount> Wallet Send BTC (wallet must be unlocked) Simple sends
getpeerinfo Network Connected peers, versions, latency Network debugging
getnetworkinfo Network Node version, connections, relay fee Node diagnostics
validateaddress <address> Util Check if address is valid Input validation
Note: Wallet commands require a loaded wallet. Use createwallet or loadwallet first.
Tip: Commands like getrawtransaction require -txindex=1 in bitcoin.conf to query non-wallet transactions.

Making Your First RPC Call

Once your node is running and RPC is enabled, you can interact with it using bitcoin-cli or raw HTTP requests with curl.

Here's how both approaches work: Using bitcoin-cli

# Get current blockchain status
bitcoin-cli getblockchaininfo

# Get the latest block hash
bitcoin-cli getbestblockhash

# Fetch block details with full transaction data
bitcoin-cli getblock $(bitcoin-cli getbestblockhash) 2

bitcoin-cli

Using curl 

Basic RPC call to get blockchain info

curl --user yourusername:yourpassword \
  --data-binary '{"jsonrpc":"1.0","id":"curlrequest","method":"getblockchaininfo","params":[]}' \
  -H 'content-type: text/plain;' \
  http://127.0.0.1:8332/

getblockchaininfo

Get a specific block by height

curl --user yourusername:yourpassword \
  --data-binary '{"jsonrpc":"1.0","id":"curlrequest","method":"getblockhash","params":[800000]}' \
  -H 'content-type: text/plain;' \
  http://127.0.0.1:8332/

## and then call with the specific hash

curl --user yourusername:yourpassword \
  --data-binary '{"jsonrpc":"1.0","id":"curlrequest","method":"getblock","params":["00000000000000000002a7c4c1e48d76c5a37902165a270156b7a8d72728a054",1]}' \
  -H 'content-type: text/plain;' \
  http://127.0.0.1:8332/

getblockhash

A successful RPC call returns JSON.

{
  "result": { ... },   // The actual data you requested
  "error": null,       // null if successful, error object if failed
  "id": "curlrequest"  // Matches your request ID for correlation
}

bitcon rpc json

When you will get an error you'd have to check for the error key

{
  "result": null,
  "error": {"code": -5, "message": "No such mempool transaction"},
  "id": "curlrequest"
}



Conclusion

Bitcoin RPC methods are tools for developers aiming to build, interact with, or extend applications on the Bitcoin blockchain.

By setting up a Bitcoin node and enabling RPC, developers unlock direct access to blockchain data, manage wallets, execute transactions, and perform various node operations.

The setup is fairly straightforward and depending on the hardware used can be time consuming. The bitcoin.conf file, requires careful setup for secure access and the result is a foundation for future bitcoin blockchain applications - from serving HTTP endpoint to fully fledged ETL pipelines.

Whether through a personal node or endpoint service, leveraging Bitcoin RPC methods ultimately enhances the potential for innovative applications within the decentralized ecosystem.

Read more