import os
import httpx
from fastapi import FastAPI, Header, HTTPException
app = FastAPI()
HILT_API_URL = os.getenv("HILT_API_URL", "https://api.hilt.so")
HILT_API_KEY = os.environ["HILT_API_KEY"]
PRODUCT = "pro-ai-api"
async def hilt_post(path: str, payload: dict):
async with httpx.AsyncClient(timeout=10) as client:
response = await client.post(
f"{HILT_API_URL}{path}",
headers={"X-Hilt-Key": HILT_API_KEY, "Content-Type": "application/json"},
json=payload,
)
response.raise_for_status()
return response.json()
@app.post("/ai/pro")
async def pro_endpoint(x_customer_id: str = Header(...)):
entitlement = await hilt_post(
"/v1/access/entitlements/check",
{
"external_product_id": PRODUCT,
"external_customer_id": x_customer_id,
"rail": "solana_usdc",
},
)
if entitlement.get("has_access"):
return {"result": "paid work can run"}
session = await hilt_post(
"/v1/access/payment-sessions",
{
"external_product_id": PRODUCT,
"external_customer_id": x_customer_id,
"rail": "solana_usdc",
"metadata": {"resource": "/ai/pro"},
},
)
payment_session = session.get("payment_session", {})
detail = {
"error": "payment_required",
"rail": session.get("rail", "solana_usdc"),
"payment_session": payment_session,
"checkout_url": payment_session.get("checkout_url"),
}
if session.get("payment_requirement"):
detail.update(
{
"payment_protocol": "x402",
"settlement_rail": "solana_usdc",
"payment_requirement": session["payment_requirement"],
}
)
raise HTTPException(status_code=402, detail=detail)