Use
RPC and REST calls
callRPC, callRPCBatch, callWithQuorum, and callREST send requests to Hive nodes, with failover.
callRPC
Sends a JSON-RPC 2.0 request and returns the result. A Hive error throws RPCError.
import { callRPC } from 'hive-tx'
const accounts = await callRPC('condenser_api.get_accounts', [['alice']])
const props = await callRPC('condenser_api.get_dynamic_global_properties')
| Parameter | Default | Meaning |
|---|---|---|
method | required | API method name |
params | [] | Array or object |
timeout | config.timeout | Per request, in milliseconds |
retry | config.retry | How many other nodes to try after a failure |
When a node times out or fails, the call moves to the next URL in config.nodes. A Hive RPCError is not retried, because every node would give the same answer. Method names are in the Hive API definitions.
callRPCBatch
Calls one method many times in a single JSON-RPC batch request. Pass one params entry per call; results come back in the same order. Failover, timeout, and retry work like callRPC. If any call in the batch fails with a Hive error, it throws RPCError.
import { callRPCBatch } from 'hive-tx'
const blocks = await callRPCBatch('condenser_api.get_block', [[1], [2], [3]])
Keep batches modest, around 20 to 50 calls: nodes time out on very large responses.
callWithQuorum
Sends the call to several random nodes and returns only when quorum of them return identical JSON. Use it when a wrong answer from one node would be costly.
import { callWithQuorum } from 'hive-tx'
const accounts = await callWithQuorum('condenser_api.get_accounts', [['alice']], 2)
The default quorum is 2, and it cannot exceed config.nodes.length.
callREST
Calls a REST API on config.restNodes. Not every public node serves REST.
import { callREST } from 'hive-tx'
const balance = await callREST(
'balance',
'/accounts/{account-name}/balances',
{ 'account-name': 'alice' }
)
APIs: balance, hafah, hafbe, hivemind, hivesense, reputation, nft-tracker, hafsql, and status. TypeScript checks the endpoint, its parameters, and the response for each one.
Fields named in the path, like {account-name}, fill the path; the rest become query parameters, and arrays repeat the key.
| Parameter | Default | Meaning |
|---|---|---|
api | required | One of the API names above |
endpoint | required | Endpoint path, with {placeholders} |
params | none | Path and query values |
timeout | config.timeout | Per request, in milliseconds |
retry | config.retry | How many other nodes to try after a failure |
Network errors, timeouts, and HTTP 5xx move to the next REST node. HTTP 404 throws at once without failover, because it usually means a wrong endpoint or parameter.
RPCError
RPCError has message, code, and optional data from the node.
import { callRPC, RPCError } from 'hive-tx'
try {
await callRPC('condenser_api.get_accounts', [['alice']])
} catch (e) {
if (e instanceof RPCError) console.log(e.code, e.message, e.data)
else throw e // every node failed
}
To follow new blocks and operations, see Streams.