Cover Image for Bitcoin API: Exploring BTC Transfers, Block Rewards, Havling and much more

Bitcoin API: Exploring BTC Transfers, Block Rewards, Havling and much more

UTXO
API

In the domain of digital finance, few innovations have captivated the world's attention like Bitcoin. Born out of the desire for decentralized currency, Bitcoin has grown from a mere concept into a global phenomenon, challenging traditional financial systems and sparking a revolution in how we perceive and interact with money. Bitcoin is the precursor for the rapid strides in blockchain technology of today and has given rise to a number of crypto networks and currencies. This innovation has fueled eager developers to tap into Bitcoin’s potential and interact with the network to build smart applications.

Bitcoin APIs, developed by Bitquery (a leading indexer and provider of crypto products), allows developers to integrate Bitcoin functionality seamlessly into their own services or applications.

Let's delve into the origins of Bitcoin and explore the capabilities of Bitcoin APIs in this article.

What is Bitcoin?

Bitcoin was first discussed in a whitepaper, Bitcoin: A Peer-to-Peer Electronic Cash System, published on Bitcoin.org. Introduced by Satoshi Nakamoto (a pseudonym for an individual or a group of developers) in 2009, Bitcoin is a cryptocurrency or a digital currency that acts as a payment method for financial transactions. When divided into smaller bits known as “satoshis”, bitcoins can be enabled for payments.

Bitcoin operates on a decentralized ledger system called the blockchain and is a proof-of-work consensus system. This is the first ever decentralized cryptocurrency that uses the blockchain technology. Because of this, Bitcoin enables safe and secure transactions on the network.

The Bitcoin network is open source, which means it’s publicly accessible and open for contributions. The currency native to the network is known as bitcoin (note the lowercase), with the symbol BTC.

Architecture

The basic architecture of the Bitcoin network consists of:

  • Network layer: Acts as a bridge between blockchain network nodes
  • Consensus layer: Ensures that the network nodes are in agreement regarding the network status
  • Application layer: Used for application development and deployment. For example, deployment of smart contracts and decentralized applications (dApps). Some of the use cases include the Lightning Network, Bitcoin Ordinals or Bitcoin L2s.

How Does Bitcoin Work?

The following components make up the decentralized system of Bitcoin:

  • The Bitcoin network - a P2P (peer-to peer) network where people can directly run and validate transactions
  • The Bitcoin blockchain (public ledger) - this ensures that the transactions are stored, validated, and verified
  • BTC, the native cryptocurrency - this is the currency that is traded on the Bitcoin network

Here are the broad steps involved in Bitcoin transactions:

  • Creation of wallets for storing bitcoins
  • Initiation of transactions for broadcasting to network
  • Validation and verification of transactions through bitcoin mining
  • Addition of verified transactions to a block

Exploring Bitcoin Network Through Bitquery APIs

Bitcoin Block Explorer API: Overview of Bitcoin Mainnet

Bitquery provides a variety of APIs to extract real-time or historic Bitcoin chain data. Use the Bitquery Explorer to track bitcoin transactions, bitcoin transfers, transaction fees, blocks, bitcoin block rewards, and more.

Bitquery Bitcoin Explorer

To embed queries in your applications, simply click the Get API on the bottom right of the screen (check the above image). To write your own queries, use Bitquery’s in-built GraphQL IDE.

Let’s delve into some of the Bitquery APIs to track Bitcoin transactions and more here. Bitcoin wallet APIs enable you to track bitcoin addresses, view bitcoin balances, check bitcoin transactions and transfers.

Address Stats API

Obtaining statistics of a Bitcoin address

This API retrieves statistics related to Bitcoin addresses. To obtain the statistics for a specific address, use the following query in IDE. For example, this query retrieves the statistics of a specific Bitcoin address, bc1qka60n3wyl0gkdla7kpnhm2597lh652jjycpue6. Specify the network as “bitcoin” and the address as applicable.

{
  bitcoin(network: bitcoin) {
    addressStats(address: {is: "bc1qka60n3wyl0gkdla7kpnhm2597lh652jjycpue6"}) {
      address {
        address
        annotation
        balance
        inboundTransactions
        inflows
        outboundTransactions
        outflows
        uniqueDaysWithTransfers
        uniqueReceivers
        uniqueSenders
      }
    }
  }
}

Blocks API

Obtaining details of all blocks on the Bitcoin chain

