0
0
FastAPIframework~10 mins

CRUD operations in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CRUD operations
Receive HTTP Request
Identify Operation: Create, Read, Update, Delete
Process Data with Database
Return HTTP Response with Result
End
This flow shows how FastAPI handles CRUD operations by receiving a request, identifying the operation type, processing data, and returning a response.
Execution Sample
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

items = {}

class Item(BaseModel):
    name: str

@app.post('/items/')
async def create_item(id: int, item: Item):
    items[id] = item.name
    return items[id]

@app.get('/items/{id}')
async def read_item(id: int):
    return items.get(id, "Item not found")

@app.put('/items/{id}')
async def update_item(id: int, item: Item):
    items[id] = item.name
    return items[id]

@app.delete('/items/{id}')
async def delete_item(id: int):
    if id in items:
        del items[id]
    return "Item deleted"
This code implements basic CRUD operations (Create, Read, Update, Delete) using an in-memory dictionary to store items.
Execution Table
StepHTTP MethodEndpointInput DataActionData StateResponse
1POST/items/{id: 1, name: 'Book'}Create item with id=1{1: 'Book'}'Book'
2GET/items/1id=1Read item with id=1{1: 'Book'}'Book'
3PUT/items/1{name: 'Notebook'}Update item id=1{1: 'Notebook'}'Notebook'
4DELETE/items/1id=1Delete item with id=1{}Item deleted
5GET/items/1id=1Read item with id=1{}Item not found
💡 After deletion, item with id=1 no longer exists, so read returns 'Item not found'.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
items{}{1: 'Book'}{1: 'Book'}{1: 'Notebook'}{}{}
Key Moments - 3 Insights
Why does the GET request after deletion return 'Item not found'?
Because in step 4 the item with id=1 was deleted from the data store, so it no longer exists when step 5 tries to read it.
How does the PUT request update the item?
In step 3, the PUT request changes the value of items[1] from 'Book' to 'Notebook', updating the stored data.
What happens if we try to create an item with an existing id?
The new value will overwrite the old one in the dictionary, as shown in step 3 where the item is updated.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of 'items' after step 3?
A{}
B{1: 'Book'}
C{1: 'Notebook'}
D{1: 'Item deleted'}
💡 Hint
Check the 'Data State' column in row for step 3.
At which step does the item with id=1 get removed from the data?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the DELETE method in the 'HTTP Method' column.
If we send a POST request with id=1 and name='Pen' after step 4, what will be the new state of 'items'?
A{1: 'Notebook'}
B{1: 'Pen'}
C{}
D{1: 'Book'}
💡 Hint
POST creates or overwrites items; check how 'items' changes in step 1.
Concept Snapshot
CRUD operations in FastAPI:
- Create: POST /items/ with id and name to add item
- Read: GET /items/{id} to fetch item
- Update: PUT /items/{id} with new data
- Delete: DELETE /items/{id} to remove item
Data stored in a dictionary for simplicity.
Full Transcript
This visual execution shows how FastAPI handles CRUD operations using a simple dictionary as data storage. Each step corresponds to an HTTP request: POST creates an item, GET reads it, PUT updates it, and DELETE removes it. The execution table tracks the data state and responses after each operation. Beginners often wonder why reading after deletion fails or how updates overwrite data. The variable tracker shows how the 'items' dictionary changes over time. This step-by-step trace helps understand the flow of CRUD in FastAPI.