Skip to content
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"
def apply_rate_limit(router, path: str, method: str, limit_str: str):\n    for route in router.routes:\n        if isinstance(route, APIRoute) and route.path == path and method.upper() in route.methods:\n            route.endpoint = limiter.limit(limit_str)(route.endpoint)\n\n\nregister_router = fastapi_users.get_register_router(UserRead, UserCreate)\n\napply_rate_limit(register_router, \"/register\", \"POST\", \"5/minute\")\n\napp.include_router(\n    register_router,\n    prefix=\"/auth\",\n    tags=[\"auth\"],\n)
\n

It's important to note that you must call apply_rate_limit before app.include_router.

\n

I just tested it, and it works without any issues.

","upvoteCount":2,"url":"https://github.com/fastapi-users/fastapi-users/discussions/1505#discussioncomment-12911985"}}}
Discussion options

You must be logged in to vote
def apply_rate_limit(router, path: str, method: str, limit_str: str):
    for route in router.routes:
        if isinstance(route, APIRoute) and route.path == path and method.upper() in route.methods:
            route.endpoint = limiter.limit(limit_str)(route.endpoint)


register_router = fastapi_users.get_register_router(UserRead, UserCreate)

apply_rate_limit(register_router, "/register", "POST", "5/minute")

app.include_router(
    register_router,
    prefix="/auth",
    tags=["auth"],
)

It's important to note that you must call apply_rate_limit before app.include_router.

I just tested it, and it works without any issues.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@alfonsodipace
Comment options

@alfonsodipace
Comment options

Answer selected by alfonsodipace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants