How th3saquib/wato works
Purpose and Entry Points
wacrm is a self-hosted, account-scoped WhatsApp CRM: shared inbox, contacts, pipelines, broadcasts, visual automations/flows, AI assistance, and public integrations. The primary runtime is a Next.js App Router application rooted at src/app/layout.tsx, with authentication/dashboard route groups in src/app/(auth) and src/app/(dashboard). HTTP integration entry points are Next route handlers under src/app/api, notably WhatsApp webhooks and the versioned REST API.
Application Shell, Identity, and Tenancy
Supabase Auth establishes user identity; middleware protects navigation and redirects unauthenticated users. Account membership, roles, invitations, ownership, and scoped API access form the core multi-user boundary. Database RLS is the principal data-isolation mechanism.
• src/middleware.ts
• src/lib/auth/api-context.ts
• src/app/api/account/members/route.ts
• supabase/migrations/017_account_sharing.sql
CRM Workspace and Realtime Inbox
Dashboard pages compose client-facing CRM modules: inbox, contacts, pipelines, activity, notifications, and settings. Inbox operations resolve WhatsApp messages to conversations and contacts; browser-side realtime/presence hooks keep shared-agent views current.
• src/app/(dashboard)/inbox/page.tsx
• src/components/inbox/message-thread.tsx
• src/lib/inbox/conversations.ts
• src/hooks/use-realtime.ts
WhatsApp Boundary
The Meta Cloud API is isolated behind WhatsApp library code and route handlers. Outbound sends, reactions, media, template lifecycle, broadcast delivery, registration/configuration, and inbound webhook verification are handled at this boundary. Credentials/tokens require encryption, while inbound public webhooks require signature verification.
• src/app/api/whatsapp/webhook/route.ts
• src/lib/whatsapp/meta-api.ts
• src/lib/whatsapp/send-message.ts
• src/lib/whatsapp/encryption.ts
Campaigns, Automations, and Flows
Broadcasts use approved templates, recipient selection/personalization, scheduling, and status tracking. Automations and flows are separately modeled visual execution systems: API routes invoke validation and engines; cron endpoints advance scheduled or waiting work; actions can send through Meta and trigger webhooks.
• src/lib/automations/engine.ts
• src/app/api/automations/cron/route.ts
• src/lib/flows/engine.ts
• src/app/api/flows/cron/route.ts
• src/components/flows/flow-builder.tsx
AI Assistance and Knowledge Retrieval
AI features support generated drafts, constrained auto-replies with human handoff, usage tracking, and account-managed knowledge. Providers are OpenAI and Anthropic; retrieval uses Postgres full-text search and optionally pgvector embeddings. AI configuration and keys are administrative/account-scoped.
• src/app/api/ai/autoreply/[conversationId]/route.ts
• src/lib/ai/auto-reply.ts
• src/lib/ai/query.ts
• src/lib/ai/providers/openai.ts
External API, MCP, and Event Delivery
/api/v1 exposes authenticated CRM resources using scoped, revocable API keys. Registered outbound webhooks deliver signed events with SSRF protections. A distinct Node/TypeScript MCP server connects AI clients to this public API, defaulting to read-only operations with explicit write tooling.
• src/app/api/v1/contacts/route.ts
• src/lib/api-keys/keys.ts
• src/lib/webhooks/deliver.ts
• mcp-server/src/index.ts
Persistence and Deployment
Supabase provides Postgres, Auth, Storage, Realtime, RPCs, and RLS; migrations are the authoritative schema evolution path. The app runs on Node.js/Next.js and can deploy to Hostinger or other Node hosts. Meta Cloud API, Supabase, and optional OpenAI/Anthropic services are required material external dependencies.
• src/lib/supabase/server.ts
• src/lib/storage/upload-media.ts
• supabase/migrations/001_initial_schema.sql
• supabase/migrations/010_flows.sql
Main entry points: conversations.ts Conversation service · send-message.ts Message sender · server.ts Supabase platform · layout.tsx App shell & routes
Components 25
Next.js application
- App shell & routes · Hosts authenticated dashboard and HTTP route handlers.
src/app/layout.tsx - Auth middleware · Protects navigation and redirects unauthenticated users.
src/middleware.ts - Scoped API identity · Resolves account-scoped API access.
src/lib/auth/api-context.ts
CRM workspace
- Shared inbox · Conversation and contact workspace for agents.
src/app/(dashboard)/inbox/page.tsx - Message thread · Renders and operates on a conversation.
src/components/inbox/message-thread.tsx - Conversation service · Resolves messages to conversations and contacts.
src/lib/inbox/conversations.ts - Realtime presence · Keeps shared-agent inbox views current.
src/hooks/use-realtime.ts
Automation systems
- Automation engine · Validates and executes visual automations.
src/lib/automations/engine.ts - Automation scheduler · Advances scheduled and waiting automation work.
src/app/api/automations/cron/route.ts - Flow engine · Executes visual flow definitions and actions.
src/lib/flows/engine.ts - Flow builder · Creates and edits flow definitions.
src/components/flows/flow-builder.tsx - AI auto-reply · Creates constrained replies with human handoff.
src/lib/ai/auto-reply.ts
Integration boundary
- WhatsApp webhook · Verifies inbound Meta webhooks.
src/app/api/whatsapp/webhook/route.ts - Meta Cloud gateway · Handles outbound messaging and WhatsApp operations.
src/lib/whatsapp/meta-api.ts - Message sender · Sends outbound messages through Meta.
src/lib/whatsapp/send-message.ts - Versioned REST API · Exposes scoped CRM resources to integrations.
src/app/api/v1/contacts/route.ts - Revocable API keys · Authenticates external API clients.
src/lib/api-keys/keys.ts - Outbound webhooks · Signs deliveries and protects against SSRF.
src/lib/webhooks/deliver.ts - MCP server · Provides read-first AI-client access to the public API.
mcp-server/src/index.ts
Platform services
- Accounts & RLS · Membership, roles, invitations, and account isolation.
supabase/migrations/017_account_sharing.sql - Supabase platform · Provides persistence, identity, storage, RPCs, and realtime.
src/lib/supabase/server.ts - Media upload · Stores media through Supabase Storage.
src/lib/storage/upload-media.ts - Core schema · Authoritative baseline schema evolution.
supabase/migrations/001_initial_schema.sql
Other components
- Meta Cloud API
- OpenAI & Anthropic
Connections 24
- Auth middleware → App shell & routes: guards routes
- App shell & routes → Shared inbox: renders
- App shell & routes → Scoped API identity: serves APIs with
- Scoped API identity → Accounts & RLS: enforces account scope
- Accounts & RLS → Supabase platform: RLS policies on
- Core schema → Supabase platform: defines schema
- Shared inbox → Message thread: composes
- Message thread → Conversation service: uses
- Shared inbox → Realtime presence: subscribes through
- Conversation service → Supabase platform: reads and writes
- WhatsApp webhook → Conversation service: ingests inbound messages
- Message sender → Meta Cloud gateway: uses
- Meta Cloud gateway → Meta Cloud API: calls
- Automation engine → Message sender: sends actions through
- Automation scheduler → Automation engine: advances
- Flow builder → Flow engine: defines
- Flow engine → Message sender: sends actions through
- AI auto-reply → OpenAI & Anthropic: generates with
- AI auto-reply → Conversation service: uses conversation context
- Versioned REST API → Revocable API keys: authenticates with
- Versioned REST API → Conversation service: accesses CRM resources
- MCP server → Versioned REST API: uses
- Outbound webhooks → Supabase platform: reads event data
- Message sender → Media upload: uploads media through