DEV Community

Cover image for How to setup an RPC API for a Blockchain Node
Michael McShinsky
Michael McShinsky

Posted on • Originally published at magitek.dev

How to setup an RPC API for a Blockchain Node

RPC (Remote Procedure Call) is a protocol that allows a program to execute code on a remote server. Many blockchain nodes include an RPC interface that allows you to interact with your node programmatically. In this tutorial, using Bitcoin, we will walk through the steps required to setup and interact with an RPC interface for your node.

Prerequisites:

Before we get started, you'll need to have a Bitcoin node installed on your computer or server. You should also have a basic understanding of how to use the command line. If you're not familiar with the command line, you may want to do some additional research before proceeding.

Step 1: Install Bitcoin Core

The first step is to download and install Bitcoin Core on your computer. You can download Bitcoin Core from the official website (https://bitcoincore.org/en/download/). Once you've downloaded the software, follow the installation instructions for your operating system.

Note: It can take a variable amount of time (hours or days) to sync your node to the blockchain network before you can use all the methods the RPC interface provides.

Bitcore Node Wallet

It is recommended to use the testnet environment for experimenting with the blockchain in development before creating a production node for real world use. If you need Bitcoin to test transactions with, you can make use of this or similar faucets: https://kuttler.eu/en/bitcoin/btc/faucet/.

Step 2: Configure Bitcoin Core

Once you've installed Bitcoin Core, you need to configure it to enable the RPC interface. To do this, you'll need to create a bitcoin.conf file in the Bitcoin data directory.

On Linux/MacOS, you can create the file by running the following command:

nano ~/.bitcoin/bitcoin.conf
Enter fullscreen mode Exit fullscreen mode

On Windows, you can create the file by navigating to the Bitcoin data directory (usually located in %APPDATA%\Bitcoin) and creating a new file called bitcoin.conf.

Step 3: Configure the RPC interface

In the bitcoin.conf file, add the following lines to enable the RPC interface:

server=1
rpcuser=<username>
rpcpassword=<password>
rpcallowip=<IP address or subnet>
Enter fullscreen mode Exit fullscreen mode

Replace and with your own username and password, respectively. These will be used to authenticate the RPC connection.

If you want to use testnet instead of a production node, you can add the following line:

testnet=1
Enter fullscreen mode Exit fullscreen mode

Replace with the IP address or subnet that you want to allow to access the RPC interface. You can specify a single IP address or a subnet in CIDR notation (e.g. 192.168.0.0/16). This is more important when you want to access the RPC interface between a client and server. If you are doing this locally, you may be able to skip this step or set it to:

rpcallowip=127.0.0.1
rpcport=8332
Enter fullscreen mode Exit fullscreen mode

Step 4: Test the RPC interface

Now that the RPC interface is set up, you can use it to interact with your Bitcoin node programmatically. You will need to restart Bitcoin Core for the changes you have made to take effect. Once restarted, you can test it by running the following command in the terminal:

bitcoin-cli getblockcount
Enter fullscreen mode Exit fullscreen mode

If you run into issues using bitcoin-cli in the terminal, see the documentation here, or you can run a curl command as follows:

curl --user <username>:<password> --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockcount", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/
Enter fullscreen mode Exit fullscreen mode

If everything is set up correctly, you should see the current block count of your Bitcoin node.

Conclusion:

That's it! You now have an RPC interface set up for your Bitcoin node. You can use the RPC interface to interact with your node programmatically and build Bitcoin applications.


For a full RPC API reference, see: RPC API Reference β€” Bitcoin

You can use a generator like Bitcoin Core Config Generator to create a bitcoin.conf for yourself.


If you found this helpful or useful, please share a πŸ’“, πŸ¦„, or πŸ”–. Thanks!

Top comments (0)