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 from
  • toAddress: the address we want to see for recipient-based transactions
  • fromBlock: the starting time range we want to fetch transactions over (defaults to latest)
  • toBlock : the ending time range we want to fetch transactions over (defaults to latest)
  • 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’s SDK allows us to more efficiently interact with Alchemy’s endpoints and make JSON-RPC requests.

transfers_api_javascript_scripts/tx-history-from-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts

github.comgithub.com

transfers_api_javascript_scripts/tx-history-from-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts

Ensure you are inside your project folder and type the following command in the terminal:

Shell
$npm install alchemy-sdk
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:

shell
$touch tx-history-from-alchemy-sdk.js
2. Write script!

Copy and paste the following code snippet into your new file: tx-history-from-alchemy-sdk.js

tx-history-from-alchemy-sdk.js
1// Setup: npm install alchemy-sdk
2import { Alchemy, Network } from "alchemy-sdk";
3
4const config = {
5 apiKey: "<-- ALCHEMY APP API KEY -->",
6 network: Network.ETH_MAINNET,
7};
8const alchemy = new Alchemy(config);
9
10const data = await alchemy.core.getAssetTransfers({
11 fromBlock: "0x0",
12 fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",
13 category: ["external", "internal", "erc20", "erc721", "erc1155"],
14});
15
16console.log(data);
3. Run script!

Now, on your command line, you can execute the script by calling:

shell
$node tx-history-from-alchemy-sdk.js

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!

transfers_api_javascript_scripts/tx-history-from-axios.js at main · alchemyplatform/transfers_api_javascript_scripts

github.comgithub.com

transfers_api_javascript_scripts/tx-history-from-axios.js at main · alchemyplatform/transfers_api_javascript_scripts

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:

shell
$touch tx-history-from-fetch.js
2. Write script!

Copy and paste in the following code snippet into your new file: tx-history-from-fetch.js

tx-history-from-fetch.js
1import fetch from 'node-fetch';
2
3 let data = JSON.stringify({
4 "jsonrpc": "2.0",
5 "id": 0,
6 "method": "alchemy_getAssetTransfers",
7 "params": [
8 {
9 "fromBlock": "0x0",
10 "fromAddress": "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",
11 }
12 ]
13});
14
15 var requestOptions = {
16 method: 'POST',
17 headers: { 'Content-Type': 'application/json' },
18 body: data,
19 redirect: 'follow'
20 };
21
22 const apiKey = "demo"
23 const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
24 const fetchURL = `${baseURL}`;
25
26 fetch(fetchURL, requestOptions)
27 .then(response => response.json())
28 .then(response => JSON.stringify(response, null, 2))
29 .then(result => console.log(result))
30 .catch(error => console.log('error', error));
3. Run script!
shell
$node tx-history-from-fetch.js

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!

transfers_api_javascript_scripts/tx-history-from-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts

github.comgithub.com

transfers_api_javascript_scripts/tx-history-from-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts

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.

shell
$touch tx-history-from-axios.js
2. Write script!

Copy and paste the following code snippet into your new file: tx-history-from-axios.js

tx-history-from-axios.js
1import axios from 'axios';
2
3 let data = JSON.stringify({
4 "jsonrpc": "2.0",
5 "id": 0,
6 "method": "alchemy_getAssetTransfers",
7 "params": [
8 {
9 "fromBlock": "0x0",
10 "fromAddress": "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",
11 }
12 ]
13});
14
15 var requestOptions = {
16 method: 'post',
17 headers: { 'Content-Type': 'application/json' },
18 data: data,
19 };
20
21 const apiKey = "demo"
22 const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
23 const axiosURL = `${baseURL}`;
24
25 axios(axiosURL, requestOptions)
26 .then(response => console.log(JSON.stringify(response.data, null, 2)))
27 .catch(error => console.log(error));
3. Run script!

Now, on your command line, you can execute the script by calling:

shell
$node tx-history-from-axios.js

Example: Getting Recipient-based Transactions

*For a no-code view of the API request check out the composer tool

Alchemy’s SDK allows us to more efficiently interact with Alchemy’s endpoints and make JSON-RPC requests.

transfers_api_javascript_scripts/tx-history-to-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts

github.comgithub.com

transfers_api_javascript_scripts/tx-history-to-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts

