Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
using slowapi with a datamodel instead of request as parameter

I want to use slowapi on a fastapi python app, for implementing RateLimiter. However, The definitions of the endpoints use a data model for parameters (inheriting from BaseModel) rather than a request, as the documentation of slowapi require. here is a sample endpoint from the code:

@app.post("/process_user")
def process_user(
    params: NewUser,
    pwd: str = Depends(authenticate),
):

where NewUser is the data model

class NewUser(BaseModel):
   ...

How can I add a slowapi rate limiter with minimum change of the design of the code?

Thanks.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to pass a Starlette request with your Pydantic model. For example...

from fastapi import FastAPI
from pydantic import BaseModel
from slowapi import Limiter
from slowapi.util import get_remote_address
from starlette.requests import Request


limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter


class NewUser(BaseModel):
   ...


@app.post("/process_user")
@limiter.limit("1/second")
def process_user(
    params: NewUser,
    pwd: str = Depends(authenticate),
    request: Request
):
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!