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 WSGIHandler

app: WSGIHandler = WSGIHandler()

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

So what did that code do?

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

  2. Next we create an instance of this class.

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

  4. Then we use the jsonrpc.Dispatcher.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 WSGI server like uWSGI, Gunicorn etc.

$ gunicorn 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
}