How to retrieve all transactions of an address by Covalent API and Java

CrazyMoney Alchemist
3 min readJul 3, 2021

--

A tutorial to create a Java program to retrieve all transactions of an Ethereum Mainnet address by using Covalent API.

Step1: Download IDE:
Go to this site: https://www.jetbrains.com/idea/
Download the version suitable for your computer

Step2: Create new Java project:

Create new project
Choose Command Line App template
Enter Project name, location and base package

Step3: Register to get free API key of Covalent:
Visit link: https://www.covalenthq.com/platform/#/auth/register/

Create account to get your free apiKey of Covalent Api
This is my apiKey. I will use it in this example

Step4: Write this code to your Main.java file:

package com.alchemist.crazymoney;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

public static void main(String[] args) {
try {
final String apiKey = "ckey_edc1619f340849c6939fba54856"; // Your Covalent Api Key
final String address = "0x42f46D529c8EdB851EAD0711BbB00be14BB4E863"; // It is an example address
final String chainId = "1"; // Chain Id of ETH
final String urlString = "https://api.covalenthq.com/v1/" + chainId + "/address/" + address + "/transactions_v2/?key=" + apiKey;
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json");
int status = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
System.out.println(inputLine);
}
in.close();
con.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}

You can replace apiKey. address by your apiKey. address
In this example: I use chainId = 1. It is chainId of Ethereum

Main.java

Step 5: Run code and get the result:

Press Run ‘Main’

The result:

The result in console log

This is result in JSon. We can see all transactions in this address.

{
"data": {
"address": "0x42f46d529c8edb851ead0711bbb00be14bb4e863",
"updated_at": "2021-07-03T09:23:06.632328232Z",
"next_update_at": "2021-07-03T09:28:06.632329012Z",
"quote_currency": "USD",
"chain_id": 1,
"items": [
{
"block_signed_at": "2021-04-09T02:43:39Z",
"block_height": 12203023,
"tx_hash": "0x692c22e198c228e9b63fee6dcdc8c6a8f1ba777a4889b17f25a18573699ff8b6",
"tx_offset": 10,
"successful": true,
"from_address": "0x42f46d529c8edb851ead0711bbb00be14bb4e863",
"from_address_label": null,
"to_address": "0xdff88f223415d27612174ff6db714caaffc28344",
"to_address_label": null,
"value": "703435998905653765",
"value_quote": 1455.9856038667367,
"gas_offered": 21000,
"gas_spent": 21000,
"gas_price": 123000000000,
"gas_quote": 5.346343975341797,
"gas_quote_rate": 2069.819580078125,
"log_events": []
},
{
"block_signed_at": "2021-04-08T23:46:22Z",
"block_height": 12202211,
"tx_hash": "0x4f640a543cd067a02ec1df5ecd92c40b762b1200684a089bf5124af8233afb0e",
"tx_offset": 226,
"successful": true,
"from_address": "0xcb846c8f7dbc8ed6552dcb41e61c7b3aa5b707cf",
"from_address_label": null,
"to_address": "0x42f46d529c8edb851ead0711bbb00be14bb4e863",
"to_address_label": null,
"value": "700000000000000000",
"value_quote": 1457.4126464843748,
"gas_offered": 21000,
"gas_spent": 21000,
"gas_price": 98000000000,
"gas_quote": 4.284793180664062,
"gas_quote_rate": 2082.01806640625,
"log_events": []
},
...
],
"pagination": {
"has_more": false,
"page_number": 0,
"page_size": 100,
"total_count": null
}
},
"error": false,
"error_message": null,
"error_code": null
}

Step7: More info

You can choose another network by changing chainID:

You can read more about chainId in this link: https://www.covalenthq.com/docs/networks

--

--

No responses yet