brk_server
HTTP server providing REST API access to Bitcoin analytics data
Overview
This crate provides a high-performance HTTP server built on axum that exposes Bitcoin blockchain analytics data through a comprehensive REST API. It integrates with the entire BRK ecosystem, serving data from indexers, computers, and parsers with intelligent caching, compression, and multiple output formats.
Key Features:
- RESTful API for blockchain data queries with flexible filtering
- Multiple output formats: JSON, CSV
- Intelligent caching system with ETags and conditional requests
- HTTP compression (Gzip, Brotli, Deflate, Zstd) for bandwidth efficiency
- Static file serving for web interfaces and documentation
- Bitcoin address and transaction lookup endpoints
- Vector database query interface with pagination
- Health monitoring and status endpoints
Target Use Cases:
- Bitcoin data APIs for applications and research
- Web-based blockchain explorers and analytics dashboards
- Research data export and analysis tools
- Integration with external systems requiring Bitcoin data
Installation
Quick Start
use Server;
use Interface;
use PathBuf;
// Initialize interface with your data sources
let interface = new;
// Optional static file serving directory
let files_path = Some;
// Create and start server
let server = new;
// Start server with optional MCP (Model Context Protocol) support
server.serve.await?;
API Overview
Core Endpoints
Blockchain Queries:
GET /api/address/{address}- Address information, balance, transaction countsGET /api/tx/{txid}- Transaction details including version, locktimeGET /api/vecs/{variant}- Vector database queries with filtering
System Information:
GET /api/vecs/index-count- Total number of indexes availableGET /api/vecs/id-count- Vector ID statisticsGET /api/vecs/indexes- List of available data indexesGET /health- Service health statusGET /version- Server version information
Vector Database API
Query Interface:
GET /api/vecs/query- Generic vector query with parametersGET /api/vecs/{variant}?from={start}&to={end}&format={format}- Range queries
Supported Parameters:
from/to: Range filtering (height, timestamp, date-based)format: Output format (json, csv)- Pagination parameters for large datasets
Address API Response Format
Examples
Basic Server Setup
use Server;
use Interface;
// Initialize with BRK interface
let interface = builder
.with_indexer_path
.with_computer_path
.build?;
let server = new;
// Server automatically finds available port starting from 3110
server.serve.await?;
Address Balance Lookup
# Get address information
# Response includes balance, transaction counts, USD value
{
}
Data Export Queries
# Export height-to-price data as CSV
# Query with caching - subsequent requests return 304 Not Modified
Transaction Details
# Get transaction information
# Response includes version, locktime, and internal indexing
{
}
Architecture
Server Stack
- HTTP Framework:
axumwith async/await for high concurrency - Compression: Multi-algorithm support (Gzip, Brotli, Deflate, Zstd)
- Caching:
quick_cachewith LRU eviction and ETag validation - Tracing: Request/response logging with latency tracking
- Static Files: Optional web interface serving
Caching Strategy
The server implements intelligent caching:
- ETags: Generated from data version and query parameters
- Conditional Requests: 304 Not Modified responses for unchanged data
- Memory Cache: LRU cache with configurable capacity (5,000 entries)
- Cache Control:
must-revalidateheaders for data consistency
Request Processing
- Route Matching: Path-based routing to appropriate handlers
- Parameter Validation: Query parameter parsing and validation
- Data Retrieval: Interface calls to indexer/computer components
- Caching Logic: ETag generation and cache lookup
- Format Conversion: JSON/CSV output formatting
- Compression: Response compression based on Accept-Encoding
- Response: HTTP response with appropriate headers
Static File Serving
Optional static file serving supports:
- Web interface hosting for blockchain explorers
- Documentation and API reference serving
- Asset serving (CSS, JS, images) with proper MIME types
- Directory browsing with index.html fallback
Configuration
Environment Variables
The server automatically configures itself but respects:
- Port selection: Starts at 3110, increments if unavailable
- Compression: Enabled by default for all supported algorithms
- CORS: Permissive headers for cross-origin requests
Memory Management
- Cache size: 5,000 entries by default
- Request weight limits: 65MB maximum per query
- Timeout handling: 50ms cache guard timeout
- Compression: Adaptive based on content type and size
Code Analysis Summary
Main Components: Server struct with AppState containing interface, cache, and file paths
HTTP Framework: Built on axum with middleware for compression, tracing, and CORS
API Routes: Address lookup, transaction details, vector queries, and system information
Caching Layer: quick_cache integration with ETag-based conditional requests
Data Integration: Direct interface to BRK indexer, computer, parser, and fetcher components
Static Serving: Optional file serving for web interfaces and documentation
Architecture: Async HTTP server with intelligent caching and multi-format data export capabilities
This README was generated by Claude Code