diff --git a/src/backend/langflow/api/v1/endpoints.py b/src/backend/langflow/api/v1/endpoints.py index 423217015..49426aeb7 100644 --- a/src/backend/langflow/api/v1/endpoints.py +++ b/src/backend/langflow/api/v1/endpoints.py @@ -100,6 +100,11 @@ async def process_flow( """ try: + if api_key is None: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid API Key", + ) api_key_user = api_key.user # Get the flow that matches the flow_id and belongs to the user flow = ( diff --git a/src/backend/langflow/services/database/models/api_key/crud.py b/src/backend/langflow/services/database/models/api_key/crud.py index adf53d382..abc84f108 100644 --- a/src/backend/langflow/services/database/models/api_key/crud.py +++ b/src/backend/langflow/services/database/models/api_key/crud.py @@ -50,17 +50,15 @@ def check_key(session: Session, api_key: str) -> Optional[ApiKey]: """Check if the API key is valid.""" query = select(ApiKey).where(ApiKey.api_key == api_key) api_key_object: Optional[ApiKey] = session.exec(query).first() - if api_key_object is None: - return api_key_object - - threading.Thread( - target=update_total_uses, - args=( - session, - api_key_object, - ), - ).start() - return api_key_object.user + if api_key_object is not None: + threading.Thread( + target=update_total_uses, + args=( + session, + api_key_object, + ), + ).start() + return api_key_object def update_total_uses(session, api_key: ApiKey):