8000 Perform rate limiting · fastapi-users fastapi-users · Discussion #1505 · GitHub
[go: up one dir, main page]

Skip to content

Perform rate limiting #1505

Answered by hgalytoby
alfonsodipace asked this question in Q&A
Apr 14, 2025 · 1 comments · 2 replies
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
0