Skip to main content

Student Management REST API

Objective In this module, you will build a complete Student Management REST API using FastAPI without a database. By the end of this module, you will be able to:
  • Build REST APIs using FastAPI.
  • Design request and response schemas using Pydantic.
  • Store data using an in-memory dictionary.
  • Implement CRUD operations.
  • Search resources using query parameters.
  • Handle API errors using HTTPException.
  • Test REST APIs using Swagger UI.

Architecture

Implementation Roadmap

We will build the Student Management REST API from scratch using the following steps:
  1. Create the Project Structure
  2. Initialize the FastAPI Application
  3. Create the Student Schemas
  4. Create the In-Memory Data Store
  5. Implement the Get All Students API
  6. Implement the Get Student by ID API
  7. Implement the Search Students API
  8. Handle Resource Not Found Errors Using HTTPException
  9. Implement the Create Student API
  10. Implement the Update Student API
  11. Implement the Delete Student API
  12. Test the Complete Application

Step 1: Create the Project Structure

Objective Create the project structure and install the required libraries for building the Student Management REST API. Instructions Create a new FastAPI project and install the required dependencies. Implementation Steps Step 1: Create a new project folder named student-api. Step 2: Open the project folder in your preferred editor (such as VS Code). Step 3: Create and activate a Python virtual environment. Step 4: Install the required libraries using uv. Step 5: Create the project structure shown below.
Note: Create the folders and files using your preferred editor or your operating system’s file explorer. You may also use terminal commands if you are comfortable with the command line.
Task Create the project structure and install the required dependencies for the Student Management REST API.
Create the Project
Create a Virtual Environment
Activate the Virtual EnvironmentmacOS / Linux
Windows
Install the Required Libraries
Create the Project StructureCreate the following folders and files.
Verify Verify that:
  • The project has been created successfully.
  • The virtual environment has been activated.
  • FastAPI and Uvicorn have been installed.
  • The app folder has been created.
  • The main.py file has been created.
  • The project structure matches the required layout.
Commit Changes
Create a .gitignore file with the following content.
Initialize the Git repository and commit the project.

Step 2: Initialize the FastAPI Application

Objective Initialize the FastAPI application and implement a simple Health Check endpoint. Instructions Open the main.py file and initialize the FastAPI application. Implementation Steps Step 1: Import the FastAPI class. Step 2: Create a FastAPI application. Step 3: Implement a Health Check endpoint. Task Initialize the FastAPI application and implement a Health Check endpoint.
Update app/main.py
Verify Run the application.
Open the Health Check endpoint.
Expected Response
Open the Swagger UI.
Verify that:
  • The application starts successfully.
  • The Health Check endpoint is accessible.
  • The Swagger UI loads successfully.
Commit Changes

Step 3: Create the Student Schemas

Objective Create Pydantic schemas for validating API requests and formatting API responses. Instructions Create a schemas.py file and implement the required request and response schemas. Implementation Steps Step 1: Create a StudentBase schema containing the common student fields. Step 2: Apply the required validations to each field. Step 3: Create a StudentCreate schema by inheriting from StudentBase. Step 4: Create a StudentUpdate schema with all fields optional. Step 5: Create a StudentResponse schema by inheriting from StudentBase and adding the id field. Task Create the Student request and response schemas.
Create app/schemas.py
Verify Verify that:
  • The StudentBase schema has been created.
  • The StudentCreate schema inherits from StudentBase.
  • The StudentUpdate schema contains optional fields.
  • The StudentResponse schema inherits from StudentBase.
  • All field validations have been implemented successfully.
Commit Changes

Step 4: Create the In-Memory Data Store

Objective Create an in-memory data store for managing student records. Instructions Create a data.py file and initialize an in-memory dictionary with sample student records. Implementation Steps Step 1: Create an empty dictionary named students. Step 2: Add a few sample student records to the dictionary. Step 3: Use the student ID as the key and the student details as the value. Task Create the in-memory data store with sample student records.
Create app/data.py
Verify Verify that:
  • The data.py file has been created.
  • The students dictionary has been initialized.
  • The dictionary contains five student records.
  • Each student has a unique ID.
Note: These sample records will be used to test the CRUD and Search APIs in the upcoming steps.
Commit Changes

Step 5: Implement the Get All Students API

Objective Implement the Get All Students API to retrieve all student records from the in-memory data store. Instructions Open the main.py file and implement the Get All Students API. Implementation Steps Step 1: Import the StudentResponse schema. Step 2: Create the GET /students endpoint. Step 3: Retrieve all student records from the in-memory data store. Step 4: Return the list of students. Task Implement the Get All Students API.
Update the imports in app/main.py
Implement the Get All Students endpoint
Verify Run the application.
Open the Swagger UI.
Invoke the GET /students endpoint. Verify that:
  • All student records are returned successfully.
  • A 200 OK response is returned.
  • The response contains all students stored in the in-memory data store.
Expected Response
Commit Changes

Step 6: Implement the Get Student by ID API

