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.
Users can get this information with calls through the REST API or Javascript SDK. Follow the steps below to interact with Bloq Connect and make the queries yourself with these two toolkits.
REST API
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, 1FvzCLoTPGANNjWoUo6jUGuAG3wg1w4YjR
:
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://btc.connect…
to 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 GET
to 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, 15ubicBBWFnvoZLT7GiU2qxjRaKJPdkDMG
:
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.
Javascript SDK
Unlike the REST API, Javascript SDK calls don’t require an authentication token. Instead, users must supply their client id and client secret keys into the header of their Javascript file. To retrieve your client keys, please see this guide.
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 filename.js
.
To see the balance in Bitcoin, rather than satoshis, divide the “balance” by 100,000,000 in the console.log()
command: 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:
There are several additional Javascript methods to grab other information pertaining to addresses. In order to execute additional calls or query additional addresses through the Javascript SDK, add more methods to the existing file.
To learn more about the Javascript SDK and all available commands, please see the Javascript SDK Technical Reference.