Using The Blockcypher API To Interact With The Bitcoin Blockchain

Vincent T.
0xCODE
Published in
5 min readNov 24, 2019
API calls to the Bitcoin blockchain.

Blockcypher provides a set of API for developers to interact with the Bitcoin blockchain. The API are based on JSON (Javascript Object Notation) and RESTful, providing HTTP or HTTPS methods used in web based applications. The current API support for the Bitcoin blockchain allows developers to gather information about transactions, wallets and blocks among other objects.

I will go over 4 methods of endpoints used in the blockchain API. The API request is sent to the Blockcypher website ‘api.blockcypher.com’. This does not require any additional software, just a terminal if you are using Linux or macOS. If you are a Windows user, you can follow this guide on how to install cURL.

Chain Endpoint

The most basic command can be run from a terminal prompt using curl.

curl https://api.blockcypher.com/v1/btc/main

This results in the following output:

This is called the chain endpoint. The returned information about the block includes the following attributes:

name — The network the block belongs to (i.e. “BTC.main”). This is the Bitcoin main network blockchain.

height — This refers to the block height or the block number n from the genesis block.

hash — This is the SHA256 hash of the block.

time — This is the block generation timestamp with the date.

latest_url — The link from the Blockcypher website (the URL includes the hash value).

previous_hash — The hash of the previous block.

previous_url — The link for the previous block.

peer_count — N/A

unconfirmed_count — Number of transactions in the memory pool.

high_fee_per_kb — The rolling average fee (in satoshis) paid per kilobyte for transactions to be confirmed within 1 to 2 blocks.

medium_fee_per_kb — The rolling average of the fee (in satoshis) paid per kilobyte for transactions to be confirmed within 3 to 6 blocks.

low_fee_per_kb — The rolling average fee (in satoshis) paid per kilobyte for transactions to be confirmed in 7 or more blocks.

last_fork_height — Current height of the latest fork on the blockchain.

last_fork_hash — Hash of the latest confirmed block in the latest fork of the blockchain.

This is a general command to give users the latest block information on the Bitcoin blockchain. Values in the attributes will vary. On average, the Bitcoin network generates a block every 10 minutes (give or take depending on the difficulty target).

Block Hash Endpoint

For more information on a specific block, the block hash endpoint can be used. This uses a curl GET method when submitted to the API website. The return object is the information for the block.

/blocks/<BLOCK_HASH>

The <BLOCK_HASH> is the specific hash of the block that a user wants to query information about. It is a string data type.

Suppose the block hash is: 0000000000000000000f15130ad31a341f4bf82752d38d8ddf8310daa21aebc0

Then the block hash will be our value to the argument.

curl https://api.blockcypher.com/v1/btc/main/blocks/0000000000000000000f15130ad31a341f4bf82752d38d8ddf8310daa21aebc0

This results in the following:

The displayed information about the block is more specific. This includes the height, the total amount of satoshis transacted, the total number of transactions and the transaction hashes. The transactions are identified as ‘txids’ and listed by their hashes (each transaction ID is a hash of the transaction). By default the list includes a total of 20 transactions with an option to the next 20 if there are more by the ‘next_txids’ URL link.

By using this method, a specific transaction that is based on its hash can be traced to the block it belongs to. For example, if we were to look up a specific transaction using the Bitcoin Block Explorer:

af1aaee970528d6a1bad73536448acfbb18ebed522bee9cd4a95d78cb59f5644

Results from the Block Explorer.

As we can see here, the ‘Included in Block’ shows the block height we got from the curl method 604996 (also called the block height). The Block Explorer verifies the same information we can get using the block hash endpoint method. From there we can see the transactions that were validated inside the specific block.

Block Height Endpoint

This method will query the blockchain using the block height endpoint GET method. Just like with the hash, the block height can used as the criteria for the query.

/blocks/<BLOCK_HEIGHT>

The <BLOCK_HEIGHT> is the height of the block or block number (from its canonical order from the genesis block). This is an integer data type.

This will generate the same output as the block hash endpoint method, but we can use flags to refine our query. We can use two integer data types called txstart and limit.

Here is a brief description of these flags:

txstart — Filters to include transaction hashes after ‘txstart’ in the block (begins with value of 0).

limit — Filters to only include a maximum ‘limit’ of transaction hashes in the block. Maximum value is 500.

Let’s try this method to post to the API.

curl 'https://api.blockcypher.com/v1/btc/main/blocks/604996?txstart=1&limit=2'

The result will return 2 transactions as specified in the ‘limit = 2’ starting from ‘txstart = 1’ which is the second transaction ID (transactions ID starts with a value of 0 for the first transaction). This is why we see that the result for ‘txids’ returns the second transaction ID as the first in the list. This is one way to help trim down a query.

Feature Endpoint

According to Blockcypher, this method can be used to gather information about “upgrade features on a blockchain”. It returns information about its state on the network using the value for <NAME>. An example is BIP65 on bitcoin, a Bitcoin Improvement Proposal.

/feature/<NAME>

We can use this GET method to query about the feature:

curl
https://api.blockcypher.com/v1/btc/main/feature/bip65?token=YOURTOKEN

This shows the BIPxx (where xx is an integer value) current state and some additional details. This is subject to change without any prior notice as the proposals and features in Bitcoin evolve.

For further developer resources:

--

--

Vincent T.
0xCODE

Blockchain, AI, DevOps, Cybersecurity, Software Development, Engineering, Photography, Technology