ASQL docs / Client APIs

JSON RPC

The server also takes JSON requests on port 7311, for programs that would rather not use a MySQL driver.

  • Listens on 127.0.0.1 unless PUBLIC=1 is set in /etc/aios/asql.conf. The port is RPC_PORT.
  • Each message, both ways, is a 4-byte big-endian length followed by that many bytes of JSON, up to 4 MB.
  • If TOKEN_FILE is set, every request must carry that token in "token".
  • A request times out after 2 seconds.

Queries

Op sql runs one statement starting with SELECT, SHOW, DESCRIBE, DESC, EXPLAIN or WITH. Data changes are refused; use the MySQL port for those.

{"op": "sql", "db": "store", "token": "tok123", "query": "SELECT sku, stock FROM items LIMIT 2"}
{"ok": true, "columns": ["sku", "stock"], "rows": [["TIRE-G1", 48], ["TIRE-S2", 120]], "row_count": 2}

Op ping checks the server is up and op stats returns its counters.

Example (Python)

import json, socket, struct

def rpc(req, host="127.0.0.1", port=7311):
    body = json.dumps(req).encode()
    with socket.create_connection((host, port)) as s:
        s.sendall(struct.pack(">I", len(body)) + body)
        n = struct.unpack(">I", s.recv(4, socket.MSG_WAITALL))[0]
        return json.loads(s.recv(n, socket.MSG_WAITALL))

print(rpc({"op": "sql", "db": "store", "query": "SELECT COUNT(*) FROM items"}))