Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
FastApi middleware on different folder not working

I,m getting this error when I try to run my FastApi api.

app = cls(app=app, **options) TypeError: 'module' object is not callable

I'm trying to add a middleware on other folder separeted from main.py and don't know why isn't working. Otherwise when I add the middleware code into main.py works without problems. Here is my code, thank you for your help and excuse my english.

main.py

from fastapi import FastAPI
from fastapi import Depends, FastAPI, HTTPException
from fastapi import Request
from routers import rutas
from utils import CheckApiKey
from utils.CheckApiKey import check_api_key

app = FastAPI()
app.add_middleware(CheckApiKey, dispatch=check_api_key) <--- Here calling middleware
app.include_router(rutas.router)


if __name__ == "__main__":
  import uvicorn

  uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)

Middleware

from fastapi import Request

async def check_api_key(request: Request, call_next):    

  print("ok")
  response = await call_next(request) 

  return response
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I found the solution...I have to create a class that inherite from starlette basehttpmiddleare, like this.

from starlette.middleware.base import BaseHTTPMiddleware


class CheckApiKey(BaseHTTPMiddleware):
 async def dispatch(self, request, call_next):
    print("ok")
    response = await call_next(request)

    return response
over 4 years ago · Santiago Trujillo Relatório

0

The CheckApiKey seems like a python module in your case and check_api_key is the middleware function.

The issue was, the add_middleware() method expects the first argument as a callable function or callable class. But in your case, you were given a module.

So,

Change your statement as,

app.add_middleware(check_api_key) 
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda