pyRPC
DemoBlogChangelogDocs
Server

Routers

Organize your API into modular routers.

Routers

pyRPC allows you to organize your API into modular Routers. This is essential for large applications where you want to split procedures across different files and namespaces.

The Default Router

For simple applications, you can use the global rpc decorator. All procedures registered this way go into the default_router.

from pyrpc_core import rpc
from pyrpc_fastapi import mount_fastapi

@rpc
def add(a: int, b: int) -> int:
    return a + b

# Mounts the default_router at /rpc
mount_fastapi(app)

Modular Routers

As your app grows, you can create isolated Router instances.

from pyrpc_core import Router

# Create a scoped router
user_router = Router()

@user_router.rpc
def get_profile(user_id: int):
    return {"id": user_id, "name": "Alice"}

Merging Routers

You can merge multiple routers into a single "root" router. This supports prefixes, which is a powerful way to namespace your API.

from pyrpc_core import Router, mount_fastapi

# Sub-routers
auth_router = Router()
@auth_router.rpc
def login(): ...

post_router = Router()
@post_router.rpc
def create(): ...

# Main app router
app_router = Router()
app_router.merge(auth_router, prefix="auth.")
app_router.merge(post_router, prefix="post.")

# Mount the specific router
mount_fastapi(app, app_router)

In this example:

  • The login procedure is available as auth.login
  • The create procedure is available as post.create

Custom Procedure Names

You can still override individual procedure names within a router:

@user_router.rpc(name="get_user")
def fetch_user_by_id(user_id: int):
    ...

Next Steps