How can I know if any given transaction has confirmations and has paid the miner's fee, using the BlockChain.info DATA API?


If a transaction returned from the http://blockchain.info/rawtx/$tx_hash endpoint has a confirmation, it will have a block_height member. You can then calculate roughly its number of confirmations by subtracting that value from the latest height retrieved from the http://blockchain.info/latestblock endpoint.

Code example, in Ruby:

#!/usr/bin/env ruby
require "open-uri"
require "json"
# call this script with `ruby block_height.rb <tx_hash>`
tx = ARGV.shift
puts "Getting info for #{tx}..."
j = JSON.parse open("http://blockchain.info/rawtx/#{tx}").read
if j["block_height"]
  b = JSON.parse open("http://blockchain.info/latestblock").read
  puts "%d confirmations" % (b["height"] - j["block_height"] + 1)
  time_since = Time.now.gmtime.to_i - j["time"]
  puts "It's been #{time_since} seconds since the transaction was created."
  puts "It's not been ten minutes yet!" if time_since < 600
  puts "It's due any time now." if time_since >= 600

Calculating the transaction fee is best done by summing the inputs and outputs of a transaction, then subtracting the inputs from the outputs. Difference is the transaction fee.

| improve this answer | |
  • Hi Colin! Thanks a lot! Could you expand a bit on the block_height part? – flaab Jan 28 '13 at 15:51
  • Added a code example for you. – Colin Dean Jan 29 '13 at 2:54
  • verifies but just out of curiosity, how do I explain the relations between the number of confirmations, the tx block height and the latest block height? – user237419 Nov 2 '15 at 6:37
  • number of confirmations = latest block height - tx block height – Colin Dean Nov 6 '15 at 19:35

I have created a simple php function with my basic knowledge of php. here

function get_tx_confirmation($tx_hash_id){ $raw_lastest_block= json_decode(file_get_contents("https://blockchain.info/latestblock"), true); $lastest_block=$raw_lastest_block["height"];
$raw_tx=json_decode(file_get_contents("https://blockchain.info/rawtx/$tx_hash_id"), true); $tx_block_height=$raw_tx["block_height"]; $confirmations = $lastest_block - $tx_block_height +1; return $confirmations;

| improve this answer | |
  • This is just to get the amount of transactions using the hash id – Meet Electrode Jan 29 '17 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.