Skip to main content

JSON-RPC server based on fastapi

Project description

fastapi-jsonrpc

tests

JSON-RPC 2.0 server on top of FastAPI. Write JSON-RPC methods the same way you write FastAPI endpoints and get OpenAPI, Swagger UI and OpenRPC for free.

fastapi-jsonrpc Swagger UI

📚 Documentation: https://smagafurov.github.io/fastapi-jsonrpc/

Install

pip install fastapi-jsonrpc

Minimal example

import fastapi_jsonrpc as jsonrpc
from pydantic import BaseModel
from fastapi import Body


app = jsonrpc.API()
api_v1 = jsonrpc.Entrypoint('/api/v1/jsonrpc')


class MyError(jsonrpc.BaseError):
    CODE = 5000
    MESSAGE = 'My error'

    class DataModel(BaseModel):
        details: str


@api_v1.method(errors=[MyError])
def echo(data: str = Body(..., examples=['hello'])) -> str:
    if data == 'error':
        raise MyError(data={'details': 'boom'})
    return data


app.bind_entrypoint(api_v1)


if __name__ == '__main__':
    import uvicorn
    uvicorn.run('app:app', port=5000, access_log=False)

Run it with uvicorn and open:

  • POST /api/v1/jsonrpc — JSON-RPC endpoint
  • GET /docs — Swagger UI
  • GET /openapi.json — OpenAPI schema
  • GET /openrpc.json — OpenRPC schema

Features

  • All of FastAPI — Depends, Body, Header, Cookie, Pydantic models, async/await.
  • Auto-generated OpenAPI and OpenRPC schemas.
  • Typed errors with Pydantic DataModel included in the schema.
  • Batch requests and notifications.
  • Context-manager JSON-RPC middlewares.
  • Optional Sentry integration (fastapi_jsonrpc.contrib.sentry.FastApiJsonRPCIntegration).
  • Pytest plugin for capturing JSON-RPC errors in tests.

See the full docs at https://smagafurov.github.io/fastapi-jsonrpc/.

Development

# Install dependencies
uv sync --frozen --group dev

# Run tests
uv run --frozen python -m pytest

# Run a single test
uv run --frozen python -m pytest tests/test_jsonrpc.py::test_name -x

# Change dependencies — edit pyproject.toml, then:
uv lock

# Build and publish
uv build
uv publish

License

MIT — see LICENSE.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_jsonrpc-3.5.0.tar.gz (446.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_jsonrpc-3.5.0-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_jsonrpc-3.5.0.tar.gz.

File metadata

  • Download URL: fastapi_jsonrpc-3.5.0.tar.gz
  • Upload date:
  • Size: 446.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_jsonrpc-3.5.0.tar.gz
Algorithm Hash digest
SHA256 e4fb3a1d8e4a32aed980dad819992b34299e247a35a8d28d930f6e972f9eb1dd
MD5 93cfce2613fabfae447666a8adce9cf9
BLAKE2b-256 3ec94e5a5eb24f7d99aa1cf1f37a2c64e9c6d79c40c19a96541345157da362c7

See more details on using hashes here.

File details

Details for the file fastapi_jsonrpc-3.5.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_jsonrpc-3.5.0-py3-none-any.whl
  • Upload date:
  • Size: 23.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_jsonrpc-3.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f13b7aa6c1f020222a9e997686bd92c43e123b3cf690f743357950d4aa4201b8
MD5 f3ad4d8440e6314a5c510d1c39c6942e
BLAKE2b-256 139039821f8171569469852502c868a47e91c091852230db4250e89c7e8c6f46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page