Question: I'm using FastAPI to create a simple api for automating my emails. I want to protect certain routes and I'm using this class: import time import jwt from fastapi import HTTPException, Request from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer #if you need to try it out just swap the following from exchange_api.auth.jwt_handler import JWT_ALGORITHM, JWT_SECRET class JWTBearer(HTTPBearer): def __init__(self, auto_error: bool = True): super(JWTBearer, self).__init__(auto_error=auto_error) async def __call__(self, request: Request): credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request) if credentials: if not credentials.scheme == "Bearer": raise HTTPException(status_code=403, detail="Invalid authentication scheme.") if not self.verify_jwt(credentials.credentials): raise HTTPException(status_code=403, detail="Invalid token or expired token.") return credentials.credentials else: raise HTTPException(status_code=403, detail="Invalid authorization code.") def verify_jwt(self, jwtoken: str) -> bool: isTokenValid: bool = False try: payload = decodeJWT(jwtoken) except: payload = None if payload: isTokenValid = True return isTokenValid def decodeJWT(token: str) -> dict: try: decoded_token = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM]) return decoded_token if decoded_token["expiration"] >= time.time() else None except: return {}  Then I wanted to protect certain routes: from fastapi import APIRouter, Depends, HTTPException, Request, Header from exchange_api.auth.jwt_bearer import JWTBearer @auth_router.get("/protected", dependencies=[Depends(JWTBearer())], tags=["auth test"]) def get_user_data(): #my issue: I'd like to access the token and its payload return {}  I have no issues with my code so far. But I'd like to periodically refresh the token and I need its payload anyway to do things inside my functions. How can I do that? I don't want my users to get kicked out in the middle of their session because their token expired. It works fine without depends and using the token as the body of the various routes but I don't think that's the right way to do this. fastapi-jwt-auth is too old and it generates dependencies conflicts with my already installed libraries. EDIT: What I mean is that I want to access the bearer token that my users submitted to authenticate to do various things and also because I plan to substitute it with a new one every time the users call a protected route but WITHOUT the need to authenticate again. EDIT: I managed to get the token: @auth_router.get("/protected", dependencies=[Depends(JWTBearer())], tags=["auth test"]) def get_user_data(request: Request): token = request.headers["authorization"] return {token} 

Answer: I was going to suggest returning the token within your auth function, but then I saw you figured it out. I'd also recommend using FastAPI-Another-JWT-Auth. It speeds things up, declutters everything, and has most of the functions you might need built in. It's a fork of the old main one which is deprecated and no longer maintained. async def some_function(auth: AuthJWT = Depends()): auth.access_token_required()  If you need the token subject, you just do: user_id = auth.get_jwt_subject()  If you want to set a new jwt each time, you just do: new_access_token = auth.create_access_token() auth.set_access_token(new_access_token)