Mode:
Duration:
1
Coding works best on desktop or with an external keyboard.
Coding works best on desktop or with an external keyboard.
Basic CRUD for items using FastAPI and in-memory storage.
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
items = {}
@app.post('/items/{item_id}')
async def create_item(item_id: int, item: Item):
items[item_id] = item
return item
@app.get('/items/{item_id}')
async def read_item(item_id: int):
if item_id not in items:
raise HTTPException(status_code=404, detail='Item not found')
return items[item_id]
@app.put('/items/{item_id}')
async def update_item(item_id: int, item: Item):
items[item_id] = item
return item
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
return items.pop(item_id, None)FastAPI is a modern, high-performance Python web framework for building APIs with automatic interactive documentation. It emphasizes speed, type hints, and ease of development, leveraging Python 3.7+ features.
Origin & Creator
FastAPI was created by Sebastián Ramírez in 2018 to provide a fast and developer-friendly Python API framework.
Industrial Note
FastAPI is ideal for projects requiring high-performance async APIs, automatic data validation, and rapid development cycles in Python.