Is there an example of how to get all the transaction data for a bitcoin address in json format?
6 Answers
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.
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 " ".$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 " ". rtrim(number_format($inValueCalc, 8), '0') ." ". $inAddy ." ";
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 " ". rtrim(number_format($outValueCalc, 8), '0') ." ". $outAddy ." ";
echo "<br>";
}
echo "</td></tr></table>";
}
}
?>
-
-
1"blockchain.info/address/…". 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. Commented Jul 5, 2017 at 6:34
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>";
}
?>
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'}
bicoin api - listreceivedbyaddress(minconf,include_empty,include_watchonly,address_filter)
return json:
[
{
address: '37ngGpLcqbgU3Bov8hjMSzgqBKfLyB3f9r',
amount: 0.0003039,
confirmations: 92,
label: '',
txids: [
'76acd9e2b7b79f68c9a4a3a4a5cce3528ee8ae6d383aed069bf03d5e95408ec7'
]
}
]
I couldn't get the transactions AND the total BTC that came in per transaction using the advice on pages like this. Luckily I stumbled upon this which solves it beautifully:
$address = 'YOUR BTC ADDRESS';
$transaction_list=array();
$satoshi=100000000;
$txnlist=file_get_contents("https://blockchain.info/rawaddr/".$address);
if($txnlist)
{
$txnlist=json_decode($txnlist,true);
if($txnlist && isset($txnlist['txs']) && $txnlist['txs'])
{
$txns=$txnlist['txs'];
foreach($txns as $txn)
{
$amount=$txn['result']/$satoshi;
$time=$txn['time'];
$hash=$txn['hash'];
$transaction_list[]=array(
'amount'=>$amount,
'hash'=>$hash,
'time'=>$time
);
}
}
$data['address']=$txnlist['address'];
$data['total_txn']=$txnlist['n_tx'];
$data['total_received']=$txnlist['total_received']/$satoshi;
$data['total_sent']=$txnlist['total_sent']/$satoshi;
$data['final_balance']=$txnlist['final_balance']/$satoshi;
$data['transaction_list']=$transaction_list;
}
print '<pre>';
print_r($data);
die();
The result:
(
[address] => XXXXXXXX
[total_txn] => 2
[total_received] => 0.00917981
[total_sent] => 0
[final_balance] => 0.00917981
[transaction_list] => Array
(
[0] => Array
(
[amount] => 0.00298651
[hash] => XXXXXXX
[time] => 1235576379
)
[1] => Array
(
[amount] => 0.0061933
[hash] => XXXXXXXXXX
[time] => 1632561967
)
)
)