title: Ratelimit in Fastapi
body: <p>How to ratelimit API endpoint request in Fastapi application ? I need to ratelimit API call 5 request per second per user and exceeding that limit blocks that particular user for 60 seconds.</p>
  <p>In main.py</p>
  <pre><code>def get_application() -&gt; FastAPI:
       application = FastAPI(title=PROJECT_NAME, debug=DEBUG, version=VERSION)
       application.add_event_handler(
          &quot;startup&quot;, create_start_app_handler(application))
       application.add_event_handler(
          &quot;shutdown&quot;, create_stop_app_handler(application))
       return application
  app = get_application()
  </code></pre>
  <p>In events.py</p>
  <pre><code>def create_start_app_handler(app: FastAPI) -&gt; Callable:  
      async def start_app() -&gt; None:           
  
          redis = await aioredis.create_redis_pool(&quot;redis://localhost:8080&quot;)
          FastAPILimiter.init(redis)
      return start_app
  </code></pre>
  <p>In endpoint</p>
  <pre><code>@router.post('/user',
               tags=[&quot;user&quot;],
               name=&quot;user:user&quot;, dependencies=[Depends(RateLimiter(times=5, seconds=60))])
  ***code****
  </code></pre>
  <p>Run from this file test.py.</p>
  <pre><code>import uvicorn
  
  from app.main import app
  
  if __name__ == &quot;__main__&quot;:
      uvicorn.run(&quot;test:app&quot;, host=&quot;0.0.0.0&quot;, port=8000, reload=True)
  </code></pre>
  <p>I edited as above but got following error.</p>
  <pre><code>File &quot;****ite-packages\starlette\routing.py&quot;, line 526, in lifespan
      async for item in self.lifespan_context(app):
    File &quot;****site-packages\starlette\routing.py&quot;, line 467, in default_lifespan
      await self.startup()
    File &quot;****site-packages\starlette\routing.py&quot;, line 502, in startup
      await handler()
    File &quot;****app\core\services\events.py&quot;, line 15, in start_app
      redis = await aioredis.create_redis_pool(&quot;redis://localhost:8080&quot;)
    File &quot;****\site-packages\aioredis\commands\__init__.py&quot;, line 188, in create_redis_pool
      pool = await create_pool(address, db=db,
    File &quot;****site-packages\aioredis\pool.py&quot;, line 58, in create_pool
      await pool._fill_free(override_min=False)
    File &quot;C****\site-packages\aioredis\pool.py&quot;, line 383, in _fill_free
      conn = await self._create_new_connection(self._address)
    File &quot;****site-packages\aioredis\connection.py&quot;, line 111, in create_connection
      reader, writer = await asyncio.wait_for(open_connection(
    File &quot;****\asyncio\tasks.py&quot;, line 455, in wait_for
      return await fut
    File &quot;****\site-packages\aioredis\stream.py&quot;, line 23, in open_connection
      transport, _ = await get_event_loop().create_connection(
    File &quot;****\asyncio\base_events.py&quot;, line 1033, in create_connection
      raise OSError('Multiple exceptions: {}'.format(
  OSError: Multiple exceptions: [Errno 10061] Connect call failed ('::1', 8080, 0, 0), [Errno 10061] Connect call failed ('127.0.0.1', 8080)
  </code></pre>