How to Get Transaction History for an Address on Ethereum
Learn how to get the full transaction history for a smart contract or a user address including external, internal, token, ERC-20, ERC-721 and ERC-1155 token transfers in a single request.
This tutorial uses the alchemy_getAssetTransfers endpoint.
A few reasons for why you’d want to get address transaction history by an address:
- Displaying your a user’s full transaction history
- Querying an address’s transactions filtered by smart contract interactions
- Analyzing a user’s historical profit and loss
Regardless of the different types of transaction history, you want to look up, this process can be extremely burdensome for developers to stitch together without the Alchemy Transfers API
In this tutorial, we’ll be using Alchemy’s Transfers API to fetch all transactions sent from
and sent to
addresses you care about to create a complete picture of a user’s transaction history.
How to query transaction history
When using the Transfers API for querying a user’s full on-chain history, it’s important to have a few key parameters on hand.
fromAddress
: the address we want to see transaction information originating fromtoAddress
: the address we want to see for recipient-based transactionsfromBlock
: the starting time range we want to fetch transactions over (defaults tolatest
)toBlock
: the ending time range we want to fetch transactions over (defaults tolatest
)category
: the type of transfer events we care about, in our case we want to see all transactions so we can simply let the param use its default argument of [“external
”, “internal
”, “token
”]
For transaction information that originates from your target sender address, use the fromAddress
parameter within the Transfers API. For recipient-based transactions, use the toAddress
parameter.
If you want to get transactions that have a specific from
and to
address, you can specify the fromAddress
and toAddress
in your request.
Example: Getting Transactions Originating From
An Address
For a no-code view of the API request check out the composer tool
Alchemy SDK (Recommended)
Alchemy’s SDK allows us to more efficiently interact with Alchemy’s endpoints and make JSON-RPC requests.
Ensure you are inside your project folder and type the following command in the terminal:
1. Create a file.
In your current directory, create a new file called tx-history-from-alchemy-sdk.js
Use your favorite file browser, code editor, or just directly in the terminal using the touch
command like this:
2. Write script!
Copy and paste the following code snippet into your new file: tx-history-from-alchemy-sdk.js
3. Run script!
Now, on your command line, you can execute the script by calling:
Node-Fetch
If you’re using node-fetch
a lightweight, common module that brings the Fetch API to Node.js and allows us to make our HTTP requests, here’s a code snipper for the request you’d make!
1. Create a file.
In your current directory, create a new file called tx-history-from-fetch.js
using your favorite file browser, code editor, or just directly in the terminal using the touch
command like this:
2. Write script!
Copy and paste in the following code snippet into your new file: tx-history-from-fetch.js
3. Run script!
Axios
If you’re using Javascript axios
, a promise-based HTTP client for the browser and Node.js which allows us to make a raw request to the Alchemy API, here’s a code snipper for the request you’d make!
1. Create a file.
In your current directory, create a new file called tx-history-from-axios.js
using your favorite file browser, code editor, or just directly in the terminal using the touch
command.
2. Write script!
Copy and paste the following code snippet into your new file: tx-history-from-axios.js
3. Run script!
Now, on your command line, you can execute the script by calling:
Example: Getting Recipient-based Transactions
*For a no-code view of the API request check out the composer tool
Alchemy SDK (Recommended)
Alchemy’s SDK allows us to more efficiently interact with Alchemy’s endpoints and make JSON-RPC requests.
Ensure you are inside your project folder and type the following command in the terminal:
1. Create a file
In your current directory, create a new file called alchemy-sdk-transfers-to-script.js
Use your favorite file browser, code editor, or just directly in the terminal using the touch
command like this:
2. Write script!
Copy and paste in the following code snippet into your new file: alchemy-sdk-transfers-to-script.js
3. Run script!
Now, on your command line, you can execute the script by calling:
Node-Fetch
If you’re using node-fetch
a lightweight, common module that brings the Fetch API to Node.js and allows us to make our HTTP requests, here’s a code snipper for the request you’d make!
1. Create a file.
In your current directory, create a new file called fetch-transfers-to-script.js
using your favorite file browser, code editor, or just directly in the terminal using the touch
command like this:
2. Write script!
Copy and paste in the following code snippet into your new file: fetch-transfers-to-script.js
3. Run script!
Now, on your command line, you can execute the script by calling:
Axios
If you’re using Javascript axios
, a promise-based HTTP client for the browser and Node.js which allows us to make a raw request to the Alchemy API, here’s a code snipper for the request you’d make!
1. Create a file.
In your current directory, create a new file called axios-transfers-to-script.js
using your favorite file browser, code editor, or just directly in the terminal using the touch
command.
2. Write script!
Copy and paste the following code snippet into your new file: axios-transfers-to-script.js
3. Run script!
Now, on your command line, you can execute the script by calling:
How to process the API response
Now that we have made a query and can see the response, let’s learn how to handle it. If you feel like jumping ahead and grabbing some pre-built code, choose a repo that matches your preferred library.
Alchemy SDK (Recommended)
Parsing with Alchemy SDK
Responses
Node-Fetch
Parsing with Node-Fetch
Responses
Axios
Parsing with Axios
Responses
Raw API Response
Without parsing the response, we have a console log that looks as follows.
Understanding API Response
-
blockNum
: the block number where a transaction event occurred, inhex
-
hash
: the transaction hash of a transaction -
from
: where the transaction originated from -
to
: where ETH or another asset was transferred to -
value
: the amount of ETH transferred -
erc721TokenId
: the ERC721 token ID.null
if not an ERC721 token transfer. -
erc1155Metadata
: a list of objects containing the ERC1155tokenId
andvalue
.null
if not an ERC1155 transfer -
tokenId
: the token ID for ERC721 tokens or other NFT token standards -
asset
:ETH
or the token’s symbol.null
if not defined in the contract and not available from other sources. -
rawContract
value
: raw transfer value denominated in the relevant Ethereum tokenaddress
: Ethereum token contract addressdecimal
: contract decimal
Printing out the asset
and value
Two of the many different response objects you may be interested in parsing are: asset
and value
.
Let’s walk through an example that parses the returned JSON object.
Whether we’re querying via alchemy web3
, axios
, or node-fetch
, we’ll need to save the queried response object into a constant.
Alchemy SDK (Recommended)
Saving response objects with Alchemy SDK
Node-Fetch
Saving response objects with Node-Fetch
Axios
Saving response objects with Axios
With our queried response object saved as a constant, we can now index through the transfers. In particular, the steps we take are:
- Loop through all transfers in the result
- Print each element’s
value
andasset
field
If you followed along, your response should look like the following:
And that’s it! You’ve now learned how to fetch transaction history for address on Ethereum. For more, check out the tutorial below:
Integrating Historical Transaction Data into your dApp
If you enjoyed this tutorial for getting address transaction history on Ethereum, give us a tweet @Alchemy! (Or give the author @crypt0zeke a shoutout!)
Don’t forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!