Welcome to the second module of the FastAPI tutorial! This module provides an introduction to query and path parameters, as well as pydantic models and data validation.
- FastAPI project structure
- Creating GET API endpoints
- Path and query parameters
- Pydantic models
- Creating POST API endpoints
- Running and testing your API
- Basic knowledge of Python
- Python 3.7 or higher installed
-
Create a virtual environment:
python -m venv .venv
Activate the virtual environment:
- On macOS/Linux:
source .venv/bin/activateOn Windows use
.venv\Scripts\activate -
Install FastAPI and Uvicorn:
pip install fastapi uvicorn
-
Create a simple FastAPI app:
# main.py from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"}
-
Run the application:
uvicorn main:app --reload
-
Test your API:
- Open your browser and go to http://127.0.0.1:8000
- Explore the interactive docs at http://127.0.0.1:8000/docs
-
Objective: Create a data model for storing item information using Pydantic.
-
Instructions:
- Define a class Item that includes:
name: a string price: a float in_stock: a boolean (default: True)- Ensure that incoming JSON payloads will be validated against this model.
- Objective: Initialize an in-memory list of items for testing.
- Instructions:
- Create 10 sample items using the Item model. Each item should have:
- A unique
namelike "Sample Item 1", "Sample Item 2", etc. - A
priceincreasing by 10.0 per item (e.g., 10.0, 20.0, …) in_stockshould be True by default.
- A unique
- Store these in a list called items.
- Create 10 sample items using the Item model. Each item should have:
- Objective: Implement a POST
/items/endpoint. - Instructions:
- Accept an Item from the request body.
- Append the item to the items list.
- Return a JSON response confirming the item's name and price.
- Objective: Implement a GET /items/ endpoint that supports pagination.
- Instructions:
- Accept query parameters:
skip(default: 0),limit(default: 10) - Return a slice of the items list from
skiptoskip + limit.
- Accept query parameters:
- Bonus:
- Add optional filters (e.g.,
in_stock=true)
- Add optional filters (e.g.,
- Objective: Create a GET
/items/{item_id}endpoint. - Instructions:
- Use a path parameter
item_idto look up an item by its index. - If the
item_idis invalid (negative or out of bounds), return an error. - Otherwise, return the corresponding Item from the list.
- Use a path parameter
-
Objective: Create a GET
/logendpoint that records an informational log message whenever it is accessed. -
Instructions:
- Configure Python’s built-in logging module with a timestamped format.
- Use a logger instance to log a message like log endpoint was called at the INFO level.
- When the endpoint is hit, return a JSON response confirming the log action (e.g., {"Message": "Endpoint with standard logging"}).
-
Pydantic model class
Itemfor the API:from pydantic import BaseModel class Item(BaseModel): name: str price: float in_stock: bool = True
-
Storage for items:
items: list = list(Item(name="Sample Item " + str(i+1), price=(i+1) * 10.0, in_stock=True) for i in range(10))
-
Endpoints for creating and retrieving items:
-GET -- /items: returns a list of items -POST -- /items: creates a new item -GET -- /items/{item_id}: returns a specific item by ID# GET request examples ## Path parameter example @app.get("/items/{item_id}") def read_item(item_id: int): if item_id < 0 or item_id >= len(items): return {"error": "Item not found"} return items[item_id] ## Query parameters example @app.get("/items") def read_item(skip:int = 0, limit:int = 10): return items[skip: skip + limit] # POST request example @app.post("/items") def create_item(item: Item): items.append(item) return {"item_name": item.name, "price": item.price}