How to get the list of transactions of input/outputs for a bitcoin address in JSON format?

0

1

Is there an example of how to get all the transaction data for a bitcoin address in json format?

Patoshi パトシ

Posted 2017-03-02T21:55:30.313

Reputation: 8 911

Answers

1

You can use api provided by blockchain.info.

For example use https://blockchain.info/rawaddr/$bitcoin_address to get all the transactions for an address in JSON format.

Abhishek Sinha

Posted 2017-03-02T21:55:30.313

Reputation: 171

2

As answered above you can use blockchain.info for extracting the results. Here I would like to add to the solution provided by m1xolyd1an, his code works fine but it extracts only latest 50 results/transactions made by the address that you are trying to retrieve the data for.

In order to extract the complete details of the transaction right from the beginning, some minor tweaks have to be made to the code. Below is my solution

<?php
$address = $_POST['Address'];
$url = "https://blockchain.info/address/".$address."?format=json&offset=0";
$json = json_decode(file_get_contents($url), true);

$totalTxs = $json["n_tx"];
echo "Total transaction : $totalTxs";
for($ex=0;$ex<$totalTxs;$ex+=50){
//$address = "1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36v";
$url = "https://blockchain.info/address/".$address."?format=json&offset=$ex";
$json = json_decode(file_get_contents($url), true);

//total transactions
$totalTxs = $json["n_tx"];
//final balance
$balanceSatoshis = $json["final_balance"];
$balanceBitcoins = $balanceSatoshis / 100000000;
$balanceBitcoins = number_format($balanceBitcoins, 8);

//loop through each transaction and display all inputs and outs
for($i=0;$i<50;$i++){

echo "<table><tr><td>";
echo "HASH OF TX:</br>";
$hash=$json["txs"][$i]["hash"];
echo "&nbsp;".$hash;

echo "</td><td width='550'>SENT FROM:<br>";
$n_inputs = count($json["txs"][$i]["inputs"]);  

for($ii = 0; $ii < $n_inputs; $ii++){   
    $inValue = $json["txs"][$i]["inputs"][$ii]["prev_out"]["value"];    
    $inValueCalc = $inValue / 100000000;    
    $inAddy = $json["txs"][$i]["inputs"][$ii]["prev_out"]["addr"];  
    echo "&nbsp;". rtrim(number_format($inValueCalc, 8), '0') ."&nbsp;". $inAddy ."&nbsp;"; 
    echo "<br>";
    }   

echo "</td><td>SENT TO:<br>";
$n_outputs = count($json["txs"][$i]["out"]);    

for($io = 0; $io < $n_outputs; $io++){  
    $outValue = $json["txs"][$i]["out"][$io]["value"];  
    $outValueCalc = $outValue / 100000000;  
    $outAddy = $json["txs"][$i]["out"][$io]["addr"];    
    echo "&nbsp;". rtrim(number_format($outValueCalc, 8), '0') ."&nbsp;". $outAddy ."&nbsp;";   
    echo "<br>";    
    }   
echo "</td></tr></table>";
}
}
?>

Nikhil Jain

Posted 2017-03-02T21:55:30.313

Reputation: 151

what was the trick you used to bypass the 50 limit?Patoshi パトシ 2017-07-05T05:09:32.433

1

"https://blockchain.info/address/".$address."?format=json&amp;offset=0". I used the offset to get the desired results. As in when the offset is set to "0" it would fetch you latest 50 results. If you go on increasing the offset by 50, then subsequently you would be getting previous results from the earlier transactions. Blockchain.info sends you the results of first page when you query with offset "0". When you query with offset ="50" it would send you the results from second page and so on. So what I basically did was iterating over the offset till I reach the end or total no of transactions.

Nikhil Jain 2017-07-05T06:34:19.493

0

You can use blockr API.

http://btc.blockr.io/api/v1/address/txs/the_address 

Where the_address is the address you are looking information about.

However, this is limited to the 200 most recent transactions.

Here you have a simple Python example that does so:

from json import loads
from requests import get

url = 'http://btc.blockr.io/api/v1/address/txs/'
btc_addr = '36YKytAqTfq5FbjrBqMwnEdS62eZjB7DV6'

r = get(url + btc_addr)
data = loads(r.content)['data']

print data

Where 36YKytAqTfq5FbjrBqMwnEdS62eZjB7DV6 is a random picked address. The result wil be:

{u'txs':[
   {u'time_utc': u'2017-03-03T08:22:57Z', u'amount': 5,  u'confirmations': 4, u'amount_multisig': 0, 
     u'tx': u'fc8bd6d4bff2c5c07f82939cbf798f4442226f39b607353fe8355cf3170d67b7'}, 
   {u'time_utc': u'2017-01-03T17:14:19Z', u'amount': -10, u'confirmations': 9084, u'amount_multisig': 0, 
     u'tx': u'daa9f8c25ce09240a02df1ac56122bb014eb9f6abe5a22bbfe04ee4fa1afb9cc'}, 
   {u'time_utc': u'2017-01-03T11:10:21Z', u'amount': 10, u'confirmations': 9129, u'amount_multisig': 0, 
     u'tx': u'13362f7a746cfe6481e08155d778bba69c8db706673b99239762f5dca14f18f4'}
], u'limit_txs': 200, u'nb_txs': 3, u'nb_txs_displayed': 3, u'address': u'36YKytAqTfq5FbjrBqMwnEdS62eZjB7DV6'}

