FAST API 활용한 사내업무 Agent만들기

최윤·2025년 3월 19일

AI

목록 보기
1/2

1. FAST API의 주요 기능

1. API 엔드포인트 정의

FastAPI에서는 @app.get(), @app.post() 같은 데코레이터를 사용해 API 엔드포인트를 정의할 수 있다.

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, FastAPI!"}

@app.get("/") → GET 요청을 받는 엔드포인트
return {} → JSON 형태의 응답 반환

2. 주요 기능 및 함수

🔹 1) HTTP 메서드 (GET, POST, PUT, DELETE 등)

FastAPI는 RESTful API를 만들기 위해 여러 HTTP 메서드를 지원한다.

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")  # GET 요청
def read_item(item_id: int):
    return {"item_id": item_id}

@app.post("/items/")  # POST 요청
def create_item(item: dict):
    return {"message": "Item created", "item": item}

@app.put("/items/{item_id}")  # PUT 요청
def update_item(item_id: int, item: dict):
    return {"message": "Item updated", "item_id": item_id, "item": item}

@app.delete("/items/{item_id}")  # DELETE 요청
def delete_item(item_id: int):
    return {"message": f"Item {item_id} deleted"}

📌 주요 함수

@app.get("/url") → GET 요청을 처리
@app.post("/url") → POST 요청을 처리
@app.put("/url") → PUT 요청을 처리
@app.delete("/url") → DELETE 요청을 처리

🔹 2) 경로 매개변수 (Path Parameters)

URL의 일부를 변수로 받을 수 있다.

@app.get("/users/{user_id}")
def get_user(user_id: int):
    return {"user_id": user_id}

user_id 값을 URL에서 가져와 처리

0개의 댓글