FastAPI는 파이썬 3.7 이상의 버전에서 API를 빌드하기위한 웹 프레임워크이다.
$ pip install fastapi
$ pip install "uvicorn[standard]"
from typing import Optional
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}
$ python main.py
$ uvicorn main:app --reload
http://localhost:8000
접속하여 확인
{"Hello": "World"}
가 출력되는 것을 확인할 수 있다.
http://localhost:8000/items/3?q=what?
로 접속해본 경우 위와 같이 나온다.
http://localhost:8000/docs
로 접속 시 다음과 같은 페이지를 확인할 수 있다.
Swagger를 사용한 대화형 API 문서로 여러 테스트 들이 가능하다.
http://localhost:8000/redoc
조금 더 깔끔한 디자인을 가지고 있으나, 테스트를 해보는 기능은 제공되지 않는다.