Guides
APIFuse에서 생성된 Pydantic 아티팩트를 서버 사이드 FastAPI 코드에서 사용하는 방법입니다.
개발자 MCP로 operation 계약과 생성된 Pydantic 아티팩트를 가져온 뒤, FastAPI 서버에서 APIFuse를 호출합니다. APIFuse 자격 증명은 서버 설정에 보관하고, 요청과 응답 페이로드 모두 유효성을 검사하세요.
코딩 에이전트에게 apifuse_dev_get_schema_bundle의 pydantic 대상을 요청하고, 생성된 모델을 서버 사이드 패키지에 배치합니다. 예: app/apifuse_generated/weather.py
# app/routes/weather.py
import os
import httpx
from fastapi import APIRouter, Response
from app.apifuse_generated.weather import (
ShortForecastInput,
ShortForecastOutput,
)
router = APIRouter()
@router.post("/weather", response_model=ShortForecastOutput)
async def weather(input_model: ShortForecastInput) -> ShortForecastOutput | Response:
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:
# 모든 실패를 하나의 상태 코드로 뭉개지 말고 APIFuse의 에러 판정을
# (상태와 body 바이트 그대로) 전달하세요. HTTPException은 body를
# {"detail": ...} 아래로 감싸 code / retryable / source 필드를 가립니다.
# 자세한 내용은 에러 처리 가이드를 참고하세요.
return Response(
content=response.content,
status_code=response.status_code,
# 업스트림 Content-Type과 무관하게 JSON으로 응답해 예기치 않은
# non-JSON body가 same-origin HTML로 렌더링되지 않게 하고,
# 가이드가 의존하는 재시도·상관관계 헤더는 유지합니다.
media_type="application/json",
headers={
key: value
for key, value in {
"Retry-After": response.headers.get("retry-after"),
"X-Request-Id": response.headers.get("x-request-id"),
}.items()
if value is not None
},
)
return ShortForecastOutput.model_validate(response.json())bundleHash와 targetArtifactHash를 모니터링하여, 스키마 변경 시 생성된 모델을 검토하세요.개발자 MCP의 apifuse_dev_* 도구는 FastAPI용 스키마, 경고, 연동 스니펫을 제공합니다. provider operation 실행은 위 예시처럼 서버 사이드 게이트웨이 호출로 수행합니다.