NEW: Fenrir v1.2.2 is now available — Logo & Favicon Patch! Read the changelog
dependency-injection.md
docs dependency-injection.md

Dependency Injection

Basic Dependency Injection

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from fenrir import Depends

# Define a dependency
async def get_current_user(x_token: str = Header(...)):
    if x_token != "valid_token":
        raise HTTPException(status_code=401)
    return {"user_id": 1, "name": "John"}

# Use dependency
@app.get("/protected")
async def protected_route(current_user: dict = Depends(get_current_user)):
    return {"message": f"Hello {current_user['name']}"}

Database Session Dependency

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from fenrir import Depends

async def get_db():
    # In production, use actual database connection
    db = {"connection": "active"}
    try:
        yield db
    finally:
        # Cleanup
        print("Database connection closed")

@app.get("/users/<user_id:int>")
async def get_user(user_id: int, db: dict = Depends(get_db)):
    return {"user_id": user_id, "from_db": True}

Dependency with Sub-dependencies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
async def verify_token(authorization: str = Header(...)):
    if not authorization.startswith("Bearer "):
        raise HTTPException(status_code=401)
    token = authorization.replace("Bearer ", "")
    return token

async def get_current_user(token: str = Depends(verify_token)):
    # Token validation logic
    return {"user_id": 1}

@app.get("/profile")
async def get_profile(current_user: dict = Depends(get_current_user)):
    return current_user
Edit on GitHub Last Updated: Jun 04, 2026
© 2026 Fenrir Project.
main*
v1.2.2
Ln 1, Col 1
UTF-8
Prettier
Light Mode
Markdown