Posted By : Rahul
One of the most popular blockchain systems, Ethereum, enables token trading, smart contract development, and the development of decentralised apps (DApps). Retrieving transaction history for a specific address is a crucial component of any blockchain. Web3.py is a well-liked Python toolkit for Ethereum developers to communicate with the Ethereum blockchain. We'll demonstrate how to use Web3.py to retrieve Ethereum transaction history in this blog post.
Web3.py is a Python library that enables interaction with the Ethereum blockchain. It enables developers to send transactions, communicate with smart contracts, query the blockchain, and much more. Web3.py is frequently used in Ethereum-based DApps and blockchain development to facilitate blockchain interactions using Python.
One essential function of any blockchain explorer, wallet app, or DApp is the ability to retrieve transaction history. Obtaining transaction information allows you to:
Examine an address's activity for reporting or auditing needs.
Also, check | Develop a Multi-Token Crypto Wallet for Ethereum with Web3.js
Installing Web3.py and configuring the connection to an Ethereum node
pip install web3
Web3.py must be connected to an Ethereum node in order to communicate with the Ethereum blockchain. Infura is a popular solution for this, offering an Ethereum access API. For this, you will want an Infura project ID.
Also, Discover | Developing Cross-Platform Crypto Wallet with Web3.js & React
from web3 import Web3
infura_url = 'https://mainnet.infura.io/v3/your infura api key'
web3 = Web3(Web3.HTTPProvider(infura_url))
# Check if connected
if web3.is_connected():
print('Connected to Ethereum Network')
else:
print('Failed to connect to Ethereum Network')
def get_transaction_history(address, start_block=0, end_block='latest'):
address = web3.to_checksum_address(address) # Ensure address is in checksum format
transactions = []
# Define block range
end_block = web3.eth.block_number if end_block == 'latest' else int(end_block)
# Loop through blocks and fetch transactions
for block_number in range(start_block, end_block + 1):
block = web3.eth.get_block(block_number, full_transactions=True)
print(f'Processing block {block_number}')
if block and block.transactions:
for txn in block.transactions:
# Check if the transaction involves the target address
if txn['to'] == address or txn['from'] == address:
transactions.append(txn)
return transactions
# Replace with the address you're querying
transactions = get_transaction_history('0xYourEthereumAddressHere',
start_block=10000000,
end_block=str(10001000))
# Display transaction details
for txn in transactions:
print(f'Transaction Hash: {txn['hash'].hex()}')
print(f'From: {txn['from']}')
print(f'To: {txn['to']}')
print(f'Amount: {web3.from_wei(txn['value'], 'ether')} ETH')
print(f'Block Number: {txn['blockNumber']}')
print(f'Gas Used: {txn['gas']}')
print('---')
Output:
 Environment updated. Reloading shell...
Connected to Ethereum Network
Processing block 21873279
Processing block 21873280
Processing block 21873281
Processing block 21873282
Processing block 21873283
Processing block 21873284
Transaction Hash: 650b635c2687df8103a3df0ede497e0ff12fb3041494adf031debc7b61e8223c
From: 0x8D9903956d0C9bE104E4Ed32852080F86cfCadcF
To: 0x66a9893cC07D91D95644AEDD05D03f95e1dBA8Af
Amount: 0 ETH
Block Number: 21873279
Gas Used: 351949
---
Fetching Ethereum transaction history using Web3.py empowers developers to build robust blockchain applications with transparency and accountability. Whether you're developing a wallet, an audit tool, or an analytics dashboard, accessing historical transaction data is key to understanding user behavior, ensuring compliance, and enabling seamless experiences. With just a few lines of Python and a connection to an Ethereum node like Infura, you can easily retrieve and analyze on-chain activity, making Web3.py an essential tool in every Ethereum developer's toolkit.
If you're looking to build or scale blockchain solutions, feel free to connect with our experienced blockchain developers for tailored development support.
June 26, 2025 at 12:53 pm
Your comment is awaiting moderation.