File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ import logging
2
+ from fastapi import APIRouter , HTTPException
3
+ from pydantic import BaseModel
4
+ from utils .create_assistant import update_env_file
5
+
6
+ # Configure logger
7
+ logger : logging .Logger = logging .getLogger ("uvicorn.error" )
8
+
9
+ router = APIRouter (prefix = "/api-keys" , tags = ["API Keys" ])
10
+
11
+ class APIKeyRequest (BaseModel ):
12
+ api_key : str
13
+
14
+ @router .post ("" )
15
+ async def set_openai_api_key (request : APIKeyRequest ):
16
+ """
17
+ Set the OpenAI API key in the application's environment variables.
18
+
19
+ Args:
20
+ request: APIKeyRequest containing the API key
21
+
22
+ Returns:
23
+ dict: Success message
24
+
25
+ Raises:
26
+ HTTPException: If there's an error updating the environment file
27
+ """
28
+ try :
29
+ update_env_file ("OPENAI_API_KEY" , request .api_key , logger )
30
+ return {"message" : "API key updated successfully" }
31
+ except Exception as e :
32
+ raise HTTPException (
33
+ status_code = 500 ,
34
+ detail = f"Failed to update API key: { str (e )} "
35
+ )
You can’t perform that action at this time.
0 commit comments