Transform your patient records into a smart and structured RESTful API with FastAPI and Pydantic.
A RESTful API built with FastAPI for managing patient records with automatic BMI calculation and health assessment. This project demonstrates fundamental concepts of API development including CRUD operations, data validation, and error handling.
- Complete CRUD Operations: Create, read, update, and delete patient records
- Automatic BMI Calculation: Computes BMI based on height and weight
- Health Assessment: Provides health verdict based on BMI ranges
- Data Validation: Robust input validation using Pydantic models
- Sorting Capabilities: Sort patients by age, height, weight, or BMI
- Error Handling: Comprehensive error responses with proper HTTP status codes
- Interactive Documentation: Auto-generated API documentation with Swagger UI
- FastAPI: Modern, fast web framework for building APIs
- Pydantic: Data validation and settings management using Python type annotations
- Python 3.8+: Core programming language
- JSON: Data storage format
- Python 3.8 or higher
- uv - Ultra-fast Python package installer and resolver
-
Install uv (if you haven't already)
# On macOS and Linux curl -LsSf https://astral.sh/uv/install.sh | sh # On Windows powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
-
Clone the repository
git clone https://github.com/ZohaibCodez/patient-management-api.git cd patient-management-api -
Create and activate virtual environment with uv
uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies
uv pip install fastapi uvicorn[standard] pydantic
-
Run the application
uv run uvicorn main:app --reload
-
Clone the repository
git clone https://github.com/ZohaibCodez/patient-management-api.git cd patient-management-api -
Create a virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Create the data file
echo "{}" > patients.json
-
Run the application
uvicorn main:app --reload
The API will be available at http://localhost:8000
Once the server is running, visit:
- Interactive API Documentation: http://localhost:8000/docs
- Alternative Documentation: http://localhost:8000/redoc
GET /- Welcome messageGET /about- API description
GET /view- Retrieve all patientsGET /patient/{patient_id}- Get specific patient by IDPOST /create- Create new patientPUT /edit/{patient_id}- Update existing patientDELETE /delete/{patient_id}- Delete patient
GET /sort- Sort patients by specified criteria
flowchart LR
Client[Client Request] --> API[FastAPI Endpoints]
API --> DB[patients.json File]
DB --> API
API --> Client
curl -X POST "http://localhost:8000/create" \
-H "Content-Type: application/json" \
-d '{
"id": "P001",
"name": "John Doe",
"city": "New York",
"age": 30,
"gender": "male",
"height": 1.75,
"weight": 70.5
}'curl -X GET "http://localhost:8000/view"curl -X GET "http://localhost:8000/sort?sort_by=bmi&order=desc"{
"id": "P001",
"name": "John Doe",
"city": "New York",
"age": 30,
"gender": "male",
"height": 1.75,
"weight": 70.5,
"bmi": 23.0,
"verdict": "Normal"
}- id: Unique patient identifier (string)
- name: Patient's full name (string)
- city: Patient's city of residence (string)
- age: Patient's age in years (integer, 1-119)
- gender: Patient's gender (male/female/others)
- height: Patient's height in meters (float, > 0)
- weight: Patient's weight in kilograms (float, > 0)
- bmi: Auto-calculated Body Mass Index (computed field)
- verdict: Health assessment based on BMI (computed field)
| BMI Range | Verdict |
|---|---|
| < 18.5 | Underweight |
| 18.5 - 24.9 | Normal |
| 25.0 - 29.9 | Overweight |
| β₯ 30.0 | Obese |
uv pip install fastapi uvicorn[standard] pydanticfastapi==0.104.1
uvicorn[standard]==0.24.0
pydantic==2.5.0
patient-management-api/
β
βββ main.py # FastAPI application
βββ patients.json # Data storage file
βββ requirements.txt # Python dependencies (for pip users)
βββ README.md # Project documentation
βββ .gitignore # Git ignore file
βββ .venv/ # Virtual environment (created by uv)
- Database integration (PostgreSQL/MySQL)
- User authentication and authorization
- Advanced filtering and search capabilities
- Patient medical history tracking
- Export functionality (PDF/Excel)
- Unit tests and integration tests
- Docker containerization
- API rate limiting
This is a learning project, but contributions are welcome! Please feel free to:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
This project is open source and available under the MIT License.
This project was built as part of my FastAPI learning journey. It demonstrates:
- RESTful API design principles
- Data validation and error handling
- Automatic documentation generation
- Clean code practices
If you have any questions or suggestions, feel free to reach out:
- GitHub: ZohaibCodez
- LinkedIn: ZohaibCodez
β If you found this project helpful, please give it a star!
