14 min read
Overviewβ
Making a dApp that requires ERC20 token data can be tedious since there are numerous tokens, and one needs to query each token contract to get the data. This guide will show how we can build a ERC20 token balance app with Quicknode Token API.
Prefer a video walkthrough? Follow along with Radek and learn how to Build an ERC20 Token Balance App with the Quicknode Token API.
What You Will Needβ
- Node.js 22.12 or later.
- Knowledge of HTML, CSS, JavaScript & React.
- Ethers.js (version 5.7) & TailwindCSS libraries.
- A Quicknode Ethereum Mainnet endpoint with the Token and NFT API v2 bundle enabled.
- A code editor (i.e., VScode).
What You Will Doβ
- Learn about the traditional method of fetching ERC20 token data.
- Learn about Quicknode Token API and its different methods.
- Boot a Quicknode Ethereum node with Token API.
- Create a React app to display ERC20 token balances of a particular wallet using Quicknode Token API and Ethers.js library along with Tailwind CSS for styling.
The Traditional wayβ
To get token data like the balance for a specific ERC20 token, one needs to query the smart contract of that particular token. Multiple smart contracts need to be queried when you want to obtain data for multiple tokens. In this process, one needs to query the contract with the help of contract ABI; this creates a lot of redundant code, which also needs to be maintained. Learn how to get token balances using this method in this guide.
Quicknode Token API makes this process very easy and short. In the next section, we will learn more about the Token API and its methods.
What is Quicknode's Token API?β
Quicknode Token API is an API using which a user can get token metadata, balances, and transfer histories of ERC20 tokens for a wallet on the Ethereum mainnet. While using the Token API, the user must make a method call to the Quicknode RPC without the need to query smart contracts. The following are the methods of Quicknode Token API:
- qn_getTokenMetadataByContractAddress returns ERC-20 metadata by contract address.
- qn_getTokenMetadataBySymbol returns ERC-20 metadata by symbol.
- qn_getWalletTokenBalance returns a wallet's ERC-20 token balances.
- qn_getWalletTokenTransactions returns a wallet's ERC-20 transfer history.
Now that we have learned about Quicknode Token API, let us boot a Quicknode Ethereum mainnet endpoint and create a small Token app in the upcoming sections.
Booting Quicknode endpoint with Token APIβ
Create a Quicknode Ethereum Mainnet endpoint and enable the Token and NFT API v2 bundle. If you used an earlier version of this tutorial, enable the v2 bundle on your endpoint before running the updated app.