Ensure you are inside your project folder and type the following command in the terminal:

Shell
$npm install alchemy-sdk
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:

Shell
$touch alchemy-sdk-transfers-to-script.js
2. Write script!

Copy and paste in the following code snippet into your new file: alchemy-sdk-transfers-to-script.js

alchemy-sdk-transfers-to-script.js
1// Setup: npm install alchemy-sdk
2import { Alchemy, Network } from "alchemy-sdk";
3
4const config = {
5 apiKey: "<-- ALCHEMY APP API KEY -->",
6 network: Network.ETH_MAINNET,
7};
8const alchemy = new Alchemy(config);
9
10const data = await alchemy.core.getAssetTransfers({
11 fromBlock: "0x0",
12 toAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",
13 category: ["external", "internal", "erc20", "erc721", "erc1155"],
14});
15
16console.log(data);
3. Run script!

Now, on your command line, you can execute the script by calling:

shell
$node alchemy-sdk-transfers-to-script.js

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!

transfers_api_javascript_scripts/tx-history-to-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts

github.comgithub.com

transfers_api_javascript_scripts/tx-history-to-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts

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:

shell
$touch fetch-transfers-to-script.js
2. Write script!

Copy and paste in the following code snippet into your new file: fetch-transfers-to-script.js

fetch-transfers-to-script.js
1import fetch from 'node-fetch';
2
3 let data = JSON.stringify({
4 "jsonrpc": "2.0",
5 "id": 0,
6 "method": "alchemy_getAssetTransfers",
7 "params": [
8 {
9 "fromBlock": "0x0",
10 "toAddress": "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",
11 }
12 ]
13});
14
15 var requestOptions = {
16 method: 'POST',
17 headers: { 'Content-Type': 'application/json' },
18 body: data,
19 redirect: 'follow'
20 };
21
22 const apiKey = "demo"
23 const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
24 const fetchURL = `${baseURL}`;
25
26 fetch(fetchURL, requestOptions)
27 .then(response => response.json())
28 .then(response => JSON.stringify(response, null, 2))
29 .then(result => console.log(result))
30 .catch(error => console.log('error', error));
3. Run script!

Now, on your command line, you can execute the script by calling:

shell
$node fetch-transfers-from-script.js

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!

transfers_api_javascript_scripts/tx-history-to-axios.js at main · alchemyplatform/transfers_api_javascript_scripts

github.comgithub.com

transfers_api_javascript_scripts/tx-history-to-axios.js at main · alchemyplatform/transfers_api_javascript_scripts

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.

shell
$touch axios-transfers-to-script.js
2. Write script!

Copy and paste the following code snippet into your new file: axios-transfers-to-script.js

axios-transfers-to-script.js
1import axios from 'axios';
2
3 let data = JSON.stringify({
4 "jsonrpc": "2.0",
5 "id": 0,
6 "method": "alchemy_getAssetTransfers",
7 "params": [
8 {
9 "fromBlock": "0x0",
10 "toAddress": "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",
11 }
12 ]
13});
14
15 var requestOptions = {
16 method: 'post',
17 headers: { 'Content-Type': 'application/json' },
18 data: data,
19 };
20
21 const apiKey = "demo"
22 const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
23 const axiosURL = `${baseURL}`;
24
25 axios(axiosURL, requestOptions)
26 .then(response => console.log(JSON.stringify(response.data, null, 2)))
27 .catch(error => console.log(error));
3. Run script!

Now, on your command line, you can execute the script by calling:

shell
$node axios-transfers-to-script.js

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.

Parsing with Alchemy SDK Responses

transfers_api_javascript_scripts/tx-history-parsed-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts

github.comgithub.com

transfers_api_javascript_scripts/tx-history-parsed-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts

Node-Fetch

Parsing with Node-Fetch Responses

transfers_api_javascript_scripts/tx-history-parsed-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts

github.comgithub.com

transfers_api_javascript_scripts/tx-history-parsed-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts

Axios

Parsing with Axios Responses

transfers_api_javascript_scripts/tx-history-parsed-axios.js at main · alchemyplatform/transfers_api_javascript_scripts

github.comgithub.com

transfers_api_javascript_scripts/tx-history-parsed-axios.js at main · alchemyplatform/transfers_api_javascript_scripts

Raw API Response

