Skip to main content
When building a web3 app, it is essentially for the app to show some information about the connected wallet like ERC20 token balances, ERC721 token balances, transaction history etc… Moralis has a wide range of out-of-the-box APIs for the most popular features like wallet native balance, fetch all NFTs and collections, wallet age and activity, and many more… In this guide, you will learn how to build a simple ERC20 token balances view for the connected wallet using Moralis API and NextJS.
If you wish to try the portfolio viewer before building it, you can do so here
portfolio view

Requirements

Before you begin, you’ll need:
  1. Moralis API Key: Get it from Moralis Dashboard
  2. Reown Project ID: Get it from Reown Cloud

Step 1: Initializing the project

Create Nextjs app

Install ShadCN components

Install Tanstack React Query (for better async state management)

Step 2: Environment Setup

Add your Moralis API key and Reown Project ID to .env.local:

Step 3: Connect Wallet functionality

Create the ContextProvider component

Create a folder named context in the src directory, inside the folder create a file named index.ts
src/context/index.ts

Wrap the app with ContextProvider component in layout.tsx

src/app/layout.tsx

Create a custom ConnectButton

Create a file in components/wallet folder named ConnectButton.tsx
src/components/wallet/ConnectButton.tsx
We are using the useAppKit and useAppKitAccount hook for creating a custom connect button instead of the standard one provided by Reown AppKit.

Create a Header component with custom ConnectButton

header Create a file named Header.tsx in src/components/layout folder and import the newly created ConnectButton component into it.
src/components/layout/Header.tsx
Import the Header component in layout.tsx
src/app/layout.tsx
You should now be able to see a custom Connect Wallet button!

Step 4: Fetching connected wallet token balances

Creating a /api/wallet/balances route

Create a file named route.ts in the folder src/app/api/wallet/balances/ We are using the “Get Native & ERC20 Token Balances by Wallet” Moralis API endpoint to get the token balances. You can find the endpoint docs here. It takes in an address and a chain id and returns a list of ERC20 tokens (including Logo, Name, Symbol) and native balance of the address.
src/app/api/wallet/balances/route.ts

Create a react hook called useTokenBalances

This react hook calls the /api/wallet/balances/ endpoint that you created and gets the token balances data, formats it into a proper list and makes it available to consume in the frontend.
src/hooks/useTokenBalances.ts
We will later use this hook in the UI components

Step 5: Creating the UI

Create a TokenRow component

token-row TokenRow component will display token details like name, symbol, logo, usd price, amount of tokens in connected wallet and the usd value. Create a file named TokenRow.tsx in the folder src/components/portfolio/tokens/
src/components/portfolio/tokens/TokenRow.tsx

Create a TokenList component

token-list Create a TokenList.tsx file in src/components/portfolio folder and import the newly created TokenRow.tsx component in it.
src/components/portfolio/TokenList.tsx

Create a PortfolioDashboard component

dashboard The PortfolioDashboard component will display the TokenList and can be further modified to have more components presenting information that you would like your user to see.
src/components/portfolio/PortfolioDashboard.tsx
Check out the other Moralis API endpoints and create visual components for the same!

Conclusion

In this guide you learned how to use Moralis API to get wallet token balances and presenting them in a UI. It is highly recommended you check out the other API endpoints and add more features to this project you created! You can find a complete portfolio dashboard project here for inspiration.