[Python] How to disable Authentication in FastAPI based on environment?

Discussão em 'Python' iniciado por Stack, Junho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have a FastAPI application for which I enable Authentication by injecting a dependency function.

    controller.py

    router = APIRouter(
    prefix="/v2/test",
    tags=["helloWorld"],
    dependencies=[Depends(api_key)],
    responses={404: {"description": "Not found"}},


    )

    Authorzation.py

    async def api_key(api_key_header: str = Security(api_key_header_auth)):
    if api_key_header != API_KEY:
    raise HTTPException(
    status_code=401,
    detail="Invalid API Key",
    )


    This works fine. However, I would like to disable the authentication based on environment. For instance, I would want to keep entering the authentication key in localhost environment.

    Continue reading...

Compartilhe esta Página