Last active
February 12, 2022 16:55
-
-
Save ankitzm/55eb1cc3b4a2478d51799868fb668b3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>eth balance</title> | |
</head> | |
<body> | |
Enter the ETH address - | |
<input id="address" size=12 required> | |
<button type="submit" onclick="getBalance(address.value)" >get balance</button> | |
<div id="balance"> | |
</div> | |
</body> | |
<script> | |
const balance = document.getElementById("balance"); | |
// API code | |
var requestOptions = { | |
method: 'GET', | |
redirect: 'follow' | |
}; | |
const API_KEY = "ADD_API_KEY_HERE" | |
function getBalance(address) { | |
fetch(`https://api.etherscan.io/api?module=account&action=balance&address=${address}&tag=latest&apikey=${API_KEY}`, requestOptions) | |
.then(response => response.json()) | |
.then(result => displayBalance(result.result)) | |
.catch(error => console.log('error', error)); | |
} | |
function displayBalance(bal) { | |
balance.innerText = `Balance = ${bal/1000000000000000000} ETH` | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment