# wppconnect-team/wppconnect architecture

> Architecture diagram of the GitHub repository wppconnect-team/wppconnect, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wppconnect-team/wppconnect
- Repository: https://github.com/wppconnect-team/wppconnect

## Overview

##### Main Components
- Entry Point: Begin at src/index.ts, which acts as the bootstrapping module.
- API Layer: Under src/api, you have:
- Helpers: Utility functions (e.g., file-to-base64, encryption, etc.)
- Layers: Modules like business.layer.ts, catalog.layer.ts, community.layer.ts, etc. These abstract business logic and organize WhatsApp functionalities (e.g., sending messages, handling groups, contacts, etc.).
- Model: Data structures and enumerations that define WhatsApp entities (chat, contact, message, etc.).
- Whatsapp API module (src/api/whatsapp.ts) coordinating the API calls.
- Controllers: In src/controllers, you see modules for auth, browser, initializer, websocket, and welcome. These orchestrate the application flow (e.g., authentication, browser automation via Puppeteer, handling connections through websockets).
- Lib Layer (WAPI): Located in src/lib/wapi, it contains:
- Business functions, functions (sending messages, file upload/download, etc.)
- Helpers and serializer modules that process and format data.
- Listeners and event handlers for real-time events from WhatsApp.
- A webpack config for building or bundling the library.
- Config: Under src/config, configuration files for Puppeteer and other runtime options.
- Token Store: Modules (e.g., fileTokenStore, memoryTokenStore) in src/token-store that manage session tokens and validity.
- Tests & Types & Utils: Additional components that support testing, type definitions (for TypeScript), and utility functions like logging and sleep.

##### Relationships & Interactions
- The entry point (src/index.ts) initializes controllers (auth, browser, etc.) which in turn configure the Puppeteer browser session (from src/config/puppeteer.config.ts) and drive the API module.
- Controllers and config send commands to the API Layer (src/api) for actions (sending messages, reading chats, etc.), which then utilize various layers (business, catalog, etc.) and helpers to perform these functions.
- The Lib layer (wapi) abstracts lower-level interactions with WhatsApp Web using functions, listeners, and serializers. This ensures that API calls from controllers get translated into direct browser or network operations.
- The token store is integrated to provide session persistence, verifying and storing tokens that maintain the connection state.
- Utility modules are used across layers for logging, cryptography, and scheduling asynchronous calls like sleep.

##### Architectural Patterns & Principles
- Layered Architecture: There’s clear logical separation—controllers manage flow, API layers abstract functional groups, and the lib/wapi module encapsulates low-level WhatsApp Web interaction.
- Modularity: With separate directories for helpers, models, and functions, the system is built with modularity in mind.
- Event-Driven: Listeners in the lib layer suggest an event-based design for handling real-time WhatsApp messages and notifications.
- Configurable & Extensible: With ample configuration (Puppeteer settings, token management) and examples demonstrating various use cases, the architecture supports extensibility.

##### Including Specific Technologies & Frameworks
- Node.js and TypeScript (given the .ts files and type definitions).
- Puppeteer: Indicated by the puppeteer.config.ts file, for headless browser automation.
- GitHub Actions (workflows included for build, lint, publish) and other development tooling (ESLint, Prettier, Commitlint, Husky).
- Webpack is configured in the lib/wapi folder.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Point Layer
    subgraph "Entry Point Layer"
        A["Entry Point (src/index.ts)"]:::entry
    end

    %% Controller Layer
    subgraph "Controller Layer"
        B1["Auth"]:::controller
        B2["Browser"]:::controller
        B3["Initializer"]:::controller
        B4["Websocket"]:::controller
        B5["Welcome"]:::controller
    end

    %% API Layer
    subgraph "API Layer"
        C1["Helpers"]:::api
        C2["Business & Functional Layers"]:::api
        C3["Models & Enums"]:::api
        C4["WhatsApp API"]:::api
    end

    %% Lib/WAPI Layer
    subgraph "Lib/WAPI Layer"
        D["Lib/WAPI"]:::lib
    end

    %% Configuration and Token Store
    subgraph "Configuration & Token Store"
        E["Configuration (Puppeteer)"]:::config
        F["Token Store"]:::token
    end

    %% Utilities and Testing
    subgraph "Utilities & Testing"
        G["Utilities & Types"]:::utils
        H["Tests"]:::tests
    end

    %% Connections
    A -->|"initiates"| B1
    A -->|"initiates"| B2
    A -->|"initiates"| B3
    A -->|"initiates"| B4
    A -->|"initiates"| B5

    B1 -->|"orchestrates"| C1
    B2 -->|"orchestrates"| C2
    B3 -->|"loads config"| E
    B4 -->|"orchestrates"| C3
    B5 -->|"orchestrates"| C4

    C1 -->|"calls"| D
    C2 -->|"calls"| D
    C3 -->|"integrates"| F
    C4 -->|"calls"| D

    E -->|"manages session"| F

    %% Utilities and Testing support Lib/WAPI
    D ---|"utilizes"| G
    D ---|"tested_by"| H

    %% Click Events
    click A "https://github.com/wppconnect-team/wppconnect/blob/master/src/index.ts"
    click B1 "https://github.com/wppconnect-team/wppconnect/blob/master/src/controllers/auth.ts"
    click B2 "https://github.com/wppconnect-team/wppconnect/blob/master/src/controllers/browser.ts"
    click B3 "https://github.com/wppconnect-team/wppconnect/blob/master/src/controllers/initializer.ts"
    click B4 "https://github.com/wppconnect-team/wppconnect/blob/master/src/controllers/websocket.ts"
    click B5 "https://github.com/wppconnect-team/wppconnect/blob/master/src/controllers/welcome.ts"
    click C1 "https://github.com/wppconnect-team/wppconnect/tree/master/src/api/helpers"
    click C2 "https://github.com/wppconnect-team/wppconnect/tree/master/src/api/layers"
    click C3 "https://github.com/wppconnect-team/wppconnect/tree/master/src/api/model"
    click C4 "https://github.com/wppconnect-team/wppconnect/blob/master/src/api/whatsapp.ts"
    click D "https://github.com/wppconnect-team/wppconnect/tree/master/src/lib/wapi"
    click E "https://github.com/wppconnect-team/wppconnect/blob/master/src/config/puppeteer.config.ts"
    click F "https://github.com/wppconnect-team/wppconnect/tree/master/src/token-store"
    click G "https://github.com/wppconnect-team/wppconnect/tree/master/src/utils"
    click H "https://github.com/wppconnect-team/wppconnect/tree/master/src/tests"

    %% Styles
    classDef entry fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef controller fill:#A9DFBF,stroke:#196F3D,stroke-width:2px;
    classDef api fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef lib fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef config fill:#D7BDE2,stroke:#6C3483,stroke-width:2px;
    classDef token fill:#FAD7A0,stroke:#B9770E,stroke-width:2px;
    classDef utils fill:#D6EAF8,stroke:#117864,stroke-width:2px;
    classDef tests fill:#FADBD8,stroke:#873600,stroke-width:2px;
```
