Skip to content

Commit 3321226

Browse files
New API endpoint that allows adding an OpenAI API key to .env
1 parent 0fa1fc8 commit 3321226

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

routers/api-keys.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
)

0 commit comments

Comments
 (0)