Skip to content

About

REST API built with FastAPI, MySQL, SQLAlchemy and JWT authentication for managing students, courses, enrollments and grades.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Student System REST API

REST API developed with FastAPI for managing students, courses, enrollments and grades.

The project includes authentication with JWT, relational database modeling with MySQL, request validation with Pydantic and automatic API documentation using Swagger UI.

Screenshots

Swagger API Documentation

Swagger Overview

JWT Authentication

Authentication Login

Students Endpoint

Students List

Student Grades Endpoint

Student Grades

Project Overview

This API was built as a backend portfolio project to demonstrate:

  • REST API development
  • Database design with relationships
  • CRUD operations
  • JWT authentication
  • Request and response validation
  • SQLAlchemy ORM usage
  • MySQL integration
  • API documentation with Swagger UI

Tech Stack

  • Python 3
  • FastAPI
  • MySQL
  • SQLAlchemy
  • Pydantic
  • PyMySQL
  • Python-Jose
  • Passlib
  • Bcrypt
  • Uvicorn
  • Python Dotenv

Main Features

  • User registration
  • User login with JWT token
  • Protected endpoints using Bearer Token
  • Student CRUD
  • Course CRUD
  • Student enrollment in courses
  • Grade management
  • Student grade consultation
  • Automatic API documentation with Swagger UI

Installation

Clone the repository:

git clone https://github.com/Santlas09/student-system-api.git
cd student-system-api

Create a virtual environment:

python -m venv venv

Activate the virtual environment on Windows:

venv\Scripts\activate

Install dependencies:

pip install -r requirements.txt

Environment Variables

Create a .env file in the root directory and configure the following variables:

DATABASE_URL=mysql+pymysql://student_api_user:StudentApi123@localhost:3306/student_system_api

SECRET_KEY=change_this_secret_key_in_production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60

Database Setup

Create the MySQL database:

CREATE DATABASE student_system_api
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

Optional: create a dedicated database user:

CREATE USER 'student_api_user'@'localhost' IDENTIFIED BY 'StudentApi123';

GRANT ALL PRIVILEGES ON student_system_api.* TO 'student_api_user'@'localhost';

FLUSH PRIVILEGES;

Run the Project

Start the development server:

uvicorn app.main:app --reload

The API will be available at:

http://localhost:8000

Swagger documentation:

http://localhost:8000/docs

Authentication Flow

Register User

POST /auth/register

Request body:

{
  "full_name": "Santiago Reyes Herrera",
  "email": "santiago@example.com",
  "password": "123456"
}

Login User

POST /auth/login

Request body:

{
  "email": "santiago@example.com",
  "password": "123456"
}

Response:

{
  "access_token": "your_jwt_token_here",
  "token_type": "bearer"
}

Use Token

Protected endpoints require this header:

Authorization: Bearer your_jwt_token_here

API Endpoints

Authentication

Method Endpoint Description Protected
POST /auth/register Register a new user No
POST /auth/login Login user and get token No
GET /auth/me Get current authenticated user Yes

Students

Method Endpoint Description Protected
GET /students/ List all students No
POST /students/ Create student Yes
GET /students/{student_id} Get student by ID No
PUT /students/{student_id} Update student Yes
DELETE /students/{student_id} Delete student Yes

Courses

Method Endpoint Description Protected
GET /courses/ List all courses No
POST /courses/ Create course Yes
GET /courses/{course_id} Get course by ID No
PUT /courses/{course_id} Update course Yes
DELETE /courses/{course_id} Delete course Yes

Enrollments

Method Endpoint Description Protected
GET /enrollments/ List enrollments No
POST /enrollments/ Enroll student in course Yes
GET /enrollments/{enrollment_id} Get enrollment by ID No
GET /enrollments/student/{student_id} Get enrollments by student No
DELETE /enrollments/{enrollment_id} Delete enrollment Yes

Grades

Method Endpoint Description Protected
GET /grades/ List grades No
POST /grades/ Create grade Yes
GET /grades/{grade_id} Get grade by ID No
PUT /grades/{grade_id} Update grade Yes
DELETE /grades/{grade_id} Delete grade Yes
GET /students/{student_id}/grades Get student grades No

Example Requests

Create Student

POST /students/
Content-Type: application/json
Authorization: Bearer your_jwt_token_here
{
  "first_name": "Santiago",
  "last_name": "Reyes",
  "email": "student1@example.com",
  "age": 22
}

Create Course

POST /courses/
Content-Type: application/json
Authorization: Bearer your_jwt_token_here
{
  "name": "Programming Fundamentals",
  "code": "PROG101",
  "description": "Introduction to programming logic using Python.",
  "credits": 3
}

Create Enrollment

POST /enrollments/
Content-Type: application/json
Authorization: Bearer your_jwt_token_here
{
  "student_id": 1,
  "course_id": 1
}

Create Grade

POST /grades/
Content-Type: application/json
Authorization: Bearer your_jwt_token_here
{
  "enrollment_id": 1,
  "title": "First Exam",
  "score": 4.5,
  "max_score": 5.0
}

Get Student Grades

GET /students/1/grades

Example response:

[
  {
    "grade_id": 1,
    "student_id": 1,
    "student_name": "Santiago Reyes",
    "course_id": 1,
    "course_name": "Programming Fundamentals",
    "course_code": "PROG101",
    "title": "First Exam",
    "score": 4.5,
    "max_score": 5.0,
    "created_at": "2026-06-07T00:00:00"
  }
]

Testing the API

You can test the API using:

  • Swagger UI: http://localhost:8000/docs
  • REST Client extension in VS Code
  • Postman
  • Hoppscotch

The project includes an api_tests.http file with ready-to-use requests.

Security Notes

  • Passwords are stored as hashes.
  • JWT tokens are required for protected endpoints.
  • Sensitive values are stored in .env.
  • The .env file should not be uploaded to GitHub.
  • A .env.example file is included to show the required environment variables without exposing real credentials.

Author

Santiago Reyes Herrera Systems Engineering Student Cartagena, Colombia

GitHub: Santlas09

About

REST API built with FastAPI, MySQL, SQLAlchemy and JWT authentication for managing students, courses, enrollments and grades.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages