Quickstart

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:

example.py
from jsonrpc import ASGIHandler

app: ASGIHandler = ASGIHandler()

@app.dispatcher.register
async def greeting(name: str) -> str:
    return f"Hello, {name}"

So what did that code do?

  1. First we imported the 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 dispatcher, instance of the AsyncDispatcher class, which represents the dictionary of all registered user-defined functions.

  4. Then we use the 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 \
    http://127.0.0.1:8000/

Finally, you will get the response like this:

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