startup-fastapi
"/home/yossef/notes/full-stack/python-backend/fastapi/startup-fastapi.md"
path: full-stack/python-backend/fastapi/startup-fastapi.md
- **fileName**: startup-fastapi
- **Created on**: 2025-03-14 16:30:25
fastapi docs: fastapi
starting with installalition for fastapi
- creating a venv
python -m venv venv
- install fast api
pip install --upgrade pip
pip uninstall fastapi
pip install "fastapi[standard]"
- for run fastapi
fastapi dev main.py
# uvicorn main:app --reload
now with the main structure for code for main.py file
from fastapi import FastAPI
api = FastAPI()
@api.get("/") # specific end point
# implementation function for this endpoint
def home(): return {"message": "welome from yossef" }
@api.get("/about")
def index():
return "information about foo"
now with the todo path param and todo query param
from fastapi import FastAPI
api = FastAPI()
## creating a template for todos
todo_user = [
{"todo_id": 1, "study": "study 1 hour today"},
{"todo_id": 2, "code": "code for 1 hour"},
{"todo_id": 3, "mediate": "mediate for 20 min"},
{"todo_id": 4, "sleep": "sleep a 10 am"},
]
# now the best route (;
@api.get("/")
def home():
return {"message": "welome from yossef" }
## now starting with the path param
# passing the same todo_id from url to the same function
# if not passing to route any param it's gone go not found route
# if passing string int parsing problem
# example: localhost:8000/todo/2
@api.get("/todo/{todo_id}")
def get_todo(todo_id: int):
# check for todo with the same id
for todo in todo_user:
if todo["todo_id"] == todo_id:
return todo
return "not found todo"
## now starting with the query param
# query param it's send in url must know the name for query and passing to
# the function
# example: localhost:8000/todos?todo_number=2
@api.get("/todos")
def getTodo(todo_number: int = None):
if todo_number: ## check if found a todo_number
return todo_user[0:todo_number]
return todo_user
Important
most of param in path and query param his default type is string
now starting with post request destroctor from body param
## now creating the endpoint for update
@api.put('/todo/{todo_id}')
def update_todo(todo_id: int, new_todo: dict = Body(...)):
"""
Parameters
---
dict
the new todo content
Returns
---
err | dict
return errr if there is no todo | return todo if found and update todo
"""
if todo_id: # check if vaild id for todo
for todo in todo_user: # making for loop for all todo
if todo["todo_id"] == todo_id: # if todo == id
todo["todo_title"] = new_todo["todo_title"] # udpate title
# update description
todo["todo_description"] = new_todo["todo_description"]
new_todo["todo_id"] = todo_id
return new_todo # return update todo
return "error happend there is no todo" # if not found any todo
now creating the endpoint for update destractor from the body request
@api.put('/todo/{todo_id}')
def update_todo(todo_id: int, new_todo: dict = Body(...)):
"""
Parameters
---
dict
the new todo content
Returns
---
err | dict
return errr if there is no todo | return todo if found and update todo
"""
if todo_id: # check if vaild id for todo
for todo in todo_user: # making for loop for all todo
if todo["todo_id"] == todo_id: # if todo == id
todo["todo_title"] = new_todo["todo_title"] # udpate title
# update description
todo["todo_description"] = new_todo["todo_description"]
new_todo["todo_id"] = todo_id
return new_todo # return update todo
return "error happend there is no todo" # if not found any todo
now creating the endpoint for delete
@api.delete('/todo/{todo_id}')
def delete_todo(id: int):
"""
Parameters
---
int
the todo id that want to delete
Returns
---
err | dict
err if there is not todo with the id, else if found deleted
todo delete it and return it
"""
## first making for loop for all todo
for index,todo in enumerate(todo_user):
if todo["todo_id"] == id: # if found the id for the todo that want to delete
deleted_todo = todo_user.pop(index) # pop from todo list
return deleted_todo # return deleted todo
return "error happend there is no todo" # if not found any todo
Tip
you can use localhost:8000/docs for testing the endpont.
PUT: Full resource replacement, or creating if it doesn’t exist.
PATCH: Small updates to an existing resource (e.g., update email or status).
continue:./fastapi-validation-schemes.md