Skip to main content

Capstone Project - Student Management REST API

Problem Statement In this capstone project, you will build a Student Management System that exposes a set of RESTful APIs to manage student records. The application should store student information in a SQLite database using SQLAlchemy ORM and follow a clean, modular architecture. Throughout this project, you will implement the application layer by layer, separating database operations, business logic, and API endpoints. The application should support the following features:
  • Add a Student
  • View All Students
  • Search Students using query parameters
  • View Student Details
  • Update Student Details
  • Delete a Student
By the end of this project, you will have a complete backend application that demonstrates industry-standard practices for building REST APIs using FastAPI.

Learning Objectives

After completing this project, you will be able to:
  • Build RESTful APIs using FastAPI.
  • Organize applications using a modular project structure.
  • Configure and use SQLite with SQLAlchemy ORM.
  • Design database models using SQLAlchemy.
  • Validate requests and responses using Pydantic Schemas.
  • Separate database operations using the Repository Pattern.
  • Implement business logic using the Service Layer.
  • Handle application errors using global exception handlers.
  • Perform CRUD operations on a persistent database.
  • Search records using query parameters.
  • Test REST APIs using Swagger UI.

Final Project Architecture


Request Flow


Development Approach

Rather than building the entire application at once, we will develop it incrementally. At the end of each step, we will run and verify the implementation before moving on to the next step. This approach makes debugging easier and helps us understand the purpose of every layer in the application. Each step consists of:
  • Objective
  • Instructions
  • Task
  • Solution
  • Run & Verify

Step 1: Create the Project and Git Repository

Objective Create a new FastAPI project directory, initialize Git, configure .gitignore, and prepare the development environment. Instructions
  • Create a new project directory named student-management-api.
  • Navigate to the project directory.
  • Initialize the project environment using uv.
  • Initialize Git and set up .gitignore to avoid tracking virtual environments or databases.
Task Create the project directory, initialize using uv, initialize Git, configure .gitignore, and commit.
Run & Verify Verify that:
  • The project directory has been created.
  • The virtual environment is activated.
  • A pyproject.toml and .gitignore files are present.
  • git status reports a clean working tree after the first commit.

Step 2: Install Dependencies

Objective Install all the libraries required to build the Student Management REST API. Instructions Install the following dependencies:
  • FastAPI
  • Uvicorn
  • SQLAlchemy
  • Email Validator
Task Install the required dependencies using uv and commit.
Run & Verify Verify that:
  • All packages are installed successfully.
  • The pyproject.toml file contains the installed dependencies.

Step 3: Create the Project Structure

Objective Organize the application package directories to separate different layers. Instructions
  • Create the application package directory app.
  • Create folders for core, models, schemas, repositories, services, and routers under app.
  • Place empty __init__.py files inside each folder.
Task Create the project structure and commit the new folders.
Project Structure
Commit the project structure to Git:
Run & Verify Verify that the directories are present and contain initial package files.

Step 4: Configure the Database

Objective Configure SQLAlchemy so that the application can communicate with the SQLite database. Instructions Create a database.py file inside the core folder and implement:
  • Database URL
  • SQLAlchemy Engine
  • Session Factory
  • Declarative Base
  • Database Dependency (get_db())
Task Configure SQLAlchemy for the SQLite database and commit.
File: app/core/database.py
Commit the changes to Git:
Run & Verify Verify that:
  • The project runs without import errors.
  • The database.py file has no syntax errors.

Step 5: Design the Database Schema

Objective Design the database schema required for the Student Management System before implementing the database models. Instructions Design a database table named students with the following columns:
  • Student ID
  • Student Name
  • Student Age
  • Student Email Address
Identify:
  • Primary Key
  • Data Types
  • Required Fields
  • Unique Constraints
Task Design the database schema for the Student Management System.
Students TableDatabase Schema

Step 6: Create the Student ORM Model

Objective Create the SQLAlchemy ORM model that maps the students table to a Python class. Instructions Create a Student model in app/models/student.py that:
  • Maps to the students table.
  • Defines the required columns using SQLAlchemy 2.0 Mapped and mapped_column.
  • Inherits from the application’s Base class.
Task Create the Student ORM model and commit.
File: app/models/student.py
Commit the model:
Run & Verify Create the database tables by temporarily adding the following code to main.py:
Run the application:
Verify that:
  • A new file named students.db is created.
  • The students table exists in the database.

Step 7: Implement the Health Check Endpoint

Objective Implement a Health Check endpoint to verify that the application can receive and respond to HTTP requests. Instructions
  • Open the main.py file.
  • Implement a Health Check endpoint.
  • Verify the endpoint using Swagger UI.
Task Implement a Health Check endpoint and commit.
File: app/main.py
Commit the entrypoint setup:
Run & Verify Open the following URL:
Expected Response:

Step 8: Create the Pydantic Schemas

Objective Create Pydantic schemas to validate incoming requests and serialize outgoing responses. Instructions Create a student.py file inside the schemas folder and implement:
  • StudentCreate
  • StudentUpdate
  • StudentResponse
Apply the following validation rules:
  • Name: Required, min 2 chars, max 50 chars.
  • Age: Required, between 1 and 120.
  • Email: Required, valid email format.
Task Define Pydantic schemas and commit.
File: app/schemas/student.py
Commit the schemas:
Run & Verify Verify that the validation schemas compile without errors.

Step 9: Implement the Repository Layer

Objective Abstract direct database operations away from business logic using the Repository Pattern. Instructions Create app/repositories/student_repository.py and implement operations:
  • Retrieve all students.
  • Search students using query parameters.
  • Retrieve a student by ID/Email.
  • Create, update, and delete student records.
Task Implement StudentRepository and commit.
File: app/repositories/student_repository.py
Commit the repository file:
Run & Verify Verify that all Repository queries match standard SQLAlchemy syntax.

Step 10: Configure Global Exception Handling

Objective Define custom exception classes and globally catch errors inside the FastAPI application. Instructions
  • Create app/core/exceptions.py.
  • Create app/core/exception_handlers.py.
  • Define AppException, StudentNotFoundError, and register global handler functions.
Task Implement custom exceptions, global handlers, and commit.
File: app/core/exceptions.py
File: app/core/exception_handlers.py
Commit exception modules:
Run & Verify Verify that custom exceptions extend the base AppException.

Step 11: Implement the Service Layer

Objective Coordinate business operations and enforce domain validation rules inside the Service Layer. Instructions
  • Create app/services/student_service.py.
  • Implement registration, retrieval, updating, and deletion operations.
  • Throw custom exceptions like AppException when an email already exists.
Task Implement StudentService and commit.
File: app/services/student_service.py
Commit the service file:
Run & Verify Verify that services cleanly intercept logical failures and raise appropriate exception types.

Step 12: Implement the REST API using APIRouter

Objective Expose operations to users through API Routers using Annotated dependency injection. Instructions
  • Create app/routers/student.py.
  • Map endpoints for creating, reading, updating, and deleting students.
  • Inject database sessions cleanly using Annotated types.
Task Implement student API endpoints and commit.
File: app/routers/student.py
Commit the router:
Run & Verify Verify that router path endpoints align correctly with client specifications.

Step 13: Integrate and Test the Student Management REST API

Objective Integrate your routers and global exception handlers in the main entrypoint and run testing verifications. Instructions
  • Update app/main.py.
  • Include routes, registers exception handlers, and start your uvicorn development server.
Task Assemble main.py with endpoints router and verify application features.
File: app/main.py
Commit the final main configuration:
Run & Verify Run the application:
Navigate to http://127.0.0.1:8000/docs to test registration, queries, and deletions.