Starting off simply for this first how-to article, we’re going to show how to query BTC and BCH using Bloq Connect to see the balance associated with an address.
You’ll need a Bloq account to interact with the REST API. First-time users can follow the Getting Started guide to create their bloq account.
An authorization token is also required. In your terminal, use
bcl client-token to generate a new one.
For the sample code below, we’ll use an address responsible for mining one of the earliest Bitcoin blocks,
When executed in the terminal, the above command should return data associated with the address, including the Bitcoin balance (and balance in satoshis), the total sent/received, and a list of the address transactions.
Because the above is a pre-fork address, we can query the Bitcoin Cash equivalent of the address well. All we have to do is change the command URL from
https://bch.connect… and swap the legacy address for its Bitcoin Cash counterpart:
With the REST API, there is an additional command to grab the information of multiple addresses simultaneously. This command requires a bit of extra attention, since you can’t put multiple data inputs (two or more addresses, in this case) directly into the URL. Instead, the command requires an additional data field to communicate a list of addresses.
With an added data field, the type of API call changes from
POST, because a data set is also supplied. The header must also include a line to specify how this posted data should be read.
Here’s what that looks like when we query for the above address as well as another early Bitcoin address,
And this is the sample code for running the call on the Bitcoin Cash counterparts to these addresses:
There are additional methods that can be called to grab specific pieces of data pertaining to addresses, like just the total sent and total received. To learn more about the REST API and all available commands, please see the Connect BTC / BCH REST API Technical Reference.
Just like the REST API command above, the sample code below takes the same early Bitcoin address from above,
1FvzCLoTPGANNjWoUo6jUGuAG3wg1w4YjR, and returns its balance (in satoshis):
Plug your “clientId” and “clientSecret” to the code above and package it as a .js file. In your terminal, navigate to the location of your file and execute the command with node
To see the balance in Bitcoin, rather than satoshis, divide the “balance” by 100,000,000 in the
console.log("Address Balance:", balance / 100000000)
To grab the balance from a Bitcoin Cash address, change the “coin” field to
bch in the header. Here’s what that code looks like with the Bitcoin Cash alternative of the previous address: