Skip to content
Beau Barker edited this page Jul 4, 2025 · 4 revisions

Sanic server.

⚠️ Sanic was completely rebuilt since this example was written, so it may no longer be valid.

from sanic import Sanic
from sanic.request import Request
from sanic.response import HTTPResponse, json

from jsonrpcserver import Result, Success, dispatch_to_serializable, method

app = Sanic("JSON-RPC app")


@method
def ping() -> Result:
    """JSON-RPC method"""
    return Success("pong")


@app.route("/", methods=["POST"])
async def test(request: Request) -> HTTPResponse:
    """Handle Sanic request"""
    return json(dispatch_to_serializable(request.body))


if __name__ == "__main__":
    app.run(port=5000)

Reference: JSON-RPC in Sanic.

Clone this wiki locally