February 3, 2026

Build Your Own Bitcoin API using Node.js and Bitcoin Core

Build Your Own Bitcoin API using Node.js and Bitcoin Core

Build Your Own Bitcoin API using Node.js and Bitcoin Core

Lastly and optionally, if you plan to store this code remotely, create a .gitignore file in the root project folder. Add .env to this file, so that you don’t accidentally commit your node’s RPC login.

In our server.js file we specified that express should look for our endpoints in /routes/api.js. These endpoints will hold the various bitcoin RPC methods that we can use to interact with the software.

Here we are again initializing an express router, request module, and environmental variables. The headers variable will be the same in each request so we can include it at the top. The line that begins with router.get("/test", ...) is our test route, which we will soon be able to view.

Start the application by entering the command:

npm run server

If the server has started correctly, in your console you should see

Server running on port 4444

Now open your browser and navigate to:

localhost:4444/api/test

you should get the message “backend works” as a response.

We will be setting up 12 more routes in this application, each which correspond to a Bitcoin RPC command. These methods can be used in the Bitcoin Core console (found under Window -> Console in Bitcoin Core), as well as in our Node.js application.

The first 8 routes we will use do not require an input argument. They include:

getblockcount
getbestblockhash
getconnectioncount
getdifficulty
getblockchaininfo
getmininginfo
getpeerinfo
getrawmempool

These 8 methods will return to us some details about the current state of the blockchain, mempool, and our node’s network connections. Each method will be accessed in the same way, and return an object with our requested data.

To setup our first route, replace the comment

// ... routes will go here

with the following code:

Here we are telling the express router to listen for when a user hits the endpoint /api/getblockcount. The dataString and optionsvariables include input parameters such as the name of the method we are calling and the url of our bitcoin node (including our login). If we are running both the server and bitcoin node, in the browser at localhost:4444/api/getblockcount we should see the response

In this response, the value 588608 is the current number of blocks in the blockchain. Each response will have an object containing the requested data in the result property.

To set up the other 7 methods

getbestblockhash, getconnectioncount, getdifficulty, getblockchaininfo, getmininginfo, getpeerinfo, getrawmempool

simply reuse the getblockcount code above for each method. In each endpoint there will be 2 instances of the method name, one to specify the endpoint address such as /getblockcount, and the other as a value in the dataString variable such as "method": "getblockcount". Replace both of these variables for each of the 7 methods above and you will have an API with 8 endpoints to Bitcoin RPC commands!

(Alternatively you can just copy all 12 endpoints from here)

Take some time to explore the various responses from these basic commands. getblockchaininfo, getmininginfo, and getpeerinfo are loaded with useful information that you can use to build interesting applications.

In this tutorial we will also be querying blockchain data using 4 methods which require 1 input argument each. These methods are listed below with a space between their name and the name of the input parameter.

getblockhash index
getblock hash
getrawtransaction id
decoderawtransaction hex

These 4 methods allow us to lookup blocks and transactions.

Each block on the Bitcoin blockchain has a unique index and hash value. The block’s hash is required to get data about the block. This hash can be retrieved with the getblockhash method, passing in an index of the specified block. Once the block’s hash is returned, we can find the block info with the getblock command, passing in the block’s hash.

To include this functionality in our API we can add the following methods for block retrieval

For getblock

For getblockhash

Note I included both endpoints here as the getblockhash method accepts a number instead of a string.

With each of these methods, we will also need to pass in an input parameter. For example, to find a blockhash with the getblockhash command, visit:

localhost:4444/api/getblockhash/520482

in your browser. The block index here is passed in by adding a / after the method endpoint and before the input parameter.

You can find this block’s transaction data by using the returned block hash as an argument to a getblock call.

Now, with our API we are capable of finding the transactions in the most recent block by stringing together the getblockcount, getblockhash, and getblock commands together. We can also find the transactions in any block that we have the index or hash for.

For our transaction methods we will need a Bitcoin node that is indexing transactions in order to access the entire blockchain. To index the transactions on your node, you will need to add the following line to the bitcoin.conf file

txindex=1

save this file, then restart Bitcoin Core. Indexing the entire blockchain will take some time as well as additional storage on your harddrive. It is recommended to index the blockchain overnight or while you are away from your computer, as it can take several to many hours.

Each transaction in the bitcoin blockchain has a unique id. This id can be used as an argument in the getrawtransaction method to return the hex encoded transaction data. The decoderawtransaction method can be used with this hex data as an argument to reveal information about that transaction.

Add the following transaction endpoints to your api.js file

For getrawtransaction

For decoderawtransaction

Every Bitcoin RPC command can be added as an endpoint in this API to make fully functional Bitcoin applications.

With these 12 methods we have setup a communication with our Bitcoin node that will allow us to build all sorts of applications.

There are a multitude of libraries and frameworks you can use to build fully functional web applications on top of Bitcoin. As a starting point, these are some basic steps to build a React application which is able to talk with our API.

To setup a new React application, enter the following command in the root of your project folder

npx create-react-app frontend

Next, change directories into your frontend folder and add the axios package for API requests by entering

yarn add axios

Additionally, you will need to add a proxy to your frontend package.json file for your API requests.

"proxy": "http://localhost:4444",

From here we simply need to setup our React application to store the API data in state. To test out the getblockchaininfo, getmininginfo, and getpeerinfo methods update your App.js file to the following:

Let’s see our basic full stack application in action by starting our React frontend with the command:

yarn start

With our API fully setup we can use Bitcoin RPC commands as long as our Bitcoin node and Node.js server are running. With these commands accessible, you can easily build a block explorer, fee calculator, node dashboard, testnet wallet, or any number of other applications. Using your own node is an excellent sandbox for testing out various ideas and learning more about how to interact with the Bitcoin blockchain.

Published at Mon, 05 Aug 2019 00:14:40 +0000

Bitcoin Pic Of The Moment
✅ Marco Verch is a Professional Photographer and Speaker from Cologne. ? This image can be used under Creative Commons 2.0. Please link to the original photo and the license.
By marcoverch on 2017-12-18 09:12:23
tags

Previous Article

Build Your Own Bitcoin API using Node.js and Bitcoin Core

Next Article

Why Is the US Not Yet a Leader in Crypto Regulation? — Experts Answer

You might be interested in …