Objective Implement the Get Student by ID API to retrieve a student using the student ID. Instructions Open the main.py file and implement the Get Student by ID API. Implementation Steps Step 1: Import the required classes. Step 2: Define a validated path parameter for the student ID. Step 3: Create the GET /students/{student_id} endpoint. Step 4: Retrieve the student from the in-memory data store. Step 5: Raise an HTTPException if the student does not exist. Step 6: Return the student. Task Implement the Get Student by ID API.
Update the imports in app/main.py
Add the validated path parameter
Implement the Get Student by ID endpoint
Verify Run the application.
Open the Swagger UI.
Invoke the GET /students/{student_id} endpoint. Example
Verify that:
  • The student details are returned successfully.
  • A 200 OK response is returned.
  • Requesting a non-existent student returns 404 Not Found.
  • Providing a student ID less than or equal to 0 returns 422 Unprocessable Entity.
  • The path parameter validation is visible in the Swagger UI.
Expected Response
Commit Changes

Step 7: Implement the Search Students by Course API

Objective Implement the Search Students by Course API to retrieve students belonging to a specific course. Instructions Open the main.py file and implement the Search Students by Course API. Implementation Steps Step 1: Import the Query class. Step 2: Define a validated query parameter for the course name. Step 3: Create the GET /students/search endpoint. Step 4: Search for students whose course matches the given course name. Step 5: Return the matching students. Task Implement the Search Students by Course API.
Update the imports in app/main.py
Add the validated query parameter
Implement the Search Students by Course endpoint
Verify Run the application.
Open the Swagger UI.
Invoke the GET /students/search endpoint. Example
Verify that:
  • Matching students are returned successfully.
  • The search is case-insensitive.
  • An empty list is returned when no matching students are found.
  • The query parameter validation is visible in the Swagger UI.
Expected Response
Expected Response (No Matches)
Commit Changes

Step 8: Implement the Create Student API

Objective Implement the Create Student API to add a new student to the in-memory data store. Instructions Open the main.py file and implement the Create Student API. Implementation Steps Step 1: Import the StudentCreate schema. Step 2: Create the POST /students endpoint. Step 3: Generate the next available student ID. Step 4: Create a new student record. Step 5: Add the student to the in-memory data store. Step 6: Return the newly created student. Task Implement the Create Student API.
Update the imports in app/main.py
Implement the Create Student endpoint
Verify Run the application.
Open the Swagger UI.
Invoke the POST /students endpoint. Request Body
Verify that:
  • A new student is created successfully.
  • A unique student ID is generated automatically.
  • The student is added to the in-memory data store.
  • A 201 Created response is returned.
Expected Response
Commit Changes

Step 9: Implement the Update Student API

Objective Implement the Update Student API to modify an existing student record. Instructions Open the main.py file and implement the Update Student API. Implementation Steps Step 1: Import the StudentUpdate schema. Step 2: Create the PUT /students/{student_id} endpoint. Step 3: Retrieve the student from the in-memory data store. Step 4: Raise an HTTPException if the student does not exist. Step 5: Update only the fields provided in the request. Step 6: Return the updated student. Task Implement the Update Student API.
Update the imports in app/main.py
Implement the Update Student endpoint
Verify Run the application.
Open the Swagger UI.
Invoke the PUT /students/{student_id} endpoint. Example
Request Body
Verify that:
  • The student record is updated successfully.
  • Only the fields provided in the request are updated.
  • Existing field values remain unchanged.
  • Updating a non-existent student returns 404 Not Found.
Expected Response
Commit Changes

Step 10: Implement the Delete Student API

Objective Implement the Delete Student API to remove a student from the in-memory data store. Instructions Open the main.py file and implement the Delete Student API. Implementation Steps Step 1: Create the DELETE /students/{student_id} endpoint. Step 2: Retrieve the student from the in-memory data store. Step 3: Raise an HTTPException if the student does not exist. Step 4: Delete the student from the in-memory data store. Step 5: Return a success message. Task Implement the Delete Student API.
Implement the Delete Student endpoint
Verify Run the application.
Open the Swagger UI.
Invoke the DELETE /students/{student_id} endpoint. Example
Verify that:
  • The student is deleted successfully.
  • A success message is returned.
  • Deleting the same student again returns 404 Not Found.
Expected Response
Commit Changes

Step 11: Test the Complete Student Management API

Objective Test all the REST APIs implemented in the Student Management application. Instructions Run the FastAPI application and test each endpoint using the Swagger UI. Implementation Steps Step 1: Start the FastAPI application. Step 2: Open the Swagger UI. Step 3: Test the Get All Students API. Step 4: Test the Get Student by ID API. Step 5: Test the Search Students by Course API. Step 6: Test the Create Student API. Step 7: Test the Update Student API. Step 8: Test the Delete Student API. Task Test all the APIs implemented in the Student Management application.
Run the application.
Open the Swagger UI.
Test the APIs in the following order.
Verify Verify that:
  • All APIs execute successfully.
  • The expected HTTP status codes are returned.
  • Student records can be created, retrieved, updated, searched, and deleted.
  • Invalid student IDs return 404 Not Found.
  • Invalid path and query parameters return 422 Unprocessable Entity.
  • All APIs are available in the Swagger UI.
Commit Changes