Back to list

FastAPI CRUD Operations

Lv.5496@mukitaro12 playsDec 28, 2025

FastAPI with Pydantic models. Modern async Python web framework.

preview.python
Python
1from fastapi import FastAPI, HTTPException
2from pydantic import BaseModel
3from typing import Optional
4
5app = FastAPI()
6
7class Item(BaseModel):
8 name: str
9 price: float
10 is_offer: Optional[bool] = None
11
12items = {}
13
14@app.post('/items/{item_id}')
15def create_item(item_id: int, item: Item):
16 items[item_id] = item
17 return item
18
19@app.get('/items/{item_id}')
20def read_item(item_id: int):
21 if item_id not in items:
22 raise HTTPException(status_code=404)
23 return items[item_id]

Custom problems are not included in rankings