Eager to get started? This page gives a good introduction to JSON-RPC Python.

A Minimal Application#

A minimal JSON-RPC Python application looks something like this:

from jsonrpc import ASGIHandler

app: ASGIHandler = ASGIHandler()

async def greeting(name: str) -> str:
    return f"Hello, {name}"

So what did that code do?

  1. First we imported the jsonrpc.ASGIHandler class. An instance of this class will be our ASGI application.

  2. Next we create an instance of this class.

  3. We accessing to the jsonrpc.ASGIHandler.dispatcher, instance of the jsonrpc.AsyncDispatcher class, which represents the dictionary of all registered user-defined functions.

  4. Then we use the jsonrpc.AsyncDispatcher.register() decorator to append our function to the our dispatcher.

  5. The function greeting returns the formatted string with greetings message.

Now, you can run it with your preferable ASGI server like Uvicorn, Hypercorn etc.

$ uvicorn example:app

And now, you can remotely invoke the greeting method:

$ curl \
    --data '{"jsonrpc": "2.0", "method": "greeting", "params": ["John Doe"], "id": 1}' \
    --header 'Content-Type: application/json' \
    --silent \
    --show-error \

Finally, you will get the response like this:

  "jsonrpc": "2.0",
  "result": "Hello, John Doe",
  "id": 1