How to Use Rpc/Restful/Websocke
<h1 align="center">How to Use Rpc/Restful/Websocket API</h1>
<p align="center" class="version">Version 0.1</p>
1. The introduction of Ontology-API
Rpc API: http://dev-docs.ont.io/#/docs-en/API/01-rpc_api
Restful API: http://dev-docs.ont.io/#/docs-en/API/02-restful_api
Websocket API: http://dev-docs.ont.io/#/docs-en/API/03-websocket_api
2. How to use restful API in SmartX
Toolkit introduction:

Open SmartX, create a python project and select "Hello World" template.

Click the "Restful" button on the panel and then enter the "Restful" panel.

There are 23 restful APIs. You can select the network and then use the API. For example, we select Main-Net and click "Send" button of the first API. Then we receive the result as shown below.

3. The Rpc API in Python SDK
You can get the code of Rpc API at the following address.
Rpc code: https://github.com/ontio/ontology-python-sdk/blob/master/ontology/rpc/rpc.py
Request parameter description:

Response parameter description:

For example, If you call the "getbestblockhash" API, the following request will be sent.
{
"jsonrpc": "2.0",
"method": "getbestblockhash",
"params": [],
"id": 1
}
And you will receive the following response.
{
"desc":"SUCCESS",
"error":0,
"jsonrpc": "2.0",
"id": 1,
"result": "773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e"
}
If the execution fails, you will receive the error code.
Error code instruction
Field | Type | Description |
---|---|---|
0 | int64 | SUCCESS |
41001 | int64 | SESSION_EXPIRED: invalided or expired session |
41002 | int64 | SERVICE_CEILING: reach service limit |
41003 | int64 | ILLEGAL_DATAFORMAT: illegal dataformat |
41004 | int64 | INVALID_VERSION: invalid version |
42001 | int64 | INVALID_METHOD: invalid method |
42002 | int64 | INVALID_PARAMS: invalid params |
43001 | int64 | INVALID_TRANSACTION: invalid transaction |
43002 | int64 | INVALID_ASSET: invalid asset |
43003 | int64 | INVALID_BLOCK: invalid block |
44001 | int64 | UNKNOWN_TRANSACTION: unknown transaction |
44002 | int64 | UNKNOWN_ASSET: unknown asset |
44003 | int64 | UNKNOWN_BLOCK: unknown block |
45001 | int64 | INTERNAL_ERROR: internel error |
47001 | int64 | SMARTCODE_ERROR: smartcode error |
4. How to use Rpc API in Python SDK
- Install Ontology Python SDK
pip install ontology-python-sdk
- Import Ontology package
from ontology.ont_sdk import OntologySdk
- Initialize the instance and set up the Rpc Address
Now, we use the Private-Net. So we set Rpc address to http://127.0.0.1:20336.
sdk = OntologySdk()
rpc_address = 'http://127.0.0.1:20336'
sdk.rpc.set_address(rpc_address)
- Call Rpc API
version = sdk.rpc.get_version()
count = sdk.rpc.get_node_count()
print(version)
print(count)
- Result
v1.0.5
1