Using Data APIs (Ormi)
The Somnia mission is to enable the building of mass-consumer real-time applications. As a Developer, you need to understand how to interact with onchain data to build UIs. This guide will teach you how to build a Token Balance dApp that fetches and displays ERC20 token balances from the Somnia Network using Next.js and the Ormi Data APIs.
Prerequisites
To complete this guide, you will need:
Basic understanding of React and TypeScript
An Ormi API key. Get one at https://subgraph.somnia.network/dashboard/api.
What is Ormi Data API?
Ormi provides a unified crypto data infrastructure for live and historical blockchain data. The Data APIs allow developers to query blockchain data without running their own nodes, making it easy to build data-rich applications on the Somnia Network.
API Base URL
The Ormi Data API for Somnia Network uses the following base URL:
API Endpoints
The API follows a RESTful structure. For fetching ERC20 token balances, the endpoint structure is:
Where:
somnia- The network identifierv1- API version{walletAddress}- The wallet address you want to querybalance/erc20- Specifies that you want ERC-20 token balances
Authentication
The Ormi API requires authentication using a Bearer token. Every request must include an Authorization header:
Important: Never expose your API key in client-side code. Always make API calls from a server-side route to keep your key secure.
Example API Request
Here's an example of how to make a direct API call using curl:
Set up the Project
Create a new Next.js application with TypeScript and Tailwind CSS:
Create the Type Definitions
First, we need to define TypeScript interfaces for the API response. Update app/page.tsx:
Build the User Interface
Now, let's create the main component with an input field and button. Update your app/page.tsx:
Create a .env file
.env fileThe .env is for keeping secrets such as the Ormi API KEY. Add the API KEY:
Create the API Route
To avoid CORS issues and keep your API key secure, we'll create an API route. Create a directory and a new file app/api/balance/route.ts:
Important: Replace YOUR_API_KEY_HERE with your actual Ormi API key.
Implement the Fetch Function
Add the fetch function to handle form submission. Update your app/page.tsx:
Don't forget to add the onSubmit handler to your form:
Display the Results
Add error handling and a table to display the token balances. Add this code after your form in app/page.tsx:
Test Your dApp
Start the development server:
Open http://localhost:3000 in your browser. Enter a wallet address that has tokens on Somnia Network.
Example test address: 0xC4890Bc98273424a18626772F266C35bf57FA56A
Look at the browser for the response and the displayed token balances. You can click on any contract address to view it in the Shannon Explorer.
Complete Code
Congratulations
You have built your first API enabled dApp on the Somnia Network!
Now that you have a working Token Balance dApp, you can extend it by using other Ormi API endpoints.
Last updated