FastAPI 연동
APIFuse에서 생성된 Pydantic 아티팩트를 서버 사이드 FastAPI 코드에서 사용하는 방법입니다.
FastAPI 연동
개발자 MCP로 operation 계약과 생성된 Pydantic 아티팩트를 가져온 뒤, FastAPI 서버에서 APIFuse를 호출합니다. APIFuse 자격 증명은 서버 설정에 보관하고, 요청과 응답 페이로드 모두 유효성을 검사하세요.
1. Pydantic 모델 생성 또는 복사
코딩 에이전트에게 apifuse_dev_get_schema_bundle의 pydantic 대상을 요청하고, 생성된 모델을 서버 사이드 패키지에 배치합니다. 예: app/apifuse_generated/weather.py
2. API 라우트에서 APIFuse 호출
# app/routes/weather.py
import os
import httpx
from fastapi import APIRouter, HTTPException
from app.apifuse_generated.weather import (
ShortForecastInput,
ShortForecastOutput,
)
router = APIRouter()
@router.post("/weather", response_model=ShortForecastOutput)
async def weather(input_model: ShortForecastInput) -> ShortForecastOutput:
api_key = os.environ["APIFUSE_API_KEY"]
async with httpx.AsyncClient(timeout=15.0) as client:
response = await client.post(
"https://api.apifuse.com/v1/korea-weather/short-forecast",
headers={"Authorization": f"Bearer {api_key}"},
json=input_model.model_dump(mode="json"),
)
if response.status_code >= 400:
raise HTTPException(status_code=502, detail="apifuse_request_failed")
return ShortForecastOutput.model_validate(response.json())3. 운영 지침
- APIFuse API 키는 서버 사이드 환경 변수 또는 시크릿 관리 시스템에 보관하세요.
- 생성된 Pydantic 모델로 발신 입력과 수신 출력의 유효성을 검사하세요.
- 브라우저와 모바일 클라이언트에는 애플리케이션 고유의 응답 형태를 반환하세요.
- provider 자격 증명, 조직 인증 정보, connection 식별자를 클라이언트에 노출하지 마세요.
- CI에서
bundleHash와targetArtifactHash를 모니터링하여, 스키마 변경 시 생성된 모델을 검토하세요.
개발자 MCP 경계
개발자 MCP의 apifuse_dev_* 도구는 FastAPI용 스키마, 경고, 연동 스니펫을 제공합니다. provider operation 실행은 위 예시처럼 서버 사이드 게이트웨이 호출로 수행합니다.