Habify BaseHabify Base
SDK & Công cụ AI

Python & FastAPI SDK (habify-sdk)

Sử dụng AsyncHabifyClient, HabifyClient, FastAPI dependencies (get_habify_user, require_habify_roles, require_habify_webhook) và xác thực JWKS cached 10 phút.

Cập nhật lần cuối: 2026-11-24

1. Cài đặt & Tích hợp FastAPI

Gói habify-sdk hỗ trợ đầy đủ cả đồng bộ (HabifyClient) lẫn bất đồng bộ (AsyncHabifyClient trên httpx), đi kèm cache JWKS 600 giây và dependency bảo vệ route FastAPI.

py
from fastapi import Depends, FastAPI
from habify_sdk import AsyncHabifyClient, HabifyUser, HabifyWebhookEvent
from habify_sdk.fastapi import (
    configure_habify,
    get_habify_user,
    require_habify_webhook,
)

app = FastAPI()
configure_habify(
    app,
    jwks_url="https://auth.habifyhub.com/api/v1/oauth/jwks",
    issuer="https://auth.habifyhub.com",
    audience="my-project-client-id",
    webhook_secret="whsec_live_current",
    webhook_previous_secret="whsec_live_prev_24h",
)

habify = AsyncHabifyClient(
    base_url="https://my-app.habifybase.app",
    api_key="hb_sk_live_...",
)

@app.get("/orders")
async def list_orders(user: HabifyUser = Depends(get_habify_user)):
    return await (
        habify.db.table("orders")
        .with_user_token(user.raw_token or "")
        .eq("user_id", user.sub)
        .limit(25)
        .execute()
    )