You can get the details of all blocks on the Bitcoin chain with this API. For example, to retrieve the details of all the blocks on Bitcoin in January, use this query. Specify the network as “bitcoin” and the dates as applicable (01/01 to 01/31).

query ($network: BitcoinNetwork!, $limit: Int!, $offset: Int!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    blocks(
      options: {desc: "height", limit: $limit, offset: $offset}
      date: {since: $from, till: $till}
    ) {
      timestamp {
        time(format: "%Y-%m-%d %H:%M:%S")
      }
      height
      difficulty
      transactionCount
      blockSizeBigInt
      blockHash
      blockSize
      blockSizeBigInt
      blockStrippedSize
      blockVersion
      blockWeight
      chainwork
      difficulty
      
    }
  }
}

Obtaining details of a specific block

To get a specific block’s data, simply specify the “height” parameter with the required block ID in the above query. For example, in this query, we are retrieving the details of the block 829329. Specify the network as “bitcoin” and the height parameter as “829329”. This gives you the current details of this block.

query ($network: BitcoinNetwork!, $height: Int!) {
  bitcoin(network: $network) {
    blocks(height: {is: $height}) {
      timestamp {
        time(format: "%Y-%m-%d %H:%M:%S")
      }
      medianTime {
        time(format: "%Y-%m-%d %H:%M:%S")
      }
      blockHash
      blockSizeBigInt
      blockStrippedSize
      blockVersion
      blockWeight
      chainwork
      difficulty
      transactionCount
    }
  }
}

Coinpath API

Tracking inflow/outflow of money

To find out the money flow between addresses, use the Coinpath API. This will retrieve data related to the amount transacted, number of transactions, amount sent or received, sender and receivers.

For example, to obtain the inputs and outflow of money for this address, 038TPWyir3ycYVkYR93rxJP7gietHWbVNQX since 15th January 2024, use this query. Specify the network as “bitcoin”, address as “038TPWyir3ycYVkYR93rxJP7gietHWbVNQX “, and the dates as applicable.

query ($network: BitcoinNetwork!, $address: String!, $inboundDepth: Int!, $outboundDepth: Int!, $limit: Int!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    inbound: coinpath(
      initialAddress: {is: $address}
      depth: {lteq: $inboundDepth}
      options: {direction: inbound, asc: "depth", desc: "amount", limitBy: {each: "depth", limit: $limit}}
      date: {since: $from, till: $till}
    ) {
      sender {
        address
        annotation
      }
      receiver {
        address
        annotation
      }
      amount
      depth
      count
    }
    outbound: coinpath(
      initialAddress: {is: $address}
      depth: {lteq: $outboundDepth}
      options: {asc: "depth", desc: "amount", limitBy: {each: "depth", limit: $limit}}
      date: {since: $from, till: $till}
    ) {
      sender {
        address
        annotation
      }
      receiver {
        address
        annotation
      }
      amount
      depth
      count
    }
  }
}

Check out a visual representation of the same here.

Inputs API

Extracting input details of a transaction

To obtain the input details of a transaction on Bitcoin, use this API. For example, let’s retrieve the inputs of a specific block, say, 829337 after 1st of Dec 2023 in this query.

Specify the network as “bitcoin, height as “ 829337“ , and the dates as applicable.

{
  bitcoin {
    inputs(
      options: {limit: 50, desc: "block.timestamp.iso8601"}
      date: {after: "2023-12-01T00:00:00Z"}
      height: {is: 829337}
    ) {
      block {
        height
        timestamp {
          iso8601
        }
      }
      inputIndex
      inputScript
      inputAddress {
        address
        annotation
      }
      outputTransaction {
        hash
      }
      transaction {
        hash
      }
      value
    }
  }
}

Outputs API

Fetching output details of a transaction

This API gives you the details of all the outputs of a transaction on the Bitcoin network. You can further filter the data to obtain the outputs of a particular block. In this query, we will retrieve transaction outputs (20) for the block, 829337 after 15th Dec 2023. Specify the network as “bitcoin” , height as 829337 and the date as applicable.

{
  bitcoin {
    outputs(
      date: {after: "2023-12-15"}
      options: {limit: 20, desc: "block.timestamp.iso8601"}
      height: {is: 829337}
    ) {
      block {
        timestamp {
          iso8601
        }
      }
      date {
        date
      }
      outputAddress {
        address
        annotation
      }
      outputDirection
      outputIndex
      outputScript
      outputScriptType {
        annotation
        pattern
        short
        simplePattern
        type
      }
      reqSigs
      transaction {
        hash
        index
      }
      value
    }
  }
}

