Skip to main content

Overview

Makes API calls to Hive blockchain nodes with automatic retry and failover support. Automatically switches between multiple nodes using JSON-RPC 2.0 protocol. If the current node fails, it will automatically try the next node in the list.

Function Signature

export const callRPC = async <T = any>(
  method: string,
  params: any[] | object = [],
  timeout = config.timeout,
  retry = config.retry
): Promise<T>

Parameters

method
string
required
The API method name (e.g., 'condenser_api.get_accounts', 'condenser_api.get_dynamic_global_properties')
params
any[] | object
default:"[]"
Parameters for the API method as an array or object. Most Hive API methods expect an array of parameters.
timeout
number
default:"config.timeout"
Request timeout in milliseconds. If not specified, uses the default timeout from configuration.
retry
number
default:"config.retry"
Number of retry attempts before throwing an error. The function will try different nodes on failure.

Return Type

result
Promise<T>
Returns a Promise that resolves to the API response. The response type can be specified using TypeScript generics.

Error Handling

The function throws an RPCError when the Hive node returns an error response:
export class RPCError extends Error {
  name = 'RPCError'
  data?: any
  code: number
  stack: undefined = undefined
  constructor(rpcError: { message: string; code: number; data?: any })
}

RPCError Properties

message
string
The error message from the RPC server
code
number
The error code from the RPC server
data
any
Additional error data if provided by the server

Examples

Get Account Information

import { callRPC } from 'hive-tx'

const accounts = await callRPC('condenser_api.get_accounts', [['alice']])
console.log(accounts)

Get Dynamic Global Properties

import { callRPC } from 'hive-tx'

const props = await callRPC('condenser_api.get_dynamic_global_properties', [])
console.log(props.head_block_number)

Get Content (Blog Post)

import { callRPC } from 'hive-tx'

const content = await callRPC('condenser_api.get_content', ['alice', 'test-post'])
console.log(content.title, content.body)

Custom Timeout and Retry Settings

import { callRPC } from 'hive-tx'

// 10 second timeout, 8 retry attempts
const data = await callRPC(
  'condenser_api.get_content',
  ['alice', 'test-post'],
  10_000,
  8
)

With TypeScript Type Safety

import { callRPC } from 'hive-tx'

interface Account {
  name: string
  id: number
  created: string
  // ... other fields
}

const accounts = await callRPC<Account[]>(
  'condenser_api.get_accounts',
  [['alice', 'bob']]
)
// accounts is typed as Account[]

Error Handling Example

import { callRPC, RPCError } from 'hive-tx'

try {
  const result = await callRPC('condenser_api.get_accounts', [['alice']])
  console.log(result)
} catch (error) {
  if (error instanceof RPCError) {
    console.error('RPC Error:', error.message)
    console.error('Error Code:', error.code)
    console.error('Error Data:', error.data)
  } else {
    console.error('Network or timeout error:', error)
  }
}

Behavior

  • Uses JSON-RPC 2.0 protocol
  • Automatically rotates through configured nodes on failure
  • Throws RPCError immediately on actual RPC errors (doesn’t retry)
  • Retries on network or timeout errors with different nodes
  • Validates JSON-RPC response format (checks id, jsonrpc version)

See Also

Build docs developers (and LLMs) love