<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sakshi Agarwal</title>
    <description>The latest articles on DEV Community by Sakshi Agarwal (@sakshi_agarwal_4c639a581c).</description>
    <link>https://dev.to/sakshi_agarwal_4c639a581c</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2143123%2F80c0778e-7fda-4f51-810b-83690e0a317a.jpg</url>
      <title>DEV Community: Sakshi Agarwal</title>
      <link>https://dev.to/sakshi_agarwal_4c639a581c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sakshi_agarwal_4c639a581c"/>
    <language>en</language>
    <item>
      <title>Creating a Basic FastAPI Application with CRUD Operations</title>
      <dc:creator>Sakshi Agarwal</dc:creator>
      <pubDate>Thu, 13 Nov 2025 12:13:55 +0000</pubDate>
      <link>https://dev.to/sakshi_agarwal_4c639a581c/creating-a-basic-fastapi-application-with-crud-operations-278o</link>
      <guid>https://dev.to/sakshi_agarwal_4c639a581c/creating-a-basic-fastapi-application-with-crud-operations-278o</guid>
      <description>&lt;p&gt;FastAPI is one of the fastest-growing Python frameworks for building modern APIs. It is fast, easy to use, and comes with built-in data validation using Pydantic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚀 What We Will Build&lt;/strong&gt;&lt;br&gt;
A simple API that manages Items with the following fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;id&lt;/li&gt;
&lt;li&gt;name&lt;/li&gt;
&lt;li&gt;description&lt;/li&gt;
&lt;li&gt;price&lt;/li&gt;
&lt;li&gt;in_stock&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;We will implement:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an item&lt;/li&gt;
&lt;li&gt;Read all items / single item&lt;/li&gt;
&lt;li&gt;Update an item&lt;/li&gt;
&lt;li&gt;Delete an item&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;📦 Project Setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensure you have Python 3.8+ installed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install FastAPI &amp;amp; Uvicorn&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install fastapi uvicorn

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🗂️ Project Structure&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fastapi-crud/
│
├── main.py
└── models.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🧱 Step 1: Create the Pydantic Model (models.py)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pydantic import BaseModel


class Item(BaseModel):
    id: int
    name: str
    description: str | None = None
    price: float
    in_stock: bool = True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;⚙️ Step 2: Build FastAPI CRUD Endpoints (main.py)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI, HTTPException
from models import Item


app = FastAPI()


# Temporary in-memory database
items_db = {}


# Create an Item
@app.post("/items", status_code=201)
def create_item(item: Item):
    if item.id in items_db:
        raise HTTPException(status_code=400, detail="Item already exists")
        items_db[item.id] = item
    return {"message": "Item created successfully", "item": item}


# Read All Items
@app.get("/items")
def get_items():
    return list(items_db.values())


# Read Single Item
@app.get("/items/{item_id}")
def get_item(item_id: int):
    if item_id not in items_db:
        raise HTTPException(status_code=404, detail="Item not found")
    return items_db[item_id]


# Update Item
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    if item_id not in items_db:
        raise HTTPException(status_code=404, detail="Item not found")
    items_db[item_id] = item
    return {"message": "Item updated", "item": item}


# Delete Item
@app.delete("/items/{item_id}")
def delete_item(item_id: int):
    if item_id not in items_db:
        raise HTTPException(status_code=404, detail="Item not found")
    del items_db[item_id]
    return {"message": "Item deleted"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;▶️ Step 3: Run the Application&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uvicorn main:app --reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;API will be available at:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://127.0.0.1:8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🧪 Step 4: Test Using Swagger UI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;FastAPI automatically provides beautiful interactive API docs.&lt;/p&gt;

&lt;p&gt;Open:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://127.0.0.1:8000/docs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can test all CRUD operations directly there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🎯 Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We learned how to build a clean FastAPI application with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pydantic models&lt;/li&gt;
&lt;li&gt;CRUD Routes&lt;/li&gt;
&lt;li&gt;In-memory storage&lt;/li&gt;
&lt;li&gt;Automatic interactive API documentatio
n&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>api</category>
      <category>backend</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
