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:

Network
Explorer URL
API Endpoint

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:

Dependencies

Install required packages:

Core 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 handling

  • winston - Comprehensive logging library used for creating structured JSON logs with timestamps, component-specific loggers (API, explorer, health), and automatic log rotation

  • express - Minimal web framework used for creating health monitoring endpoints and serving the application

  • helmet - Security middleware used for setting HTTP security headers to protect against XSS, clickjacking, and other common web vulnerabilities

  • cors - Cross-Origin Resource Sharing middleware used for enabling secure cross-origin requests from frontend applications

  • dotenv - Environment variable loader used for managing configuration settings and sensitive information like API keys and network settings

  • ethers - Ethereum library used for blockchain interactions, RPC connections, and retrieving network information

Project Structure

Package Configuration

Update package.json with build scripts:

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

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_ENV

  • Allows easy modification of settings without code changes

API Integration

Explorer Service

This service provides comprehensive logging for all API interactions and follows Blockscout's REST API patterns.

src/services/somniaExplorerService.ts

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

src/utils/logger.ts

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, and metadata. 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.

Usage Examples

Structured Logging Output

API Service Usage

Troubleshooting

If API requests fail
  • Verify Somnia Network explorer endpoints are accessible

  • Check network connectivity to shannon-explorer.somnia.network or explorer.somnia.network

  • Ensure correct API endpoint format matches Blockscout v2 schema

If logging doesn't work
  • Ensure the logs directory exists: mkdir logs

  • Check file permissions for log files

  • Verify Winston configuration matches your environment

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