Twallia — System Architecture
Technical Documentation

System Architecture

A complete technical overview of the Twallia platform — AI-powered voice teammates for the front line of small and medium business.

v2.4 Node.js 20 React 19 Next.js 15 Payload CMS 3 MongoDB OpenAI Realtime Gemini Live
Platform overview

System Architecture Diagram

Four layers — external users, telephony & frontend, core backend, and data & AI services.

External Users
Caller
Inbound phone call
Visitor
twallia.com website
Business Owner
cms.twallia.com portal
↓
Telephony & Frontend
Twilio
Phone numbers · SMS · Media Streams
React Frontend
Vite · React Router · Marketing + Auth
Next.js Portal
Payload CMS · Dashboard · Billing
↓
Core Backend — Node.js / Express
Auth API
Signup · Login · Verify · JWT
Voice Engine
OpenAI Realtime · Gemini Live · WS
Appointments
Google Cal · Outlook · NLU booking
Outbound Scheduler
Campaign dialer · cron · cost tracking
↓
Data & AI Services
MongoDB
Users · Tenants · Calls · Transcripts
OpenAI
GPT-4o Realtime · summaries
Gemini
Gemini Live · alt voice provider
Stripe
Subscriptions · Webhooks · Checkout
Brevo
Email verification · SMTP

Data flows

Key System Flows

How data moves through the platform for the four most important user journeys.

Inbound Call Flow

1Caller dials → Twilio receives call
2Twilio POST to /twilio/voice → returns TwiML with Media Stream URL
3WebSocket opens at /media-stream with tenant email param
4Load agent profile from CMS (JSON fallback)
5Connect to OpenAI/Gemini Realtime — stream audio bidirectionally
6AI triggers tool calls (book, cancel, transfer) server-side
7Save CallSession + Transcript to CMS, generate AI summary

Sign-Up Flow

1User submits form → POST /api/auth/signup
2Duplicate check in CMS (MongoDB)
3Create User + Tenant records in CMS
4Send 6-digit verification code via Brevo email
5POST /api/auth/verify → validate code
6Scrape company website → build agent profile
7Return JWT token → portal login

Appointment Booking

1AI detects booking intent during call
2Parse date/time → validateSlot() checks office hours
3Fetch free slots from Google/Outlook Calendar
4Scripted responses only — AI cannot fabricate availability
5Caller confirms → create calendar event
6Send confirmation SMS → save Appointment to CMS

Outbound Campaign

1User creates CallSchedule with prompt + customer list
2cron scheduler ticks every 60s, checks for due campaigns
3Per customer → check office hours + DNC list
4Initiate Twilio call → route to Realtime AI
5Save transcript + AI summary → log usage cost

Components

Platform Components

Each component and its responsibility within the system.

Marketing Frontend

React 19 + Vite SPA. Homepage, auth flows, contact, blog, and dynamic CMS-driven pages. Communicates with backend via REST API.

React 19ViteReact Router 7

CMS Portal

Next.js 15 + Payload CMS. Self-service portal: profile, phone settings, agent config, transcripts, usage analytics, billing and outbound campaigns.

Next.js 15Payload CMS 3TypeScript

Auth API

JWT-based auth with email verification. Dual persistence (MongoDB + JSON fallback). Rate-limited signup/verify routes. SSO token for portal login.

JWTBrevo emailbcrypt

Voice Engine

Real-time audio pipeline. Converts Twilio μ-law 8kHz to PCM for OpenAI/Gemini. Streams bidirectionally. Supports function calling mid-call.

OpenAI RealtimeGemini LiveWebSocket

Appointment Engine

Server-side booking logic. AI cannot directly access the calendar — all intent parsing, slot checking and booking happens server-side. Anti-hallucination by design.

Google CalendarOutlookState machine

Telephony Layer

Twilio provider with TwiML generation, media stream routing, SMS, SIP domains, softphone (Twilio.js) and per-second call cost tracking.

TwilioTwiMLSIPMedia Streams

Outbound Scheduler

Cron-based campaign dialer. Fetches active CallSchedules from CMS, respects office hours, initiates Twilio calls and tracks results and cost.

cronCallSchedulescost tracking

Chat Widget

Embeddable JS snippet for any website. No auth required, rate-limited. Uses GPT-4.1-mini for conversations, captures leads and supports appointment booking.

EmbeddableGPT-4.1-miniLead capture

MongoDB / CMS Collections

20 Payload CMS collections: Users, Tenants, TenantProfiles, CallSessions, Transcripts, Customers, CallSchedules, Appointments, UsageEvents and more.

MongoDBPayload CMS20 collections

Billing (Stripe)

Three subscription tiers — Starter $39, Growth $89, Pro $199/mo. Checkout sessions, webhook handler for subscription lifecycle events.

StripeSubscriptionsWebhooks

Integrations

External Services

Third-party services the platform depends on and their role.

ServicePurposeAuthTier
TwilioPhone numbers, inbound/outbound calls, SMS, media streams, SIP domains, softphone tokensAccount SID + Auth TokenCore
OpenAIGPT-4o Realtime (voice), gpt-4.1-mini (chat), gpt-4.1-nano (summaries)API KeyCore
Google GeminiGemini Live — alternate real-time voice providerAPI KeyCore
MongoDB AtlasPrimary database for all 20 CMS collectionsConnection URICore
StripeSubscription management, checkout sessions, webhook eventsSecret Key + Webhook SecretCore
Google CalendarAppointment availability, booking and cancellation via OAuth 2.0OAuth 2.0 (per tenant)Core
Microsoft OutlookAlternate calendar provider for Microsoft 365 usersOAuth 2.0 (per tenant)Optional
BrevoTransactional email — verification codes, password reset, contact notificationsAPI Key + SMTPCore
Google AnalyticsWebsite analytics, page views, user behaviour on twallia.comMeasurement IDAnalytics
CoolifySelf-hosted deployment — CI/CD, environment variables, SSL, domain routingDeploy webhookInfra

Tech stack

Technology Stack

Runtime
Node.js 20 · Express 5
Frontend
React 19 · Vite · React Router 7
CMS & Portal
Next.js 15 · Payload CMS 3
Database
MongoDB Atlas · JSON fallback
AI Providers
OpenAI SDK v6 · Google Gemini
Telephony
Twilio SDK v5 · TwiML · SIP
Auth
JWT · bcrypt · API Key (Payload)
Real-time
WebSocket · Twilio Media Streams
CI / Deploy
GitHub Actions · Coolify · Nixpacks
Testing
Vitest (unit) · Playwright (E2E)
Styling
CSS variables · Quicksand · #FF6B2C
Email
Brevo SMTP API · Nodemailer