Skip to content

CynthiaKaluson/medibook-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ₯ MediBook API

A production-ready Smart Appointment REST API for healthcare clinics β€” built with FastAPI, PostgreSQL, and AI.

Python FastAPI PostgreSQL OpenAI Deployed on Render License: MIT


🌐 Live Demo

Resource Link
πŸš€ Live API https://medibook-api-9xi4.onrender.com
πŸ“– Interactive Docs (Swagger) https://medibook-api-9xi4.onrender.com/docs
❀️ Health Check https://medibook-api-9xi4.onrender.com/health

πŸ“Œ What is MediBook?

MediBook is a backend API that powers a smart healthcare appointment booking system. It allows clinics to manage doctors and availability slots, while patients can book appointments, receive email confirmations, and interact with an AI assistant.

Built as a portfolio project to demonstrate real-world backend engineering skills β€” async Python, relational database design, JWT authentication, third-party integrations, AI features, and cloud deployment.


✨ Features

πŸ” Authentication & Security

  • Clinic and patient registration with bcrypt password hashing
  • JWT token-based authentication with role separation (clinic vs patient)
  • Google OAuth 2.0 login for patients

πŸ₯ Clinic Management

  • Clinics register and manage their own doctors
  • Full multi-tenancy β€” each clinic only sees their own data
  • Doctor profiles with specialization, bio, and contact info

πŸ“… Smart Appointment Booking

  • Clinics create available time slots for doctors
  • Overlap detection β€” no double-booking of slots
  • Conflict detection β€” patients can't book two appointments at the same time
  • Atomic slot locking β€” slot marked unavailable in the same DB transaction as booking
  • Patients can cancel appointments (slot automatically freed)
  • Clinics can update appointment status and add clinical notes

πŸ“§ Email Notifications

  • Confirmation email on booking (human-readable date/time format)
  • Cancellation email when appointment is cancelled
  • 24-hour reminder emails via Celery background tasks
  • Powered by Resend

πŸ€– AI Features (OpenAI GPT-4o-mini)

  • Smart Slot Suggester β€” recommends the best available slot based on patient's medical reason and urgency level
  • MediBot β€” context-aware patient chatbot that knows the patient's appointment history and answers questions about the platform

βš™οΈ Background Tasks

  • Celery + Redis task queue for automated appointment reminders
  • Runs every hour, scans for appointments within the next 24 hours

πŸ› οΈ Tech Stack

| Layer | Technology | Why |
|---|---|---|
| Framework | FastAPI | Async, fast, auto-docs, modern Python |
| Database | PostgreSQL | Relational data integrity, industry standard |
| ORM | SQLAlchemy 2.0 (async) | Production-grade, type-safe queries |
| Migrations | Alembic | Version-controlled schema changes |
| Authentication | JWT + Google OAuth | Industry standard auth patterns |
| Email | Resend | Simple API, reliable delivery |
| AI | OpenAI GPT-4o-mini | Cost-efficient, powerful reasoning |
| Task Queue | Celery + Redis | Scalable background job processing |
| Deployment | Render | Cloud hosting with PostgreSQL |
| Validation | Pydantic v2 | Request/response validation |

πŸ“ Project Structure

medibook-api/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                  # FastAPI app entry point
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py            # Environment settings (Pydantic)
β”‚   β”‚   β”œβ”€β”€ security.py          # JWT + bcrypt
β”‚   β”‚   └── dependencies.py      # Auth dependencies
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   β”œβ”€β”€ base.py              # SQLAlchemy declarative base
β”‚   β”‚   └── session.py           # Async DB session
β”‚   β”œβ”€β”€ models/                  # SQLAlchemy models
β”‚   β”‚   β”œβ”€β”€ clinic.py
β”‚   β”‚   β”œβ”€β”€ doctor.py
β”‚   β”‚   β”œβ”€β”€ patient.py
β”‚   β”‚   β”œβ”€β”€ slot.py
β”‚   β”‚   └── appointment.py
β”‚   β”œβ”€β”€ schemas/                 # Pydantic schemas
β”‚   β”œβ”€β”€ api/v1/endpoints/        # Route handlers
β”‚   β”‚   β”œβ”€β”€ auth.py
β”‚   β”‚   β”œβ”€β”€ doctors.py
β”‚   β”‚   β”œβ”€β”€ slots.py
β”‚   β”‚   β”œβ”€β”€ appointments.py
β”‚   β”‚   └── ai.py
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ email_service.py     # Resend integration
β”‚   β”‚   └── ai_service.py        # OpenAI integration
β”‚   └── tasks/
β”‚       └── reminder_tasks.py    # Celery background tasks
β”œβ”€β”€ alembic/                     # Database migrations
β”œβ”€β”€ render.yaml                  # Render deployment config
β”œβ”€β”€ requirements.txt
└── .env.example

πŸš€ Running Locally

Prerequisites

  • Python 3.11+
  • PostgreSQL
  • Redis (for Celery)
  • Conda or virtualenv

Setup

