IOTA JSON-RPC
Theiota-json-rpc library crate provides a flexible framework for building JSON-RPC servers to expose IOTA APIs.
Overview
The JSON-RPC framework:- Supports requests over HTTP and WebSocket
- Primarily used to spawn nested services in
iota-indexeroriota-nodeapplications - Provides modular API implementations that can be selectively registered
Architecture
The library provides module implementations for various JSON-RPC server traits:| Server Trait | Derived From | Purpose |
|---|---|---|
IndexerApiServer | IndexerApi | Indexer-specific queries |
GovernanceReadApiServer | GovernanceReadApi | Governance and epoch data |
MoveUtilsApiServer | MoveUtilsApi | Move utilities |
CoinReadApiServer | CoinReadApi | Coin and balance queries |
TransactionBuilderApiServer | TransactionBuilderApi | Transaction construction |
ReadApiServer | ReadApi | General read operations |
WriteApiServer | WriteApi | Transaction execution |
iota-json-rpc-api crate from JSON-RPC API definitions.
Module Registration
Modules are not automatically registered and must be explicitly included based on requirements:- For
iota-node: Modules useAuthorityStatefor internal state - For
iota-indexer: Modules useIndexerReaderfor database access
IotaRpcModule trait providing:
- List of supported JSON-RPC methods
- OpenRPC documentation
Building a JSON-RPC Server
Basic Example
Key Components
JsonRpcServerBuilder: Main builder for constructing the server- Takes version string and Prometheus registry
- Registers modules via
register_module() - Starts server with
start()
ServerType::Http: HTTP-only serverServerType::WebSocket: WebSocket-only serverNone: Both HTTP and WebSocket
Available API Modules
Read APIs
ReadApi: Core read operations- Get objects by ID
- Query transaction blocks
- Get checkpoint data
- Protocol configuration
- Chain identifier
- Search objects by filter
- Query events
- Transaction history for objects/addresses
- Get all coins for an address
- Get coin metadata
- Get balance by coin type
- Epoch data and validators
- Staking information
- Committee composition
- Normalize struct tags
- Resolve Move types
Write APIs
WriteApi: Transaction execution- Execute signed transactions
- Submit transactions to validators
- Wait for transaction confirmation
- Build transfer transactions
- Build Move call transactions
- Build publish transactions
- Split/merge coins
Usage in IOTA Components
In iota-indexer
The indexer uses JSON-RPC to expose indexed data:IndexerApi: For querying indexed dataCoinReadApi: For coin queriesGovernanceReadApi: For governance dataWriteApi: For transaction execution (proxied to fullnode)
In iota-node
Fullnodes and validators expose JSON-RPC APIs: Fullnode APIs:- All read APIs
- Transaction execution APIs
- Transaction builder APIs
- Read-only APIs
- No transaction execution
Connection Examples
Using cURL
Using IOTA SDK (TypeScript)
Using IOTA SDK (Rust)
Configuration Options
Server Configuration
- Socket Address: IP and port to bind
- Runtime: Optional Tokio runtime handle
- Server Type: HTTP, WebSocket, or both
Module Selection
Choose which API modules to expose based on:- Security requirements: Limit exposed APIs
- Performance: Reduce overhead by excluding unnecessary modules
- Functionality: Match client needs
OpenRPC Documentation
Each module provides OpenRPC specification for its methods:- Method names and parameters
- Request/response schemas
- Error codes and descriptions
rpc.discover method.
Environment Variables
Common environment variables:DATABASE_CONNECTION_URL: Database URL for indexer modulesRPC_TIMEOUT_SECS: Request timeout in secondsMAX_CONCURRENT_REQUESTS: Maximum concurrent requests
Best Practices
- Module Selection: Only register modules you need
- Metrics: Always provide a Prometheus registry for monitoring
- Timeouts: Configure appropriate request timeouts
- Concurrency: Set concurrency limits based on resources
- Error Handling: Implement proper error handling for all RPC methods