OmniTransactions API

Retrieving details of Omni transactions

Bitcoin Omni transactions are transactions that are built on the Omni layer protocol on top of the Bitcoin blockchain. They enable advanced features on the Bitcoin blockchain, such as transactions (issue, send, trade) of digital assets, tokens, and smart contracts.

The OmniTransactions API fetches details of omni transactions on the Bitcoin network.

For example, in this query, we are fetching details of all omni transactions on Bitcoin after 1st Dec 2023. Specify the network as “bitcoin” and the date as applicable.

{
  bitcoin {
    omniTransactions(
      options: {limit: 10, desc: "block.timestamp.iso8601"}
      date: {after: "2023-12-01"}
    ) {
      block {
        timestamp {
          iso8601
        }
      }
      blockHash
      feeValue
      hash
      index
      json
      txSender
      type
      typeInt
      valid
      version
    }
  }
}

OmniTransfers API

Fetching details of Omni transfers

Bitcoin Omni transfers refer to transactions involving tokens created and managed on the Omni Layer protocol, which operates on top of the Bitcoin blockchain. This API fetches details of omni transfers on the Bitcoin network.

For example, this query returns details of all omni transfers on Bitcoin after 15th Dec 2023. pecify the network as “bitcoin” and the date as applicable.

{
  bitcoin {
    omniTransfers(
      date: {after: "2023-12-15"}
      options: {desc: "block.timestamp.iso8601", limit: 10}
    ) {
      block {
        timestamp {
          iso8601
        }
      }
      blockHash
      currency {
        address
        name
        symbol
      }
      date {
        date
      }
      direction
      divisible
      hash
      index
      ismine
      transferFrom
      transferTo
      txSender
      type
      typeInt
      value
    }
  }
}

Transactions API

This API allows you to track bitcoin transactions on the Bitcoin network.

Viewing daily bitcoin transaction count

Let’s get the number of daily transactions per day for the last 10 days using this simple query. Specify the network as “bitcoin” , and the count parameter as shown here.

query {
  bitcoin {
    transactions(options: {desc: "date.date", limit: 10}) {
      date {
        date
      }
      count
    }
  }
}

Fetching inputs and outputs of a transaction

You can also get inputs and outputs of a Bitcoin transaction using this API. For example, here is the query which retrieves the current inputs and outputs of a specific address ae70f479da86601a656511790a536315faf8a1f05f5132efa0e9edf8d78ab4e8. Specify the network as “bitcoin” and the address as ae70f479da86601a656511790a536315faf8a1f05f5132efa0e9edf8d78ab4e8.

{
  bitcoin(network: bitcoin) {
    inputs(
      txHash: {is: "ae70f479da86601a656511790a536315faf8a1f05f5132efa0e9edf8d78ab4e8"}
      options: {asc: "inputIndex"}
    ) {
      inputIndex
      block {
        height
        timestamp {
          time(format: "%y-%d-%m")
        }
      }
      address: inputAddress {
        address
        annotation
      }
      value
      value_usd: value(in: USD)
      inputScript
      inputScriptType {
        pattern
        simplePattern
      }
      outputTransaction {
        hash
      }
      transaction {
        hash
      }
      value
    }
    outputs(
      txHash: {is: "ae70f479da86601a656511790a536315faf8a1f05f5132efa0e9edf8d78ab4e8"}
      options: {asc: "outputIndex"}
    ) {
      outputIndex
      address: outputAddress {
        address
        annotation
      }
      value
      value_usd: value(in: USD)
      outputDirection
      block {
        height
        timestamp {
          time(format: "%y-%d-%m")
        }
      }
      outputDirection
      outputScript
      outputScriptType {
        pattern
        short
        simplePattern
        type
      }
      reqSigs
      transaction {
        hash
      }
    }
  }
}

Checking bitcoin transactions by date

You can check for the number of transactions on the Bitcoin network by date. For example, let’s fetch the number of transactions for 7th Feb 2024. Build this query in IDE as shown. Set the “since” and “till”parameters to 2024-02-07. Specify the network as “bitcoin” to get the number of transactions on that day.

query ($network: BitcoinNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    transactions(options: {asc: "date.date"}, date: {since: $from, till: $till}) {
      date: date {
        date(format: $dateFormat)
      }
      count: countBigInt
    }
  }
}

Tracking bitcoin transaction fee by date

Now, let’s filter the above query to view the total transaction fees for transactions on 7th Feb 2024. Add the parameter “feeValue” to the query as shown. Set the date parameter as before.

