Problem Formulation
Given a single Bitcoin address in the form of a stream of characters such as the address of the first “Genesis” block ever created on the Bitcoin blockchain:
12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX
How to get all transactions for this BTC address in a Python script?
Solution
To get all transactions of a given Bitcoin address, import the pandas library and call pandas.read_json(url)
to create a pandas DataFrame from the JSON object. You can then access the 'txs'
column on the DataFrame df
using indexing df['txs']
to get a series of all transactions concerning this address.
The following code:
- Imports the
pandas
library. - Creates the URL of the Bitcoin address using
blockchain.info
as a server. - Converts the JSON object from the server to a DataFrame using
pandas.read_json(url)
. - Prints the
'txs'
column of the DataFrame.
import pandas your_btc_address = '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX' # Genesis Block transactions_url = 'https://blockchain.info/rawaddr/' + your_btc_address df = pandas.read_json(transactions_url) transactions = df['txs'] print(transactions)
The output is a series of transactions:
0 {'hash': '367c89950f2865a2c07feb84def6d9fb061b... 1 {'hash': 'e585ec92476a3e867eb991ec1115788ef684... 2 {'hash': '8241cb1f6a48879fb712b398c6d0e1bba88a... 3 {'hash': 'a0b98914bb4ce5660e1ce3bafd268a0b159a... 4 {'hash': '640b727abd8605031e86fc2a2fa1fe41d607... ... 95 {'hash': '9fefa807ab8791b6a4fca5f1d8de1b5fd012... 96 {'hash': '56484b549f42a4485fb79b2838c7829805d0... 97 {'hash': 'ddd78924a1e15ad98b28342987d266e95bad... 98 {'hash': '0cabee6178c031c259609ce33864e5db60f1... 99 {'hash': 'ca0eac93cb73d28872aa2ce644a2f1cfbbe2... Name: txs, Length: 100, dtype: object
To explore them even further, you can keep using indexing like so:
print(transactions[0])
{'hash': '367c89950f2865a2c07feb84def6d9fb061bb31bee696a2859b9fbbfec657bea', 'ver': 2, 'vin_sz': 1, 'vout_sz': 2, 'size': 225, 'weight': 900, 'fee': 1369, 'relayed_by': '0.0.0.0', 'lock_time': 681784, 'tx_index': 8250102780526455, 'double_spend': False, 'time': 1620083348, 'block_index': 681785, 'block_height': 681785, 'inputs': [{'sequence': 4294967293, 'witness': '', 'script': '47304402206224bce979129a96a531ec1e0be2d26e200ce7573d724ccfd4773623b3b312fb022011bb9bef63ba3653fd3a77be82d92cc05165319197fd98f5ee79afb18d47ef84012102935f179f40dd2c8597456bff4efca7d6917bac69b477c19053e151bb1e47dbd1', 'index': 0, 'prev_out': {'spent': True, 'script': '76a914997e440e1e0b35bdea90488919a727dbc38f64fa88ac', 'spending_outpoints': [{'tx_index': 8250102780526455, 'n': 0}], 'tx_index': 1658202184535850, 'value': 4269, 'addr': '1EzbeTMmTN4U99dP8N8PQuzJmmcAxETPNE', 'n': 0, 'type': 0}}], 'out': [{'type': 0, 'spent': False, 'value': 1000, 'spending_outpoints': [], 'n': 0, 'tx_index': 8250102780526455, 'script': '76a914119b098e2e980a229e139a9ed01a469e518e6f2688ac', 'addr': '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX'}, {'type': 0, 'spent': False, 'value': 1900, 'spending_outpoints': [], 'n': 1, 'tx_index': 8250102780526455, 'script': '76a914a63561664bd7d907cc993165bd1fcc22540dee4988ac', 'addr': '1G9q7nTUEDL9kHumqYxXH8uzEGp6i6MYtp'}], 'result': 1000, 'balance': 5034680038}
… and so:
print(transactions[0]['hash']) # 367c89950f2865a2c07feb84def6d9fb061bb31bee696a2859b9fbbfec657bea
Depending on what you need, you can use this as a starting point for further processing.
π Recommended: Bitcoin Generate Public Address and Private Key in Python
Alternative Solution with Library
πͺ Question: How to Get All Transactions for a BTC Address in Python?
To get all transactions for a Bitcoin address in Python, you can also use the BlockCypher API. To access the API, first install the associated Python library using PIP:
pip install blockcypher
Once installed, use the following code to get all transactions for a BTC address:
from blockcypher import get_address_full # Insert your BTC address here: address = 'bc1qud92jmtms8mwmx33zexs9mpwryuwrldqwyvp6805ntl9x8hlghlstvka58' # Get all transactions transactions = get_address_full(address) # Print transactions print(transactions)
This returns a dictionary with all the transactions for the specified address. In the example case, it looks like this:
Thanks for Reading! β₯οΈ
If you want to learn more about becoming a crypto freelancer, check out my free webinar where I share how I created a thriving coding business online—and how you can too!
Freelancing is an exciting way to make money from the comfort of your own home as a coder. Feel free to learn more about the course here.