This manual demonstrates JSON-RPC requests usage for Python developers (Linux).
Requests can be processed:
- [[JSON-RPC requests for Python#Request to the local Node via the UNIX-socket|on the local Node via the UNIX-socket]]
- [[JSON-RPC requests for Python#Request to the RPC Node|on the RPC Nodes (remote)]]
List of available requests:
**![[2. JSON-RPC Requests#JSON-RPC requests to interact with the Node node|JSON-RPC Requests]]**
## Request to the local Node via the UNIX-socket
This type of requests is provided via the `unix` socket on the local machine, where the Cellframe Node is installed.
### Request Structure
This is a common structure of JSON-RPC requests on Python.
To be able to make requests make sure that:
- Python 3.6+ is installed
- The process `cellframe-node` is running
- The corresponding network `network_name` is fully synchronized and online
- Libraries `requests_unixsocket` and `json` are installed and imported
```python
import requests_unixsocket
import json
# Request Settings
SOCKET_PATH = "/opt/cellframe-node/var/run/node_cli"
URL = "http+unix://%2Fopt%2Fcellframe-node%2Fvar%2Frun%2Fnode_cli/connect"
# JSON OBJECT
REQUEST_DATA = '{
"method": "method_name",
"subcommand": "subcommand_name",
"arguments": {
"arg_name": "value"
},
"id": "1"
}'
# Sending Request
try:
with requests_unixsocket.Session() as session:
response = session.post(
URL,
data=json.dumps(REQUEST_DATA),
headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
print("Success:")
print(json.dumps(response.json(), indent=2))
else:
print(f"Error: {response.status_code}")
print(response.text)
except Exception as e:
print(f"Request sending error: {e}")
```
### Example
Let's request [[JSON-RPC Request - MEMPOOL COUNT]].
We have to retrieve the JSON object with the requested data and put it into the `REQUEST_DATA` section.
`CURL request structure:`
```actionscript
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '
{
"method": "mempool",
"subcommand": ["count"],
"arguments": {
"net": "Backbone"
},
"id": "1"
}'
```
We will take this part from the CURL request:
```json
{
"method": "mempool",
"subcommand": ["count"],
"arguments": {
"net": "Backbone"
},
"id": "1"
}'
```
And put it into the `REQUEST_DATA` section.
```json
# JSON OBJECT
REQUEST_DATA = '{
"method": "mempool",
"subcommand": "count",
"arguments": {
"network": "Backbone"
},
"id": "1"
}'
```
The rest of the python file `.py` must remain intact.
#### Output
After starting a project we will get the following output in the terminal.
```json
Success:
{
"type": 2,
"result": [
{
"net": "Backbone",
"chains": [
{
"name": "zerochain",
"count": 3
},
{
"name": "main",
"count": 16
}
]
}
],
"id": 1
}
```
This manual is a common for all **[[2. JSON-RPC Requests|JSON-RPC Requests]]**, so you can request any of them by retrieving the following JSON object.
## Request to the RPC Node
This type of request doesn't require the Cellframe Node to be installed on your local machine. We maintain several RPC Nodes that handle your remote requests.
> [!HINT] Important
> Not all requests can be processed on the RPC Nodes. For example, some of the requests from groups [[DAG. JSON-RPC Requests|DAG]] and [[ESBOCS. JSON-RPC Requests|ESBOCS]] are administrative and will not be fulfilled.
### Request Structure
This is a common structure of JSON-RPC requests on the remote nodes.
To be able to make requests make sure that:
- Python 3.6+ is installed
- Libraries `requests` and `json` are installed and imported
```python
import requests
import json
# Remote node settings
RPC_URL = "http://rpc.cellframe.net" # common address
# JSON-RPC request data
REQUEST_DATA = '{
"method": "method_name",
"subcommand": "subcommand_name",
"arguments": {
"arg_name": "value"
},
"id": "1"
}'
# Sending request
try:
response = requests.post(
RPC_URL,
data=json.dumps(REQUEST_DATA),
headers={"Content-Type": "application/json"},
timeout=10 # 10 seconds
)
# Process response
if response.status_code == 200:
result = response.json()
print("Success:")
print(json.dumps(result, indent=2))
if "error" in result:
print(f"JSON-RPC Error: {result['error']}")
else:
print(f"HTTP Error: {response.status_code}")
print(response.text)
except requests.exceptions.RequestException as e:
print(f"Connection Error: {e}")
except json.JSONDecodeError as e:
print(f"Parsing JSON Error: {e}")
except Exception as e:
print(f"Unexpected Error: {e}")
```
### Example
Let's request [[JSON-RPC Request - MEMPOOL COUNT]].
We have to retrieve the JSON object with the requested data and put it into the `REQUEST_DATA` section.
`CURL request structure:`
```actionscript
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '
{
"method": "mempool",
"subcommand": ["count"],
"arguments": {
"net": "Backbone"
},
"id": "1"
}'
```
We will take this part from the CURL request:
```json
{
"method": "mempool",
"subcommand": ["count"],
"arguments": {
"net": "Backbone"
},
"id": "1"
}'
```
And put it into the `REQUEST_DATA` section.
```json
# JSON OBJECT
REQUEST_DATA = '{
"method": "mempool",
"subcommand": "count",
"arguments": {
"network": "Backbone"
},
"id": "1"
}'
```
The rest of the python file `.py` must remain intact.
#### Output
After starting a project we will get the following output in the terminal.
```json
Success:
{
"type": 2,
"result": [
{
"net": "Backbone",
"chains": [
{
"name": "zerochain",
"count": 0
},
{
"name": "main",
"count": 0
}
]
}
],
"id": 1
}
```
This manual fits for all **[[2. JSON-RPC Requests|JSON-RPC Requests]]**, so you can request any of them by retrieving the corresponding JSON object.