# 1. Clone the repository
git clone https://github.com/CynthiaKaluson/medibook-api.git
cd medibook-api

# 2. Create and activate environment
conda create -n medibook python=3.13
conda activate medibook

# 3. Install dependencies
pip install -r requirements.txt

# 4. Set up environment variables
cp .env.example .env
# Edit .env with your values

# 5. Run database migrations
alembic upgrade head

# 6. Start the server
uvicorn app.main:app --reload

Visit http://localhost:8000/docs for the interactive API documentation.


πŸ”‘ Environment Variables

DATABASE_URL=postgresql+asyncpg://user:password@localhost:5432/medibook_db
SECRET_KEY=your-secret-key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
OPENAI_API_KEY=sk-...
RESEND_API_KEY=re_...
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
REDIS_URL=redis://localhost:6379/0
APP_ENV=development

πŸ“‘ API Endpoints

Authentication

Method Endpoint Description Auth
POST /api/v1/auth/clinic/register Register a clinic None
POST /api/v1/auth/clinic/login Clinic login β†’ JWT None
POST /api/v1/auth/patient/register Register a patient None
POST /api/v1/auth/patient/login Patient login β†’ JWT None
GET /api/v1/auth/patient/google Google OAuth URL None
GET /api/v1/auth/patient/google/callback Google OAuth callback None

Doctors

Method Endpoint Description Auth
POST /api/v1/doctors/ Create doctor Clinic JWT
GET /api/v1/doctors/ List clinic's doctors Clinic JWT
GET /api/v1/doctors/{id} Get doctor by ID Clinic JWT
DELETE /api/v1/doctors/{id} Delete doctor Clinic JWT

Slots

Method Endpoint Description Auth
POST /api/v1/slots/ Create time slot Clinic JWT
GET /api/v1/slots/doctor/{id} List available slots None

Appointments

Method Endpoint Description Auth
POST /api/v1/appointments/ Book appointment Patient JWT
GET /api/v1/appointments/my My appointments Patient JWT
PATCH /api/v1/appointments/{id}/cancel Cancel appointment Patient JWT
PATCH /api/v1/appointments/{id}/status Update status/notes Clinic JWT
GET /api/v1/appointments/clinic/all All clinic appointments Clinic JWT

AI Features

Method Endpoint Description Auth
POST /api/v1/ai/suggest-slot AI slot recommendation Patient JWT
POST /api/v1/ai/chat Chat with MediBot Patient JWT

πŸ’‘ Example Requests

Book an Appointment

curl -X POST https://medibook-api-9xi4.onrender.com/api/v1/appointments/ \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "slot_id": "uuid-here",
    "doctor_id": "uuid-here",
    "reason": "Annual checkup"
  }'

AI Smart Slot Suggestion

curl -X POST https://medibook-api-9xi4.onrender.com/api/v1/ai/suggest-slot \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "doctor_id": "uuid-here",
    "reason": "Severe chest pain and difficulty breathing"
  }'

Response:

{
  "suggestion": {
    "recommended_slot_id": "uuid-here",
    "reason": "Morning slot recommended for severe symptoms requiring prompt evaluation",
    "urgency": "high"
  },
  "doctor": "Dr. Chidi Okeke",
  "specialization": "Cardiologist"
}

Chat with MediBot

curl -X POST https://medibook-api-9xi4.onrender.com/api/v1/ai/chat \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "message": "What appointments do I have coming up?",
    "history": []
  }'

Response:

{
  "response": "You have one upcoming appointment β€” a routine checkup scheduled for March 20, 2026 at 09:00 AM with Dr. Chidi Okeke (Cardiologist). Would you like to make any changes?",
  "patient": "Ada Obi"
}

πŸ—οΈ Key Architecture Decisions

Async everywhere β€” FastAPI + SQLAlchemy 2.0 async + asyncpg means the API handles concurrent requests without blocking, which is critical for healthcare systems with simultaneous users.

UUID primary keys β€” All IDs are UUIDs instead of integers, preventing enumeration attacks where an attacker guesses resource IDs.

Transactional slot locking β€” When a patient books an appointment, the slot is marked unavailable in the same database commit as the appointment creation. This prevents race conditions where two patients book the same slot simultaneously.

Multi-tenancy by design β€” Every doctor and appointment query is scoped to the authenticated clinic's ID, ensuring clinics never see each other's data.

Non-blocking email β€” Email failures are caught and logged but never crash the booking flow. Patients get their appointment even if the email service is temporarily down.


πŸ‘©πŸ½β€πŸ’» Author


**Cynthia Kalu**
Junior Backend Developer
β€’ [GitHub](https://github.com/CynthiaKaluson)
β€’ [LinkedIn](https://www.linkedin.com/in/cynthia-kalu-okorie/)

πŸ“„ License


This project is licensed under the MIT License.

About

A production-ready Smart Appointment REST API for healthcare clinics, built with FastAPI, PostgreSQL, JWT + Google OAuth, AI-powered slot suggestions, context-aware patient chatbot, and automated email reminders.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors