Overview
While live contract reads tell you what’s happening right now, analytics provide the context of your application’s growth and health. Integrating Dune Analytics allows you to provide users with historical insights, network-wide benchmarks, and aggregate metrics.Initia App
Handles wallet connections, onchain transactions, and the primary UI.
Secure Backend
A lightweight proxy that securely manages your private Dune API key.
Dune Analytics
The SQL engine that processes historical data and serves API results.
Prerequisites
Ensure you have the following before starting:- Dune API Key: Obtained from your Dune settings (select API keys from the sidebar).
- Saved Query IDs: The numeric IDs for the queries you want to display. Open Dune Queries to create or inspect them.
- Node.js Environment: To run the backend proxy and frontend application.
Step 1: Backend Implementation
Your backend acts as a secure proxy, ensuring yourDUNE_API_KEY is never
exposed to the frontend.
Pattern: The backend proxy pattern is language-agnostic. The examples here
use Node.js and Express, but the same security model applies in Python, Go,
Rust, or any server-side stack.
1. Setup and Configuration
Create anapi/ directory, a src/ folder inside it, and install the required
dependencies:
The backend example uses express for routing, cors for origin control, and
dotenv for environment variables.
api/package.json with these scripts:
api/package.json
api/.env and store your secrets in it:
api/.env
2. Proxy Server
This Express server validates requests and proxies them to Dune. It keepsDUNE_API_KEY server-side and only forwards allowed query IDs.
api/src/server.js
Step 2: Frontend Implementation
The frontend connects to your backend proxy, not to Dune directly.1. Configuration
Switch to your frontend directory before setting environment variables:.env):
frontend/.env
2. Fetch Helper
Use this helper to abstract the backend API call:frontend/src/lib/dune.ts
Limit: The backend proxy supports
?limit= on the results endpoint, and
the frontend helper exposes it as the optional limit argument.Step 3: Mapping Multiple Queries
Use stable Application Keys to map your UI components to specific Dune Query IDs. This allows you to update queries on the backend without changing frontend code.frontend/src/lib/dune.ts
3. Usage Example
After definingDUNE_QUERY_MAP, a React component can use the helper like this:
frontend/src/components/Analytics.tsx
Step 4: Local Development
Start Frontend
Run the frontend app from the
frontend/ directory with npm run dev and
verify VITE_DUNE_API_BASE_URL points to the backend.Verify Backend
Confirm your proxy is live by visiting
http://localhost:4000/api/health in
your browser or running: bash curl http://localhost:4000/api/health Troubleshooting and Security Tips
- API Key Security: Never expose
DUNE_API_KEYin your frontend. It must remain server-side. - Source of Truth: Dune is an analytics layer, not your contract’s source of truth. Keep critical validation and state transitions onchain.
- Access Control: Always use the
DUNE_ALLOWED_QUERY_IDSallowlist on your backend to prevent unauthorized proxying. - Data Freshness: Dune’s results endpoint returns the latest saved execution, not necessarily a fresh one.
- Maintainability: Use Application Keys (like
overview) in your UI components instead of hardcoding numeric IDs.
Advanced: Saving Preferences Onchain (Optional)
For personalized experiences, you can store a user’s selected analytics view on the Initia rollup.Performance: Store only the preference (the
view key) onchain. The
actual analytics data should always be fetched from Dune via your backend.Resources
Common Dune Example Tables
These are example tables frequently used for Initia analytics:| Table | Recommended Use |
|---|---|
initia.transactions | Network activity and gas trends. |
initia.bridge_transfers | Asset flow and bridge route analytics. |
initia.tx_messages | Detailed message-type activity. |
Choosing the Right Tool
| Feature | Initia Indexer | Dune Analytics |
|---|---|---|
| Data Scope | Real-time app state. | Historical trends & aggregates. |
| Query Style | Specific accounts/events. | Complex SQL across millions of rows. |
| Primary Use | Core app logic. | Dashboards & public reports. |
Next Steps
Backend Caching
Reduce API consumption by caching results on your server.
Rich Dashboards
Combine results from multiple Dune views into a single UI.