Project Setup
In this project, you will create a FastAPI application using uv for package management and a virtual environment for dependency isolation.Step 1: Create the Project Folder
Create a new project folder.Step 2: Create a Virtual Environment
Create a virtual environment using uv..venv folder.
Step 3: Activate the Virtual Environment
Windows (Command Prompt)Step 4: Install the Required Packages
- Installs FastAPI and Uvicorn
- Creates
pyproject.toml - Creates
uv.lock
Step 5: Create the Project Structure
Step 6: Create the Application File
Create a file named main.py.Step 7: Run the Application
--reload option automatically restarts the server whenever you save changes.
Step 8: Open the Application
Home:http://127.0.0.1:8000
Swagger UI: http://127.0.0.1:8000/docs
Build the Student CRUD API
Step 1: Create the FastAPI Application
Createmain.py and initialize the FastAPI application.
Show Code
Show Code
Step 2: Create the Student Model
Show Code
Show Code
Step 3: Create a Local Data Store
Show Code
Show Code
Step 4: Build the Create Student API
Endpoint:POST /students
Sample Request
Show Code
Show Code
Step 5: Build the Get All Students API
Endpoint:GET /students
Show Code
Show Code
Step 6: Build the Get Student by ID API
Endpoint:GET /students/{student_id}
Example: GET /students/1
Show Code
Show Code
Step 7: Build the Update Student API
Endpoint:PUT /students/{student_id}
Sample Request
Show Code
Show Code
Step 8: Build the Delete Student API
Endpoint:DELETE /students/{student_id}
Show Code
Show Code
Step 9: Test the Complete CRUD Flow
Open Swagger UI:http://127.0.0.1:8000/docs
Execute the APIs in this order:
GET /studentsPOST /studentsGET /studentsGET /students/{student_id}PUT /students/{student_id}GET /students/{student_id}DELETE /students/{student_id}