Course05: How to Use Rpc/Restful/Websocket API
At 2018-11-16 By zhaoyue

How to Use Rpc/Restful/Websocket API

Version 0.1

Click here to view the course video

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:

toolkit

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

create a project

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

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.

First API

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:

request

Response parameter description:

response

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

FieldTypeDescription
0int64SUCCESS
41001int64SESSION _ EXPIRED: invalided or expired session
41002int64SERVICE _ CEILING: reach service limit
41003int64ILLEGAL _ DATAFORMAT: illegal dataformat
41004int64INVALID _ VERSION: invalid version
42001int64INVALID _ METHOD: invalid method
42002int64INVALID _ PARAMS: invalid params
43001int64INVALID _ TRANSACTION: invalid transaction
43002int64INVALID _ ASSET: invalid asset
43003int64INVALID _ BLOCK: invalid block
44001int64UNKNOWN _ TRANSACTION: unknown transaction
44002int64UNKNOWN _ ASSET: unknown asset
44003int64UNKNOWN _ BLOCK: unknown block
45001int64INTERNAL _ ERROR: internel error
47001int64SMARTCODE _ ERROR: smartcode error

4. How to use Rpc API in Python SDK

  1. Install Ontology Python SDK
pip install ontology-python-sdk
  1. Import Ontology package
from ontology.ont_sdk import OntologySdk
  1. 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)
  1. Call Rpc API
version = sdk.rpc.get_version()
count = sdk.rpc.get_node_count()
print(version)
print(count)
  1. Result
v1.0.5
1
Please feel free to give any suggestion
Contact: Yue Zhao 
Wechat: 16621171248
Email: messixaviinsta0303@163.com