Design and Features of the Polkadot API Server

April 13, 2021 ยท View on GitHub

This page will present the inner workings of the API Server as well as the features that one is able to interact with and how. The following points will be presented and discussed:

Design

The components involved in the API Server are the following:

  • The Polkadot Nodes from which the API retrieves information
  • This API Server which uses the @polkadot/api JavaScript package to retrieve data from the Polkadot Nodes
  • The User/Program which sends GET Requests to the API Server as per the defined Endpoints, and receives JSON formatted responses from the API Server

The diagram below gives an idea of the various components at play when the API Server is running, and how they interact with each other and the user/program:

design

The API Server works as follows:

  • The API Server connects to each of these nodes one by one, as specified in the config/user_config_nodes.ini file.
  • The API connections to each node exist within JS as promise objects.
  • The API Server opens a port, as specified in the config/user_config_main.ini file.
  • By communicating through this port, the API Server receives the endpoints specified in the Complete List of Endpoints section below, and requests information from the nodes it is connected to accordingly.
  • Once the requested information is received from the node, it is formatted as a JSON, and returned.

Complete List of Endpoints

API EndpointRequired InputsOptional InputsOutput
Miscellaneous
/api/pingApiNoneNonepong if the API is accessible
/api/pingNodewebsocketNonepong if the API could access the Node
/api/getConnectionsListNoneNoneList of nodes (websocket_ips) the API is connected to
RPC
/api/rpc/chain/getBlockHashwebsocketblock_numberblock hash of the specified block, or of the latest block if one is not specified
/api/rpc/chain/getFinalizedHeadwebsocketNoneblock hash
/api/rpc/chain/getHeaderwebsockethashheader of the specified block hash, or of the latest block if the hash is not specified
/api/rpc/rpc/methodswebsocketNoneversion and methods, a list of RPC methods that are exposed by the node
/api/rpc/system/chainwebsocketNonesystem chain
/api/rpc/system/healthwebsocketNonesystem health - peers, isSyncing
/api/rpc/system/networkStatewebsocketNonepeerId, listenedAddresses, externalAddresses and connectedPeers for the specified node. The current state of the network
/api/rpc/system/propertieswebsocketNoness58Format, tokenDecimals and tokenSymbol for the network of the specified node. Properties defined in the chain spec
Query
/api/query/balances/totalIssuancewebsocketNoneThe total amount of units issued in the chain. Value may be in Hex
/api/query/council/memberswebsocketNoneList of council members
/api/query/council/proposalCountwebsocketNoneNumber of proposals
/api/query/council/proposalOfwebsocket, hashNoneproposal info - end, proposalHash, treshold, delay
/api/query/council/proposalswebsocketNoneList of proposals
/api/query/democracy/publicPropCountwebsocketNoneNumber of public proposals
/api/query/democracy/referendumCountwebsocketNoneNumber of referendums
/api/query/democracy/referendumInfoOfwebsocket, referendum_indexNonereferendum info - end, proposalHash, treshold, delay
/api/query/imOnline/authoredBlockswebsocket, session_index, validator_idNoneNumber of blocks authored by the specified validatorId in the specified sessionIndex
/api/query/imOnline/receivedHeartbeatswebsocket, session_index, auth_indexNoneAny data which shows that it is still online, despite not having signed any blocks
/api/query/session/currentIndexwebsocketNonecurrent index
/api/query/session/disabledValidatorswebsocketNoneList of disabled validators
/api/query/session/validatorswebsocketNoneList of validators
/api/query/staking/activeErawebsocketNoneindex and start of the active era
/api/query/staking/bondedwebsocket, account_idNoneController account assigned to the stash account with the specified account_id. Will return null if the stash doesn't have a controller assigned.
/api/query/staking/erasRewardPointswebsocketera_indexThe total and individual rewards in the specified era index, or in the active one if it is not specified
/api/query/staking/erasStakerswebsocket, account_idera_indexstakers info - total balance nominated, balance nominated belonging to the owner, List of stakers who have nominated and how much they have nominated in the specified era index, or in the active one if it is not specified
/api/query/staking/erasTotalStakewebsocketera_indexThe total amount staked in the specified era index, or in the active one if it is not specified. Value may be in Hex
/api/query/staking/erasValidatorRewardwebsocketera_indexThe total validator era payout in the specified era index, or in the last finished era (active era - 1) if it is not specified
/api/query/staking/payeewebsocket, account_idNoneReward destination address assigned to the stash with the specified account_id. Returns json with different keys depending on the type of reward destination: staked: null - when reward destination is a stash, controller: addr - when reward destination is a controller, and result: addr when reward destination is any other address.
/api/query/staking/unappliedSlasheswebsocketera_indexList of slashed validators (unapplied) in the specified era, or the current era if an era index is not specified.
/api/query/staking/validatorswebsocket, account_idPreferences of the validator whose stash is the specified account_id (validator commission and blocked status).
/api/query/system/eventswebsocketblock_hashevents that happened in the specified block hash, or in the latest block if the block hash is not specified
Custom
/api/custom/getSlashAmountwebsocket, account_addressblock_hashThe balance slashed (if any) of the specified account address in the specified block hash, or in the latest block if the block hash is not specified
Derive
/api/derive/staking/validatorswebsocketNonenextElected - List of validators which will be active in the next session and validators - List of validators which are currently active

Using the API

For example, the endpoint /api/rpc/system/health can be called as follows: http://localhost:3000/api/rpc/system/health?websocket=ws://1.2.3.4:9944. If successful, this will return:

{
    "result": {
        "peers": 92,
        "isSyncing": false,
        "shouldHavePeers": true
    }
}

If an API connection for the node specified in the websocket field is not set up, this will return:

{
    "error": "An API for ws://1.2.3.4:9944 needs to be setup before it can be queried"
}

If an API call without all the required fields is sent, such as http://localhost:3000/api/query/imOnline/authoredBlocks?websocket=ws://1.2.3.4:9944&session_index=3, this will return:

{
    "error": "You did not enter the stash account address of the validator that needs to be queried"
}

Back to API front page