Building a Clinic Website & Appointment Booking Backend with Phone OTP & Zalo Reminders
Build a clinic website and appointment backend on Habify Base: database-level slot collision locks, patient verification via your BYO SMS/Zalo OTP account with 5 anti-abuse layers, and physical database privacy.

You can operate a full clinic presentation and appointment scheduling backend on Habify Base with database-level privacy suited for patient contact records. Doctor schedules, time slots, bookings, BYO phone OTP verification, and Zalo reminders run cleanly inside an isolated PostgreSQL 18 database.
Two classic booking failures, overlapping appointments on the same doctor slot and expensive SMS OTP bombing attacks, are eliminated natively at the database and gateway tiers.
1. Clinic Booking Architecture on Habify Base
Your clinic frontend connects over @habifybase/sdk to six coordinated backend capabilities:
| Clinic Feature | Habify Base Solution | Operational Value |
|---|---|---|
| Specialties, doctors & fees | Read-only public catalog policies | Fast SEO pages with zero patient table exposure |
| Double-booking lock | PostgreSQL partial unique index (doctor_id, slot_start) | Concurrent requests serialize cleanly inside Postgres |
| Patient phone login | Passwordless OTP over clinic's own SMS or Zalo OA | Frictionless sign-in to check or reschedule visits |
| Anti-SMS-pumping shield | 5 layers: Turnstile, country allowlist, cooldown, daily caps | Blocks toll-fraud floods before any carrier API triggers |
| Automated visit reminders | Zalo ZBS (OAuth v4 PKCE) or 8 BYO SMS carriers | Dispatched directly from the clinic's official brand |
| Sensitive data isolation | Physical per-project DB + VN replica + Clone PII masking | Matches Law 91/2025 privacy rules across prod and test |
2. Preventing Double-Booked Doctor Slots Inside PostgreSQL
Application-level availability checks fail under concurrent traffic. Declaring a PostgreSQL partial unique index over active bookings ensures only one patient can ever hold a doctor's time slot:
CREATE TABLE public.doctors (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
full_name text NOT NULL,
specialty text NOT NULL,
is_active boolean NOT NULL DEFAULT true
);
ALTER TABLE public.doctors ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.doctors FORCE ROW LEVEL SECURITY;
CREATE POLICY doctors_public_read ON public.doctors
FOR SELECT USING (is_active = true);
CREATE TABLE public.appointments (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
doctor_id uuid NOT NULL REFERENCES public.doctors(id),
patient_user_id uuid NOT NULL,
slot_start timestamptz NOT NULL,
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'confirmed', 'cancelled', 'completed')),
symptoms_note text,
created_at timestamptz NOT NULL DEFAULT now()
);
-- Khóa cứng chống đặt trùng giờ khi lịch chưa hủy
CREATE UNIQUE INDEX uniq_doctor_active_slot
ON public.appointments (doctor_id, slot_start)
WHERE status IN ('pending', 'confirmed');
ALTER TABLE public.appointments ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.appointments FORCE ROW LEVEL SECURITY;
CREATE POLICY appointments_patient_or_staff ON public.appointments
USING (
patient_user_id::text = current_setting('app.current_user_id', true)
OR current_setting('app.current_role', true) = 'clinic_staff'
)
WITH CHECK (
patient_user_id::text = current_setting('app.current_user_id', true)
OR current_setting('app.current_role', true) = 'clinic_staff'
);3. Patient Phone OTP and 5 Layers of Anti-SMS-Pumping Defense
Patients confirm bookings with a 6-digit OTP sent through your clinic's own SMS carrier or Zalo OA. Five built-in guardrails neutralize SMS pumping before carrier charges happen:
- Layer 1 (Human Verification): Enforces Cloudflare Turnstile or hCaptcha tokens before live SMS OTP dispatch.
- Layer 2 (Country Allowlist): Restricts destinations to configured prefixes (default VN +84), blocking international revenue-share numbers.
- Layer 3 (Cooldown & Attempt Caps): Applies 60-second resend cooldowns, hourly IP/phone limits, and 5-attempt invalidation.
- Layer 4 (Daily Project Cap): Enforces a hard daily OTP limit per project to bound carrier exposure.
- Layer 5 (Sandbox Test Numbers): Supports deterministic QA phone numbers with fixed OTP codes at zero carrier fee.
// 1. Gửi mã OTP SĐT cho bệnh nhân (kèm token chống bot Turnstile)
await habifyPublic.auth.sendOtp({
phone: "0909123456",
purpose: "login",
channel: "sms",
turnstile_token: turnstileToken,
});
// 2. Xác nhận OTP -> nhận phiên đăng nhập có gắn quyền RLS
const { user } = await habifyPublic.auth.verifyOtp({
phone: "0909123456",
code: otpCode,
purpose: "login",
});
// 3. Ghi lịch khám (tự động báo xung đột nếu khung giờ vừa có người đặt)
await habifyPublic.db.from("appointments").insert({
doctor_id: selectedDoctorId,
patient_user_id: user.id,
slot_start: "2026-12-02T09:00:00+07:00",
symptoms_note: symptomSummary,
});4. Zalo ZBS Reminders and Patient Privacy Isolation
Medical visit details carry strict obligations under Vietnam's Personal Data Protection Law 91/2025/QH15. Each clinic runs on its own physical PostgreSQL 18 database streamed to a physical Vietnam replica (vn_replica).
When engineers spin up staging copies via Clone-to-Sandbox, patient emails and phone numbers are masked automatically while refresh sessions are cleared. Prior to scheduled visits, automated server jobs trigger reminders through the clinic's authorized Zalo OA.
Frequently Asked Questions
Does Habify Base charge per-SMS fees for patient OTP messages?
No. Phone OTP dispatches strictly through your clinic's own BYO SMS carrier account or Zalo OA with zero platform surcharge.
How do returning patients view previous appointments without a password?
Enable passwordless phone OTP sign-in so returning patients can enter their number, confirm the 6-digit code, and inspect their booking history.
What happens if two patients submit the same doctor time slot at once?
PostgreSQL's partial unique index commits the first booking and returns a clean conflict error for the second so your UI prompts them to pick another time.
Related Documentation & Guides
Launch This Architecture in Free Sandbox in 3 Minutes
Spin up an isolated PostgreSQL 18 database, grab your hb_pk_* / hb_sk_* keys, and wire @habifybase/sdk.