from fastapi import Request from fastapi.responses import JSONResponse from core.config import settings from models.response import BaseResponse # Adjust this import based on your project structure async def api_key_middleware(request: Request, call_next): api_key = settings.get_api_key() if "x-api-key" not in request.headers or request.headers["x-api-key"] != api_key: response_data = BaseResponse( StatusCode=401, Message="Invalid or missing API Key", Data=None # You can now pass None or any other type of data you wish to include ) return JSONResponse(status_code=401, content=response_data.dict()) response = await call_next(request) return response