Build your first web3 app
Build a small web app in vanilla JavaScript that uses the Tatum SDK to fetch and display the ETH balance of any Ethereum address.
This guide shows you how to build a small web app in vanilla JavaScript that uses the Tatum JavaScript SDK to retrieve and display the ETH balance of an Ethereum address. You need an API key from the Tatum Dashboard.
Steps
- Create a new Node.js project and install the Tatum SDK package.
- Build an HTML page with an input field for the Ethereum address and a button that starts the balance lookup.
- Write JavaScript that calls the Tatum SDK's
address.getBalance()method for the entered address. - Start the app with the Parcel development server.
Create a new project
The first step is to create a new project by running npm init in the terminal. This will prompt you to enter some basic information about the project, such as the project name and version.
npm initInstall Tatum SDK
To install SDK, simply run the following command in your terminal or command prompt.
This will install the Tatum SDK package and its dependencies:
npm install @tatumio/tatumyarn add @tatumio/tatumpnpm install @tatumio/tatumbun install @tatumio/tatumCreate an index.html file
After installing the Tatum SDK, create an index.html file in your project directory. It contains the input field, the button and a div element for displaying the balance. The script tag loads app.js, which you create in the next step.
<html>
<head>
<meta charset="utf-8"/>
<title>My First Tatum DApp</title>
<script type="module" src="./app.js"></script>
</head>
<body>
<h1>Get Eth balance with Tatum!</h1>
<input type="text" id="address" placeholder="Enter wallet address" />
<button id="get-balance">Get Balance</button>
<div id="balance"></div>
</body>
</html>Create an app.js file
Next, create an app.js file in your project directory. It adds a click listener to the button and uses the Tatum SDK to retrieve the balance of the entered Ethereum address. Replace {YOUR_API_KEY} with your API key.
import { TatumSDK, Network } from '@tatumio/tatum'
const button = document.getElementById('get-balance');
const addressInput = document.getElementById('address');
const balanceDiv = document.getElementById('balance');
button.addEventListener('click', async () => {
const tatum = await TatumSDK.init({ network: Network.ETHEREUM, apiKey: '{YOUR_API_KEY}' });
const balance = await tatum.address.getBalance({
addresses: [addressInput.value],
});
const balanceData = balance.data.filter(asset => asset.asset === 'ETH')[0];
balanceDiv.textContent = `${balanceData.balance} ${balanceData.asset}`;
await tatum.destroy();
});
When you click the button, the code initializes the Tatum SDK for the Ethereum network, calls tatum.address.getBalance with an array that contains the entered address, and displays the ETH balance in the balance div. tatum.destroy() then stops the SDK's background jobs.
This example puts your API key in code that runs in the browser, where anyone who opens the page can read it. Use it for local testing only; in production, call Tatum from your backend.
Each TatumSDK instance is tied to one blockchain network. All supported networks are listed insrc/dto/Network.tson GitHub.
Update package.json
To start the app from the index.html file, set the source property in your package.json file. If npm init added a main field, remove it: Parcel treats main as a library build target.
{
"name": "my-first-tatum-dapp",
"version": "1.0.0",
"source": "./index.html",
"license": "MIT",
"dependencies": {
"@tatumio/tatum": "^4.0.0"
}
}Start server
The last step is to start the server using Parcel. You can run npx parcel in the terminal to start the server, and your app will be available at http://localhost:1234.
npx parcelYou can enter a wallet address and click the "Get Balance" button to retrieve the balance for that Ethereum address. The balance will be displayed below the button.
Parcel bundles and serves your app automatically.
Next steps
You have built a web app that reads the ETH balance of an Ethereum address with the Tatum SDK. To continue:
- Manage your API keys and create notifications in the Tatum Dashboard.
- Configure the SDK with Configuration options.
- See what the RPC, Notification, NFT and Address modules do in SDK submodules.
- Read NFT balances, collections and owners with the NFTs guides.
- Generate wallets and sign transactions with Wallet Operations.
- Call blockchain nodes directly with RPC Nodes.
- Get webhooks for address activity with Notifications.
- Browse more example apps in the tatumio/example-apps repository on GitHub.
Updated 6 days ago