How to create an ERC20 Token Balance App with Quicknode Token APIβ
Create a React app with Vite. The app uses Ethers.js 5.7 to validate addresses and format balances, and Tailwind CSS to style the interface.
npm create vite@9 token-wallet -- --template react --no-interactive
cd token-wallet
npm install ethers@5.7
npm install -D tailwindcss@3 postcss autoprefixer
npx tailwindcss init -p
Update tailwind.config.js so Tailwind scans the HTML entry point and React components.
/** @type {import('tailwindcss').Config} */
export default {
content: ["./index.html", "./src/**/*.{js,jsx}"],
theme: {
extend: {},
},
plugins: [],
}
Replace the starter styles in src/index.css with the Tailwind directives.
@tailwind base;
@tailwind components;
@tailwind utilities;
The form will take an Ethereum address and display the token balances for that wallet. Replace src/App.jsx with the following code.
import { useState } from 'react';
import { utils } from "ethers";
function App() {
//State variables
const [tokens, setTokens] = useState([])
const [address, setAddress] = useState('')
}
export default App;
The component stores the wallet address and token balances in React state.
Configure the server routeβ
Create .env and add it to .gitignore. Copy the Ethereum endpoint Base URL and Auth token into these server settings. To use a full HTTPS endpoint URL, set it as QUICKNODE_RPC_URL and leave QUICKNODE_RPC_TOKEN blank.
We recommend sending the Auth token in the x-token header for HTTPS requests. This reduces the risk of exposing credentials through URL logging and monitoring systems.
Quicknode supports both header and URL-path authentication. Open How to authenticate on your endpoint overview to see the HTTPS examples. Keep the endpoint credentials on the server.
QUICKNODE_RPC_URL=https://your-endpoint.quiknode.pro/
QUICKNODE_RPC_TOKEN=your-auth-token
Create server.cjs in the React project's root. It calls qn_getWalletTokenBalance with the v2 request format and maps the API's result array and totalBalance field to the assets and amount fields used by the React app.
const { createServer } = require("node:http");
const { QUICKNODE_RPC_URL, QUICKNODE_RPC_TOKEN } = process.env;
if (!QUICKNODE_RPC_URL) throw new Error("Set QUICKNODE_RPC_URL in .env.");
function json(res, status, value) {
res.writeHead(status, { "Content-Type": "application/json" });
res.end(JSON.stringify(value));
}
createServer(async (req, res) => {
const url = new URL(req.url, "http://localhost");
if (req.method !== "GET" || url.pathname !== "/api/tokens") {
return json(res, 404, { error: "Route not found." });
}
const address = url.searchParams.get("wallet") || "";
if (!/^0x[0-9a-fA-F]{40}$/.test(address)) {
return json(res, 400, { error: "Enter a valid Ethereum address." });
}
const params = {
wallet: address,
contracts: [
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"0xdAC17F958D2ee523a2206206994597C13D831ec7",
"0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0",
"0xC18360217D8F7Ab5e7c516566761Ea12Ce7F9D72",
],
};
try {
const upstream = await fetch(QUICKNODE_RPC_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(QUICKNODE_RPC_TOKEN ? { "x-token": QUICKNODE_RPC_TOKEN } : {}),
},
redirect: "error",
signal: AbortSignal.timeout(15000),
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "qn_getWalletTokenBalance", params: [params] }),
});
const data = upstream.ok ? await upstream.json() : null;
const tokens = data?.result === null ? [] : data?.result?.result;
if (!data?.error && Array.isArray(tokens)) {
const assets = tokens.map((token) => ({
name: token.name,
symbol: token.symbol,
decimals: Number(token.decimals),
amount: token.totalBalance,
}));
return json(res, 200, { assets });
}
} catch {
// Keep upstream errors and credentials on the server.
}
return json(res, 502, { error: "Unable to load balances from Token API v2." });
}).listen(3001, "127.0.0.1");
Update vite.config.js so the browser can call /api/tokens through the Vite development server.
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api': 'http://127.0.0.1:3001',
},
},
});
Start the API server from the project root and leave it running while you work on the React app.
node --env-file=.env server.cjs
Add the form handler and balance request inside App in src/App.jsx.
//Function to handle form submission
const handleSubmit = (e) => {
e.preventDefault();
setAddress(address);
fetchTokens()
.then(data => {
setTokens(data.assets)
})
.catch(err => setTokens([]))
}
//Function to fetch tokens
const fetchTokens = async () => {
if (!utils.isAddress(address)){
alert('Please enter a valid Ethereum wallet address')
return;
}
const response = await fetch("/api/tokens?wallet=" + encodeURIComponent(address), {
headers: { Accept: "application/json" },
});
if (!response.ok) throw new Error("Unable to load tokens.");
return response.json()
}
When the form is submitted, fetchTokens validates the address and calls the local API route. The server queries Quicknode for the four configured contracts, and handleSubmit stores the returned balances for the table.
Add the following JSX inside App.
//JSX code
return(
<div className="h-screen w-screen justify-center space-x-3">
<div className="flex justify-center space-x-3 w-screen h-14 mt-10">
<form
onSubmit={handleSubmit}
className="w-4/12 h-15 relative block overflow-hidden rounded-md border border-gray-200 px-3 pt-3 shadow-sm focus-within:border-blue-600 focus-within:ring-1 focus-within:ring-blue-600 dark:border-gray-700 dark:bg-gray-800">
<input
onChange={e => setAddress(e.target.value)}
type="text"
placeholder="Enter your Address here π―"
className="mt-1 w-full border-none p-0 focus:border-transparent focus:outline-none focus:ring-0 sm:text-sm"
/>
<button
type='submit'
className="rounded-lg top-1 right-1 bottom-1 border absolute w-12 justify-center bg-blue-400 text-white p-3 font-bold uppercase"
>
GO
</button>
</form>
</div>
<div className="relative top-4/12 left-1/4 overflow-x-auto justify-center space-x-3 w-6/12 h-140 m-10">
<table className="min-w-full divide-y-2 divide-gray-200 text-sm">
<thead>
<tr>
<th
className="whitespace-nowrap px-4 py-2 text-left font-medium text-gray-1000">
Name
</th>
<th
className="whitespace-nowrap px-4 py-2 text-left font-medium text-gray-900">
Symbol
</th>
<th
className="whitespace-nowrap px-4 py-2 text-left font-medium text-gray-900">
Balance
</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-200">
{tokens.map((token, index) => (
<tr key={index}>
<td className="whitespace-nowrap px-4 py-2 text-gray-900">{token.name}</td>
<td className="whitespace-nowrap px-4 py-2 text-gray-900">{token.symbol}</td>
<td className="whitespace-nowrap px-4 py-2 text-gray-900">{utils.formatUnits(token.amount, token.decimals)}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
The above code is basic HTML in JS (JSX) and Tailwind styling. The few worth noting lines are:
Line 44: Calling the handleSubmit function on form submission.
Line 47: Getting the current value of the input field and setting it to setAddress.
Line 79: Mapping the token data in the tokens array.
Line 81: Printing the token's name, symbol, and balance (formatting the units based on that token's decimal values).
Note: The app will only display balances of tokens which me added in the contracts section of the RPC.
Save the file, and the final file should look like this:
import { useState } from 'react';
import { utils } from "ethers";
function App() {
//State variables
const [tokens, setTokens] = useState([])
const [address, setAddress] = useState('')
//Function to handle form submission
const handleSubmit = (e) => {
e.preventDefault();
setAddress(address);
fetchTokens()
.then(data => {
setTokens(data.assets)
})
.catch(err => setTokens([]))
}
//Function to fetch tokens
const fetchTokens = async () => {
if (!utils.isAddress(address)){
alert('Please enter a valid Ethereum wallet address')
return;
}
const response = await fetch("/api/tokens?wallet=" + encodeURIComponent(address), {
headers: { Accept: "application/json" },
});
if (!response.ok) throw new Error("Unable to load tokens.");
return response.json()
}
//JSX code
return(
<div className="h-screen w-screen justify-center space-x-3">
<div className="flex justify-center space-x-3 w-screen h-14 mt-10">
<form
onSubmit={handleSubmit}
className="w-4/12 h-15 relative block overflow-hidden rounded-md border border-gray-200 px-3 pt-3 shadow-sm focus-within:border-blue-600 focus-within:ring-1 focus-within:ring-blue-600 dark:border-gray-700 dark:bg-gray-800">
<input
onChange={e => setAddress(e.target.value)}
type="text"
placeholder="Enter your Address here π―"
className="mt-1 w-full border-none p-0 focus:border-transparent focus:outline-none focus:ring-0 sm:text-sm"
/>
<button
type='submit'
className="rounded-lg top-1 right-1 bottom-1 border absolute w-12 justify-center bg-blue-400 text-white p-3 font-bold uppercase"
>
GO
</button>
</form>
</div>
<div className="relative top-4/12 left-1/4 overflow-x-auto justify-center space-x-3 w-6/12 h-140 m-10">
<table className="min-w-full divide-y-2 divide-gray-200 text-sm">
<thead>
<tr>
<th
className="whitespace-nowrap px-4 py-2 text-left font-medium text-gray-1000">
Name
</th>
<th
className="whitespace-nowrap px-4 py-2 text-left font-medium text-gray-900">
Symbol
</th>
<th
className="whitespace-nowrap px-4 py-2 text-left font-medium text-gray-900">
Balance
</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-200">
{tokens.map((token, index) => (
<tr key={index}>
<td className="whitespace-nowrap px-4 py-2 text-gray-900">{token.name}</td>
<td className="whitespace-nowrap px-4 py-2 text-gray-900">{token.symbol}</td>
<td className="whitespace-nowrap px-4 py-2 text-gray-900">{utils.formatUnits(token.amount, token.decimals)}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}
export default App;
With the API server running, start the frontend in a second terminal.
npm run dev
Open http://localhost:5173 to see the balance form.

Paste an address in the input field and click on GO; you should see the balances of tokens.

Get the complete ERC-20 balance app from qn-guide-examples.
Conclusionβ
You are fantastic if you made it till here. In this tutorial, we learned about Quicknode Token API and created a ERC20 token balance app using the Token API.
Make more out of the Token API with its various methods. Join our Discord if you have any questions, or reach out to us via Twitter.
