Explorer API Health and Monitoring
A comprehensive guide for integrating Somnia Network explorer APIs with production-ready logging and health monitoring systems.
This guide demonstrates how to build robust applications on Somnia Network using Blockscout's explorer APIs, implement enterprise-grade logging, and set up comprehensive health monitoring for your dApps.
What You'll Learn
Somnia Network explorer API integration
Production logging with Winston
Health monitoring systems
Error handling and debugging
Performance optimization
Prerequisites
Node.js (v16+)
TypeScript/JavaScript knowledge
Blockchain/EVM concepts
Funded Somnia wallet
REST API experience
Explorer Endpoints
Somnia Network uses Blockscout as its blockchain explorer infrastructure:
Testnet
https://shannon-explorer.somnia.network
/api
Mainnet
https://explorer.somnia.network
/api
Available APIs
Blockscout provides multiple API interfaces:
REST API - Primary interface for UI operations
RPC API - Etherscan-compatible endpoints
ETH RPC API - Standard JSON-RPC methods
GraphQL - Advanced querying capabilities
Quick Start
Initialize your Somnia integration project:
mkdir somnia-explorer-integration
cd somnia-explorer-integration
npm init -yDependencies
Install required packages:
# Core dependencies
npm install axios winston express helmet cors dotenv ethers
# Development dependencies
npm install --save-dev @types/node typescript ts-node nodemon @types/express @types/corsCore Dependencies:
axios- Promise-based HTTP client used for making API requests to Somnia Network's Blockscout explorer APIs with automatic request/response logging and error handlingwinston- Comprehensive logging library used for creating structured JSON logs with timestamps, component-specific loggers (API, explorer, health), and automatic log rotationexpress- Minimal web framework used for creating health monitoring endpoints and serving the applicationhelmet- Security middleware used for setting HTTP security headers to protect against XSS, clickjacking, and other common web vulnerabilitiescors- Cross-Origin Resource Sharing middleware used for enabling secure cross-origin requests from frontend applicationsdotenv- Environment variable loader used for managing configuration settings and sensitive information like API keys and network settingsethers- Ethereum library used for blockchain interactions, RPC connections, and retrieving network information
Project Structure
mkdir src/{config,services,utils} logsPackage Configuration
Update package.json with build scripts:
{
"name": "somnia-explorer-integration",
"version": "1.0.0",
"description": "Somnia Network explorer integration with logging and health checks",
"main": "dist/app.js",
"scripts": {
"build": "tsc",
"start": "node dist/app.js",
"dev": "nodemon --exec ts-node src/app.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": ["somnia", "blockchain", "explorer", "blockscout"],
"author": "Your Name",
"license": "MIT"
}Configuration
Network Settings
Create a configuration file for Somnia Network settings to:
Consolidate all Somnia Network endpoints (testnet/mainnet) in one location
Enable automatic environment-based network switching
Provide TypeScript interfaces for type safety
Make it easy to update endpoints without changing multiple files
// src/config/network.ts
export const SOMNIA_CONFIG = {
testnet: {
name: 'Somnia Testnet',
rpcUrl: 'https://dream-rpc.somnia.network',
chainId: 50312,
explorerUrl: 'https://shannon-explorer.somnia.network',
explorerApiUrl: 'https://shannon-explorer.somnia.network/api'
},
mainnet: {
name: 'Somnia Mainnet',
rpcUrl: 'https://somnia-json-rpc.stakely.io',
chainId: 5031,
explorerUrl: 'https://explorer.somnia.network',
explorerApiUrl: 'https://explorer.somnia.network/api'
}
};
export const getCurrentNetwork = () => {
return process.env.NODE_ENV === 'production'
? SOMNIA_CONFIG.mainnet
: SOMNIA_CONFIG.testnet;
};Environment Variables
Why create a .env file:
Keeps sensitive information out of source code
Useful when using third-party RPC API Keys
Enables different configurations for development/production environments
Controls which Somnia network to use (testnet/mainnet) via
NODE_ENVAllows easy modification of settings without code changes
# .env
NODE_ENV=development
PORT=3000
LOG_LEVEL=infoAPI Integration
Explorer Service
This service provides comprehensive logging for all API interactions and follows Blockscout's REST API patterns.
This SomniaExplorerService class provides comprehensive logging for all API interactions and follows Blockscout's REST API patterns. The service includes:
Type-safe interfaces for transaction and block data
Axios interceptors for automatic request/response logging
Error handling with detailed logging for debugging
Network-aware configuration that adapts to different Somnia environments
Tip: The service automatically logs all API requests and responses, making it easy to debug issues and monitor performance.
Logging System
Winston Configuration
This Winston configuration provides several enterprise-grade features that make it production-ready:
Structured JSON logs: logs are formatted as JSON objects with consistent fields like
timestamp,level,message, andmetadata. This makes it easy to parse logs programmatically, search for specific events, and integrate with log analysis tools like ELK Stack or Splunk.File rotation: Automatically manages log file sizes by creating new files when they reach 5MB and keeping only the 5 most recent files.
Component-specific loggers: Creates separate logger instances for different parts of your application (API, Explorer, Health) using
logger.child().Error tracking with stack traces: Captures complete error information including stack traces using
winston.format.errors({ stack: true }).Performance metrics: Logs response times and request metadata, allowing monitoring of API performance.
Separate log files by purpose: Different files for general logs, API requests, errors, exceptions, and promise rejections.
Warning: Ensure the logs directory exists before starting your application, or Winston will fail to write log files.
Usage Examples
Structured Logging Output
{
"timestamp": "2024-01-15 10:30:45",
"level": "info",
"message": "Somnia Explorer API Request",
"service": "somnia-explorer-integration",
"network": "testnet",
"component": "explorer",
"method": "GET",
"url": "/v2/transactions/0x1234...",
"baseURL": "https://shannon-explorer.somnia.network/api",
"chain": "somnia"
}API Service Usage
// Initialize the explorer service
const explorerService = new SomniaExplorerService();
// Get transaction data
const txData = await explorerService.getTransaction('0x1234...');
if (txData) {
console.log('Transaction found:', txData.hash);
}
// Get block data
const blockData = await explorerService.getBlock(12345);
if (blockData) {
console.log('Block transactions:', blockData.transactions.length);
}
// Get address transactions
const addressTxs = await explorerService.getAddressTransactions(
'0xabcd...',
1, // page
10 // limit
);
console.log('Address transactions:', addressTxs.length);Troubleshooting
You've successfully implemented:
Comprehensive API integration with the Somnia Explorer using type-safe interfaces
Enterprise-grade logging with Winston, including structured JSON output and file rotation
Error handling and monitoring for production-ready applications
Performance tracking with request/response logging and timing metrics
Your application now has robust blockchain data access with proper logging infrastructure that will help you monitor, debug, and maintain your Somnia-based applications in production.
Last updated