query ($network: BitcoinNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    transactions(options: {asc: "date.date"}, date: {since: $from, till: $till}) {
      date: date {
        date(format: $dateFormat)
      }
      count: countBigInt
      feeValue
      avgFee: feeValue(calculate: average)
    }
  }
}

Retrieving bitcoin transaction scripts

In the Bitcoin network, input and output scripts are fundamental components of transactions that define how bitcoins are spent and where they are sent. These scripts are important for validation of transactions. Through Bitquery APIs, you can extract the input/output scripts for a transaction on Bitcoin.

For example, let’s retrieve all the output scripts for a specific transaction, say, 4e4fee030ef37f16631063488f65382522d73c6dd311669f3f4cc6b5cf2ba49a. Set the address (hash) parameter in the query here as shown. Specify the network as “bitcoin”.

query ($network: BitcoinNetwork!, $hash: String!, $limit: Int!, $offset: Int!) {
  bitcoin(network: $network) {
    outputs(
      txHash: {is: $hash}
      options: {asc: "outputIndex", limit: $limit, offset: $offset}
    ) {
      outputIndex
      address: outputAddress {
        address
        annotation
      }
      value
      value_usd: value(in: USD)
      outputDirection
      outputScriptType {
        annotation
        pattern
        type
      }
      outputScript
    }
  }
}

Similarly, you can query for input scripts by using the “inputs” parameter in the query.

Extracting number of BTC transactions

To obtain transactions of BTC, the native crypto on the Bitcoin network, use this API. For example, in this query, let’s get all the BTC transactions (both inbound and outbound) for the period from 1st Feb 2024 to 9th Feb 2024. Specify the network as “bitcoin” and the dates as given.

query ($network: BitcoinNetwork!, $address: String!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    inputs(
      date: {since: $from, till: $till}
      inputAddress: {is: $address}
      options: {asc: "date.date"}
    ) {
      date {
        date(format: $dateFormat)
      }
      value
    }
    outputs(
      date: {since: $from, till: $till}
      outputAddress: {is: $address}
      options: {asc: "date.date"}
    ) {
      date {
        date(format: $dateFormat)
      }
      value
    }
  }
}

You can check only for inbound or only for outbound by modifying the above query. Simply use either the inputs or the outputs parameter as required.

Mining API

The bitcoin mining API extracts mining information on the Bitcoin network, including the number of miners, blocks produced by them, mining rewards and so on.

Obtaining unique miners on Bitcoin

Want to know the number of miners or number of unique miners on the Bitcoin network? Simple. Build this query in IDE. Here, we will retrieve the number of unique miners for a period of two weeks, say 24h Jan 2024 to 7th Feb 2024. Specify the network as “bitcoin” and the dates as given.

query ($network: BitcoinNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    outputs(
      options: {asc: "date.date"}
      date: {since: $from, till: $till}
      txIndex: {is: 0}
      outputDirection: {is: mining}
      outputScriptType: {notIn: ["nulldata", "nonstandard"]}
    ) {
      date: date {
        date(format: $dateFormat)
      }
      count: countBigInt(uniq: addresses)
      value
    }
  }
}

**Checking mined bitcoin value **

Additionally, you can also query for the mined value on the Bitcoin network. To do so, use this query. This query returns the mined value per day for 2 weeks as before (say 24h Jan 2024 to 7th Feb 2024). Specify the network as “bitcoin” and the dates as given.

query ($network: BitcoinNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    outputs(
      options: {asc: "date.date"}
      date: {since: $from, till: $till}
      txIndex: {is: 0}
      outputDirection: {is: mining}
      outputScriptType: {notIn: ["nulldata", "nonstandard"]}
    ) {
      date: date {
        date(format: $dateFormat)
      }
      count: countBigInt(uniq: addresses)
      value
    }
  }
}

You can also obtain mined value per block by specifying “uniq: blocks” for the count parameter in the above query.

Viewing number of mined blocks

This API also enables you to get data on the number of blocks produced by miners over a period of time.

To find out how many blocks were mined during a period, use this query. For example, here we are retrieving the number of blocks mined during a week (01-07 Feb 2024). Specify the network as “bitcoin” and the dates as applicable.

query ($network: BitcoinNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    outputs(
      options: {asc: "date.date"}
      date: {since: $from, till: $till}
      txIndex: {is: 0}
      outputDirection: {is: mining}
      outputScriptType: {notIn: ["nulldata", "nonstandard"]}
    ) {
      address: outputAddress {
        address
        annotation
      }
      date {
        date(format: $dateFormat)
      }
      reward: value
      count(uniq: blocks)
    }
  }
}

