TypeGPTProxy / main.py
AIMaster7's picture
Create main.py
08a3216 verified
raw
history blame
1.04 kB
from fastapi import FastAPI, Request, Header, HTTPException
import httpx
app = FastAPI()
# Replace with your actual backend API key and base URL
API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
BASE_URL = "https://fast.typegpt.net"
# Custom public header
PUBLIC_API_KEY = "TypeGPT-Free4ALL"
@app.api_route("/{path:path}", methods=["GET", "POST"])
async def proxy(request: Request, path: str, x_api_key: str = Header(None)):
if x_api_key != PUBLIC_API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
# Reconstruct full URL to target
target_url = f"{BASE_URL}/{path}"
# Prepare headers and body
headers = dict(request.headers)
headers["Authorization"] = f"Bearer {API_KEY}"
headers.pop("host", None)
body = await request.body()
async with httpx.AsyncClient() as client:
response = await client.request(
request.method,
target_url,
content=body,
headers=headers
)
return response.json()