RPC Overview
The Chia node and services come with a JSON RPC API server that allows you to access information and control the services. These are accessible via HTTP, WebSockets, or via client SDKs. The ports can be configured in ~/.chia/mainnet/config/config.yaml
. The RPC ports should not be exposed to the internet. TLS certificates are used to secure the communication.
Default Ports:
- Daemon: 55400
- Full Node: 8555
- Farmer: 8559
- Harvester: 8560
- Wallet: 9256
- DataLayer: 8562
HTTP/JSON
The certificates must be used when calling the RPCs from the command line, make sure to use the correct certificates for the services you are calling. All endpoints are made with POST with JSON data. The response is a JSON dictionary with a success field, which can be true or false.
WebSockets
If you are using the Websockets API, you can go directly through the daemon, which routes requests. Each WebSocket message contains the following fields: Some examples can be found here: https://github.com/Chia-Mine/chia-agent.
{
"command": "get_blockchain_state",
"ack": false,
"data": {},
"request_id": "123456",
"destination": "wallet",
"origin": "ui"
}
WebSockets Example (courtesy of baerrs)
import json
from datetime import datetime
import websocket
import ssl
# websocket.enableTrace(True)
def on_message(ws, message):
print('{0}: Got message: {1}'.format(datetime.now(), message))
def on_error(self, error):
print('Error in websocket: {0}'.format(error))
def on_close(self, ws, e):
print("{0]: Websocket closed: {1}".format(datetime.now(), e))
def on_open(self):
print('{0}: Connected to Websocket'.format(datetime.now()))
message = {"destination": "daemon", "command": "register_service", "request_id": "123456ca", "origin": "", "data": { "service": 'chia_agent'}}
on_send_message(self, message)
message = {"destination": "daemon", "command": "register_service", "request_id": "123456w", "origin": "", "data": { "service": 'wallet_ui'}}
on_send_message(self, message)
def on_send_message(ws, message):
print('{0}: Sent Message: {1}'.format(datetime.now(), message))
wsapp.send(json.dumps(message))
def on_ping(ws, data):
print('{0}: Got ping: {1}'.format(datetime.now(), data))
print("Starting Something")
wsapp = websocket.WebSocketApp("wss://localhost:55400",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_ping=on_ping)
wsapp.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE, "certfile": "ssl/daemon/private_daemon.crt",
"keyfile": "ssl/daemon/private_daemon.key", "ssl_context.check_hostname": False})
wsapp.close()