Similarly, you can get the number of transactions by miners, number of addresses on miners etc.

Retrieving number of bitcoin block rewards

This API returns information about Bitcoin mining rewards. Specifically, it retrieves data about outputs from mining transactions on the Bitcoin network within a specified time range.

For example, this query fetches the mining reward data from January 2024. Specify the network as “bitcoin”, and dates as applicable.

query ($network: BitcoinNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    outputs(
      options: {asc: "date.date"}
      date: {since: $from, till: $till}
      txIndex: {is: 0}
      outputDirection: {is: mining}
      outputScriptType: {notIn: ["nulldata", "nonstandard"]}
    ) {
      address: outputAddress {
        address
        annotation
      }
      date {
        date(format: $dateFormat)
      }
      reward: value
      count(uniq: blocks)
    }
  }
}

Similarly, you can also obtain reward data for a specific block by specifying the height parameter to the one you require.

Bitcoin Halving

Bitcoin “halving” is a prominent event on the Bitcoin blockchain network, which happens once in 4 years. During a halving event, the rewards that Bitcoin miners receive for successfully mining a new block is reduced by half. Halving cuts the issuance rate of new bitcoins, thereby lowering or limiting the supply of new ones.

The recent halving event took place on May 11 2020 and the next one is likely to occur early or mid 2024, where the block rewards are expected to fall further.

Let’s demonstrate halving using Bitquery Mining API.

Query 1: In this query, let’s check the mining rewards for a specific block, say 616295 on 6 Feb 2020 (the last halving event). Set the height parameter to 616295 and the date as 6th Feb 2020. Run the query and note the reward amount.

query ($network: BitcoinNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    outputs(
      options: {asc: "date.date"}
      date: {since: $from, till: $till}
      txIndex: {is: 0}
      outputDirection: {is: mining}
      outputScriptType: {notIn: ["nulldata", "nonstandard"]}
      height: {is: 616295}
    ) {
      address: outputAddress {
        address
        annotation
      }
      date {
        date(format: $dateFormat)
      }
      reward: value
      count(uniq: blocks)
    }
  }
}

Query 2: Now, do the same for Feb 2024. For example, let’s check the mining reward for the block 829623 on Feb 9 2024. In this query, set the height parameter to 829623 and the date as 9th Feb 2024. Run the query and note the reward amount as shown in the Results column.

query ($network: BitcoinNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  bitcoin(network: $network) {
    outputs(
      options: {asc: "date.date"}
      date: {since: $from, till: $till}
      txIndex: {is: 0}
      outputDirection: {is: mining}
      outputScriptType: {notIn: ["nulldata", "nonstandard"]}
      height: {is: 829623}
    ) {
      address: outputAddress {
        address
        annotation
      }
      date {
        date(format: $dateFormat)
      }
      reward: value
      count(uniq: blocks)
    }
  }
}

As you can see from the above results, the mining rewards from 2020 to 2024 have been reduced in half.

Conclusion

As the Bitcoin ecosystem continues to evolve, so too will the capabilities of Bitcoin APIs. With the advent of technologies such as the Lightning Network for scalable micropayments and smart contracts on Bitcoin, developers can expect to see a new wave of APIs that enable even more innovative use cases and applications.

Developers can leverage the power of these new APIs for building a decentralized network or other innovative crypto applications. By leveraging Bitquery APIs, developers and analysts can gain valuable insights into the dynamics of the Bitcoin network, halving events, mining rewards, and more.

About Bitquery

Bitquery is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. Our products are:

  • Coinpath® APIs provide blockchain money flow analysis for more than 24 blockchains. With Coinpath’s APIs, you can monitor blockchain transactions, investigate crypto crimes such as bitcoin money laundering, and create crypto forensics tools. Read this to get started with Coinpath®.
  • Digital Assets API provides index information related to all major cryptocurrencies, coins, and tokens.
  • DEX API provides real-time deposits and transactions, trades, and other related data on different DEX protocols like Uniswap, Kyber Network, Airswap, Matching Network, etc.

If you have any questions about our products, ask them on our Telegram channel or email us at sales@bitquery.io. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.

This article has been written by guest writer Aparna Sridhar

Subscribe to our newsletter

Subscribe and never miss any updates related to our APIs, new developments & latest news etc. Our newsletter is sent once a week on Monday.