sr-gi

Posted 2017-03-02T21:55:30.313

Reputation: 2 382

the link to blockr is redirecting to coinbase.Kaizoku Gambare 2017-10-21T09:15:10.263

blockr.io have shutdown their server recently.sr-gi 2017-10-21T19:07:57.810

0

Your question is tagged with blockchain.info so you can just use the endpoint https://blockchain.info/address/1ADDRESSTOLOOKUPGOESHERE?format=json

The output will provide an initial summary on the address, followed by each transaction in the txs[] array with all inputs and outputs. You also requested an example so below is a snippet response.

{
    "hash160":"eaad40023319c547321b63f8adc6cc5a11759c61",
    "address":"1NPrfWgJfkANmd1jt88A141PjhiarT8d9U",
    "n_tx":159,
    "total_received":261352990,
    "total_sent":259703473,
    "final_balance":1649517,
    "txs":[

{
   "ver":1,
   "inputs":[
      {
         "sequence":4294967295,
         "prev_out":{
            "spent":true,
            "tx_index":221676260,
            "type":0,
            "addr":"19hNEoHRGb2wDpxWGgDEDwaoHdEfKjZwWx",
            "value":3214191,
            "n":20,
            "script":"76a9145f635f8cf8ad279cd74eb24a44cd128973e00a0e88ac"
         },
         "script":"47304402207edc4373fbb9a01ac6e07eb88f91c7008e2ed951603a3ce03393d2bf7e99bcbd022060963067a9559cc8b140136dc4f78819731260151ed9d81c19c25ffd8df42b7a01410497e0923c9b2ea5261733f81fa83333d25373db3feae91e15ae42f5347e8f65080c90b33cba4470e5eabec06db30a57b4017ad3588a26722e36249eb5c6679a39"
      }
   ],
   "block_height":454724,
   "relayed_by":"87.128.111.190",
   "out":[
      {
         "addr_tag_link":"http:\/\/wearechange.org\/donate\/",
         "addr_tag":"wearechange.org",
         "spent":false,
         "tx_index":225606846,
         "type":0,
         "addr":"12HdLgeeuA87t2JU8m4tbRo247Yj5u2TVP",
         "value":166948,
         "n":0,
         "script":"76a9140e1d1da1fc5bb5165a54a4d9ecefbc8458bae3d388ac"
      }
   ],

So let's say you're using PHP and you want to fetch and display all the transactions for a particular address. You'd call the API then loop through each in the array.

http://phpfiddle.org/main/code/e4jy-rrqt

<?php

$address = "1NPrfWgJfkANmd1jt88A141PjhiarT8d9U";
$url = "https://blockchain.info/address/".$address."?format=json";
$json = json_decode(file_get_contents($url), true);

//total transactions
$totalTxs = $json["n_tx"];
//final balance
$balanceSatoshis = $json["final_balance"];
$balanceBitcoins = $balanceSatoshis / 100000000;
$balanceBitcoins = number_format($balanceBitcoins, 8);

//loop through each transaction and display all inputs and outs
for($i=0;$i<$totalTxs;$i++){

    echo "<table><tr><td width='550'>";
    echo "SENT FROM:<br>";
    $n_inputs = count($json["txs"][$i]["inputs"]);  

    for($ii = 0; $ii < $n_inputs; $ii++){   
        $inValue = $json["txs"][$i]["inputs"][$ii]["prev_out"]["value"];    
        $inValueCalc = $inValue / 100000000;    
        $inAddy = $json["txs"][$i]["inputs"][$ii]["prev_out"]["addr"];  
        echo "<button style='background-color:red;'>". rtrim(number_format($inValueCalc, 8), '0') ."</button><a href='#'>". $inAddy ."</a>";    
        echo "<br>";
        }   
    echo "</td><td>SENT TO:<br>";
    $n_outputs = count($json["txs"][$i]["out"]);    

    for($io = 0; $io < $n_outputs; $io++){  
        $outValue = $json["txs"][$i]["out"][$io]["value"];  
        $outValueCalc = $outValue / 100000000;  
        $outAddy = $json["txs"][$i]["out"][$io]["addr"];    
        echo "<button style='background-color:green;'>". rtrim(number_format($outValueCalc, 8), '0') ."</button><a href='#'>". $outAddy ."</a>";    
        echo "<br>";    
        }   
    echo "</td></tr></table>";
}


?>

m1xolyd1an

Posted 2017-03-02T21:55:30.313

Reputation: 3 356