diff --git a/backend/app/api/routes/threads.py b/backend/app/api/routes/threads.py index 8a49ef3c..1360ec36 100644 --- a/backend/app/api/routes/threads.py +++ b/backend/app/api/routes/threads.py @@ -11,6 +11,7 @@ from app.core import logging, settings from app.models import UserOrganization from app.utils import APIResponse +from app.crud import get_key_by_org logger = logging.getLogger(__name__) router = APIRouter(tags=["threads"]) @@ -167,7 +168,15 @@ async def threads( _current_user: UserOrganization = Depends(get_current_user_org), ): """Asynchronous endpoint that processes requests in background.""" - client = OpenAI(api_key=settings.OPENAI_API_KEY) + api_key = get_key_by_org(session=_session, org_id=_current_user.organization_id) + + if not api_key: + return APIResponse.failure_response( + error="API key not configured for this organization." + ) + + client = OpenAI(api_key=api_key) + langfuse_context.configure( secret_key=settings.LANGFUSE_SECRET_KEY, public_key=settings.LANGFUSE_PUBLIC_KEY, @@ -206,7 +215,14 @@ async def threads_sync( _current_user: UserOrganization = Depends(get_current_user_org), ): """Synchronous endpoint that processes requests immediately.""" - client = OpenAI(api_key=settings.OPENAI_API_KEY) + api_key = get_key_by_org(session=_session, org_id=_current_user.organization_id) + + if not api_key: + return APIResponse.failure_response( + error="API key not configured for this organization." + ) + + client = OpenAI(api_key=api_key) # Validate thread is_valid, error_message = validate_thread(client, request.get("thread_id")) diff --git a/backend/app/crud/__init__.py b/backend/app/crud/__init__.py index a58c5f27..8a5764b4 100644 --- a/backend/app/crud/__init__.py +++ b/backend/app/crud/__init__.py @@ -29,3 +29,11 @@ get_api_keys_by_organization, delete_api_key, ) + +from .credentials import ( + set_creds_for_org, + get_creds_by_org, + get_key_by_org, + update_creds_for_org, + remove_creds_for_org, +)