Without parsing the response, we have a console log that looks as follows.

json
1{
2"transfers": [
3 {
4 "blockNum": "0xb7389b",
5 "hash": "0xfde2a5157eda40b90514751f74e3c7314f452a41890b19a342ee147f5336dfd6",
6 "from": "0x5c43b1ed97e52d009611d89b74fa829fe4ac56b1",
7 "to": "0xe9b29ae1b4da8ba5b1de76bfe775fbc5e25bc69a",
8 "value": 0.245,
9 "erc721TokenId": null,
10 "erc1155Metadata": null,
11 "tokenId": null,
12 "asset": "ETH",
13 "category": "external",
14 "rawContract": {}
15 },
16 {
17 "blockNum": "0xcf5dea",
18 "hash": "0x701f837467ae3112d787ddedf8051c4996ea82914f7a7735cb3db2d805799286",
19 "from": "0x5c43b1ed97e52d009611d89b74fa829fe4ac56b1",
20 "to": "0x92560c178ce069cc014138ed3c2f5221ba71f58a",
21 "value": 152.89962568845024,
22 "erc721TokenId": null,
23 "erc1155Metadata": null,
24 "tokenId": null,
25 "asset": "ENS",
26 "category": "token",
27 "rawContract": {}
28 },
29 {
30 "blockNum": "0xd14898",
31 "hash": "0x2f5d93a9db65548eb43794aa43698acd653e6b2df35c6028b8599a234f2c6dc0",
32 "from": "0x5c43b1ed97e52d009611d89b74fa829fe4ac56b1",
33 "to": "0x83abecf7204d5afc1bea5df734f085f2535a9976",
34 "value": 27579.060635486854,
35 "erc721TokenId": null,
36 "erc1155Metadata": null,
37 "tokenId": null,
38 "asset": "PEOPLE",
39 "category": "token",
40 "rawContract": {}
41 }
42]
43}

Understanding API Response

  • blockNum: the block number where a transaction event occurred, in hex

  • 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 ERC1155 tokenId and value. 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 token
    • address: Ethereum token contract address
    • decimal: 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.

Saving response objects with Alchemy SDK

Alchemy SDK (Recommended)
1// Alchemy SDK
2
3 const res = await alchemy.core.getAssetTransfers({
4 fromBlock: "0x0",
5 toAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",
6 category: ["external", "internal", "erc20", "erc721", "erc1155"],
7 })

Node-Fetch

Saving response objects with Node-Fetch

Node-fetch
1// Node-Fetch
2
3 fetch(fetchURL, requestOptions)
4 .then((res) => {
5 return res.json()
6 })
7 .then((jsonResponse) => {
8 //Print token name / asset value
9 for (const events of jsonResponse.result.transfers) {
10 console.log("Token Transfer: ", events.value, " ", events.asset);
11 }
12 })
13 .catch((err) => {
14 // handle error
15 console.error(err);
16 });

Axios

Saving response objects with Axios

Axios
1// Axios
2
3 const res = await axios(axiosURL, requestOptions);

With our queried response object saved as a constant, we can now index through the transfers. In particular, the steps we take are:

  1. Loop through all transfers in the result
  2. Print each element’s value and asset field
javascript
1// Print token asset name and its associated value
2 for (const events of res.data.result.transfers) {
3 console.log("Token Transfer: ", events.value, " ", events.asset);
4 }

If you followed along, your response should look like the following:

response
$Token Transfer: 0.5 ETH
>Token Transfer: 0.27 ETH
>Token Transfer: 9.90384 ETH
>Token Transfer: 0.07024968 ETH
>Token Transfer: 0.000447494250654841 ETH
>Token Transfer: null null
>Token Transfer: 0.075 ETH
>Token Transfer: 0.003 ETH
>Token Transfer: null BURN
>Token Transfer: 54 DAI
>Token Transfer: 12.5 GTC
>Token Transfer: 2 GTC
>Token Transfer: 0.42 ETH
>........
>Token Transfer: 0.588 WETH
>Token Transfer: null null
>Token Transfer: null null
>Token Transfer: 2.3313024 ETH
>Token Transfer: 0.0633910153108353 ETH
>Token Transfer: 0.0335 ETH
>Token Transfer: 2 GTC

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

docs.alchemy.comdocs.alchemy.com

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!