FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
POST /items: Adding New Data to Your API with FastAPI & Pydantic
How-ToProgramming Languages

POST /items: Adding New Data to Your API with FastAPI & Pydantic

via Dev.to PythonFiyinfoluwa Ojo1mo ago

From Read-Only to Interactive Until now our API could only read data. Today we add the ability to CREATE : the C in CRUD. The Request & Response DTOs Request : what the client sends class ItemCreateDTO(BaseModel): name: str description: Optional[str] = None price: float @field_validator("price") def price_must_be_positive(cls, v): if v <= 0: raise ValueError("Price must be a positive number") return v Response : what the API returns class ItemResponseDTO(BaseModel): id: int name: str description: Optional[str] price: float created_at: datetime Two separate DTOs : one for input, one for output. That's clean API design. The POST Endpoint @app.post("/items", response_model=ItemResponseDTO, status_code=201) def create_item(item: ItemCreateDTO): db = SessionLocal() new_item = Item( name=item.name, description=item.description, price=item.price ) db.add(new_item) db.commit() db.refresh(new_item) db.close() return new_item Notice status_code=201 : that's the correct HTTP status for a successf

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
24 views

Related Articles

How-To

The Difference between `let`, `var` and `const`

Medium Programming • 2d ago

How-To

Circulation Metrics Framework for Living Systems

Medium Programming • 2d ago

Red Rooms makes online poker as thrilling as its serial killer
How-To

Red Rooms makes online poker as thrilling as its serial killer

The Verge • 2d ago

Don’t Know What Project to Build? Here Are Developer Projects That Actually Make You Better
How-To

Don’t Know What Project to Build? Here Are Developer Projects That Actually Make You Better

Medium Programming • 2d ago

Why Most Developers
Stay Broke
How-To

Why Most Developers Stay Broke

Medium Programming • 3d ago

Discover More Articles