Lets build our first api’s
Lets Build apis for inventory
- Identify nouns and verbs
- Product
- Get
- update
- delete
- Create
- Sale(Order)
- Create
- Get
- Delete
- Procurement
- Create
- Get
- Delete
- Vendor
- Get
- update
- delete
- Create
- Customer
- Get
- update
- delete
- Create
- Employee
- Get
- update
- delete
- Create
- Product
To build this lets use Fastapi
- Create a folder called as hello-api
- lets add dependencies Refer Here
uv init .
uv add fastapi[standard]
- Now create an application object with simple code
from fastapi import FastAPI
# create an application object
app = FastAPI()
@app.get("/")
def home():
return "Welcome to my API"
- From the terminal run
uv run fastapi dev main.py -
To debug watch classroom video.
-
Basic code
from fastapi import FastAPI
# create an application object
app = FastAPI()
@app.get("/")
def home():
return "Welcome to my API"
@app.get("/products")
def get_products():
return [
{"id": 1, "name": "Laptop"},
{"id": 2, "name": "Mouse"}
]
@app.post("/products")
def add_product(id:int, name: str):
return {"id": id, "name": name}
@app.put("/products/{id}")
def update_product(id:int, name:str):
return {"id": id, "name": name}
@app.get("/customers")
def get_customers():
return [
{"id": 1, "name": "John"},
{"id": 2, "name": "Jane"}
]
@app.delete("/products/{id}")
def delete_product(id: int):
raise NotImplementedError()
- Ideal status codes: Use llm
-
Define models and have some validations and different ways of sending data
-
Exercise:
- Try creating the same api with title inventory and version 1.0.0.0
- For all create methods change default status code to 201 and for all delete change the status code to 204
- Try build a client for this api using requests.
