Provides a comprehensive health microservice API with endpoints for managing doctors, patients, appointments, medical records, telemedicine visits, lab orders and results, referrals, pharmacy prescriptions, and insurance claims through JWT-authenticated RESTful operations.
Uses PostgreSQL as the database backend for storing and managing health-related data including patient records, appointments, and medical information with async SQLAlchemy support.
Health Microservice API
A comprehensive FastAPI microservice for health-related operations with JWT authentication, PostgreSQL database, and Alembic migrations.It is backed by MCP server using FastApiMCP
Working Demo
health-api-mcp-server-with-fastapi-demo.webm
Features
JWT-based authentication
PostgreSQL database with async SQLAlchemy
Alembic database migrations
Comprehensive health domain models (Patient, Doctor, Appointment, Medical Record)
RESTful API endpoints
Interactive API documentation (Swagger UI)
Modular project structure
CORS middleware
Async/await support
UV package manager for fast dependency resolution
Comprehensive test suite
Prerequisites
Python 3.13.3+
PostgreSQL
UV package manager
Project Structure
The project follows a modular structure suitable for large applications with clear separation of concerns between models, schemas, routes, and business logic.
Installation
Install UV
Setup Project
Clone the repository:
Install dependencies:
Set up environment variables:
Database Migrations with Alembic (using UV)
Alembic is used for handling database migrations. All commands below assume you are in the project root directory.
But first off all create up a PostgreSQL database:
Generate a New Migration (Autogenerate)
Apply Migrations (Upgrade Database)
Start the application (choose one):
Some more required alembic migration commands
These commands are not required during setup but are useful for managing migrations in the future during development.
Downgrade Database (Revert Last Migration)
View Current Migration Status
Show Migration History
For more Alembic commands and usage, see the Alembic documentation.
Development
Install development dependencies:
Run tests:
Code formatting:
Type checking:
Docker
Build and run with Docker Compose:
This will start both the PostgreSQL database and the FastAPI application.
API Documentation
Once the application is running, visit:
Swagger UI: http://localhost:5000/docs
ReDoc: http://localhost:5000/redoc
MCP ping: check on terminal with the following command
Database Migrations
Create a new migration:
Apply migrations:
Authentication
Register a new user:
POST /auth/registerLogin to get access token:
POST /auth/loginUse the token in Authorization header:
Bearer <token>
Main API Endpoints (after authentication)
After obtaining a JWT access token, you can access the following endpoints:
Doctors
GET /doctors— List all doctorsGET /doctors/{doctor_id}— Get a specific doctor by IDPOST /doctors— Create a new doctorPUT /doctors/{doctor_id}— Update a doctor's informationDELETE /doctors/{doctor_id}— Delete a doctor
Patients
GET /patients— List all patientsGET /patients/{patient_id}— Get a specific patient by IDPOST /patients— Create a new patientPUT /patients/{patient_id}— Update a patient's informationDELETE /patients/{patient_id}— Delete a patient
Medical Records
GET /medical-records— List all medical records (optionally filter by patient)GET /medical-records/{record_id}— Get a specific medical record by IDPOST /medical-records— Create a new medical recordPUT /medical-records/{record_id}— Update a medical recordDELETE /medical-records/{record_id}— Delete a medical record
Appointments
GET /appointments— List all appointmentsGET /appointments/{appointment_id}— Get a specific appointment by IDPOST /appointments— Create a new appointmentPUT /appointments/{appointment_id}— Update an appointmentDELETE /appointments/{appointment_id}— Delete an appointment
Telemedicine
POST /telemedicine/visits/— Create a new virtual visitGET /telemedicine/visits/— List all virtual visitsGET /telemedicine/visits/{visit_id}— Get a specific virtual visit by IDPOST /telemedicine/chats/— Create a new chat logGET /telemedicine/chats/— List all chat logsGET /telemedicine/chats/{chat_id}— Get a specific chat log by IDPOST /telemedicine/videos/— Create a new video sessionGET /telemedicine/videos/— List all video sessionsGET /telemedicine/videos/{video_id}— Get a specific video session by ID
Lab
POST /lab/orders/— Create a new lab orderGET /lab/orders/— List all lab ordersGET /lab/orders/{order_id}— Get a specific lab order by IDPOST /lab/results/— Create a new lab resultGET /lab/results/— List all lab resultsGET /lab/results/{result_id}— Get a specific lab result by IDPOST /lab/images/— Create a new diagnostic imageGET /lab/images/— List all diagnostic imagesGET /lab/images/{image_id}— Get a specific diagnostic image by ID
Referral
POST /referral/requests/— Create a new referral requestGET /referral/requests/— List all referral requestsGET /referral/requests/{request_id}— Get a specific referral request by IDPOST /referral/statuses/— Create a new referral statusGET /referral/statuses/— List all referral statusesGET /referral/statuses/{status_id}— Get a specific referral status by IDPOST /referral/notes/— Create a new specialist noteGET /referral/notes/— List all specialist notesGET /referral/notes/{note_id}— Get a specific specialist note by ID
Pharmacy
POST /pharmacy/medications/— Create a new medicationGET /pharmacy/medications/— List all medicationsGET /pharmacy/medications/{med_id}— Get a specific medication by IDPOST /pharmacy/prescriptions/— Create a new prescriptionGET /pharmacy/prescriptions/— List all prescriptionsGET /pharmacy/prescriptions/{pres_id}— Get a specific prescription by IDPOST /pharmacy/orders/— Create a new pharmacy orderGET /pharmacy/orders/— List all pharmacy ordersGET /pharmacy/orders/{order_id}— Get a specific pharmacy order by ID
Insurance
POST /insurance/plans/— Create a new insurance planGET /insurance/plans/— List all insurance plansGET /insurance/plans/{plan_id}— Get a specific insurance plan by IDPOST /insurance/claims/— Create a new insurance claimGET /insurance/claims/— List all insurance claimsGET /insurance/claims/{claim_id}— Get a specific insurance claim by IDPOST /insurance/payments/— Create a new paymentGET /insurance/payments/— List all paymentsGET /insurance/payments/{payment_id}— Get a specific payment by IDPOST /insurance/invoices/— Create a new invoiceGET /insurance/invoices/— List all invoicesGET /insurance/invoices/{invoice_id}— Get a specific invoice by ID
All these endpoints require the Authorization: Bearer header. Refer to the interactive API docs at /docs for detailed request/response schemas and try out the endpoints interactively.
Package Management with UV
UV provides fast dependency resolution and installation. Useful commands:
uv sync- Install dependencies from lock fileuv add <package>- Add a new dependencyuv remove <package>- Remove a dependencyuv run <command>- Run command in virtual environmentuv lock- Update the lock file
MCP Integration
Integration with MCP is done by using the
FastApiMCP class from the fastapi_mcp package.
The MCP server is mounted to the FastAPI
application using the mount method.
windsurf settings,
vscode or cursor settings,
Note: I haven't tested on vscode or cursor yet.
License
MIT License
TODOs
Add more test cases
Add more features
Add more documentation
Add more security features
Add more logging
Add more monitoring
Add more performance optimization
Contributing
Install development dependencies:
uv sync --group devMake your changes
Run tests:
uv run pytestFormat code:
uv run black app/ tests/Submit a pull request
See more at [Contributing](https://github.com/ AlwaysSany/health-api/blob/main/CONTRIBUTING.md).
Contact
Author: Sany Ahmed
Email: sany2k8@gmail.com