# santosl2/go-api-whatsmeow architecture

> Architecture diagram of the GitHub repository santosl2/go-api-whatsmeow, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/santosl2/go-api-whatsmeow
- Repository: https://github.com/santosl2/go-api-whatsmeow

## Overview

#### Purpose and Entry Point

A compact Go HTTP API that exposes WhatsApp operations through the `whatsmeow` library. The process starts in `main.go`, which is responsible for assembling infrastructure, WhatsApp connectivity, and HTTP handlers.

Primary entry point:
- `main.go`

#### Application Composition

The repository is organized around two service adapters and an instance-oriented HTTP boundary:

- SQL-backed persistence adapter: `pkg/services/sqlstore.go`
- WhatsApp/whatsmeow integration adapter: `pkg/services/whatsmeow.go`
- Instance HTTP handler: `pkg/instances/handler/instances_handler.go`

`main.go` is the composition root: it should create concrete services and bind handler routes rather than embedding business logic itself.

#### WhatsApp Integration Boundary

`pkg/services/whatsmeow.go` encapsulates communication with the external WhatsApp network via the third-party `whatsmeow` Go library. This is the material external-service boundary and likely owns client lifecycle concerns such as connection, authentication/session restoration, event handling, and message transport.

Paths:
- `pkg/services/whatsmeow.go`
- `go.mod`

HTTP handlers should depend on this service boundary rather than invoke `whatsmeow` directly.

#### Persistent Session / Device Storage

`pkg/services/sqlstore.go` provides the SQL persistence boundary, likely used by `whatsmeow` to retain device identity, login state, and session material across process restarts. It separates database details from API and WhatsApp behavior.

Paths:
- `pkg/services/sqlstore.go`
- `go.mod`

The database is infrastructure required for durable WhatsApp client state, not an API-facing domain store unless additional behavior is added.

#### Instance API Layer

The `instances` package models the application’s primary API concept: a WhatsApp instance. The handler package translates HTTP requests into instance lifecycle actions and returns HTTP responses.

Paths:
- `pkg/instances/handler/instances_handler.go`
- `pkg/instances`

Likely control flow: HTTP request → instance handler → WhatsApp service and/or SQL-backed session store → response. This is the main boundary between callers and integration infrastructure.

#### Runtime and External Interfaces

Runtime: a single Go service/module, defined by:
- `go.mod`
- `main.go`

External interfaces:
- HTTP API, with example/manual requests in `client.http`
- WhatsApp protocol/network through `whatsmeow`
- SQL database through the store adapter

The process is not structured as a frontend application; clients interact over HTTP while the service manages WhatsApp client sessions internally.

#### Architectural Constraints

Keep transport-specific behavior in `pkg/instances/handler/instances_handler.go`, WhatsApp protocol behavior in `pkg/services/whatsmeow.go`, and persistence concerns in `pkg/services/sqlstore.go`. New instance operations should preserve this direction of dependency: entrypoint/handler orchestration → service adapters → external WhatsApp and SQL systems.

Main entry points: [`pkg/instances/handler/instances_handler.go`](https://github.com/santosl2/go-api-whatsmeow/blob/main/pkg/instances/handler/instances_handler.go) (Instance Handler), [`pkg/services/whatsmeow.go`](https://github.com/santosl2/go-api-whatsmeow/blob/main/pkg/services/whatsmeow.go) (Whatsmeow Service), [`main.go`](https://github.com/santosl2/go-api-whatsmeow/blob/main/main.go) (Composition Root), [`pkg/services/sqlstore.go`](https://github.com/santosl2/go-api-whatsmeow/blob/main/pkg/services/sqlstore.go) (SQL Store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Go Service"]
  node_main{{"Composition Root<br/>Go entrypoint<br/>[main.go]"}}
  node_instance_handler["Instance Handler<br/>HTTP handler"]
  node_instances["Instances<br/>API concept"]
  node_go_module["Go Module<br/>dependencies<br/>[go.mod]"]
  node_manual_client["Request Examples<br/>HTTP client file<br/>[client.http]"]
end

subgraph group_adapters["Infrastructure Adapters"]
  node_whatsmeow_service["Whatsmeow Service<br/>WhatsApp adapter<br/>[whatsmeow.go]"]
  node_sqlstore[("SQL Store<br/>persistence adapter<br/>[sqlstore.go]")]
end

subgraph group_external["External Systems"]
  node_http_clients(("HTTP Clients<br/>API callers"))
  node_whatsapp_network(("WhatsApp Network<br/>external protocol"))
  node_sql_database[("SQL Database<br/>session storage")]
end

node_main -->|"binds routes"| node_instance_handler
node_main -->|"constructs"| node_whatsmeow_service
node_main -->|"constructs"| node_sqlstore
node_main -.->|"uses dependencies"| node_go_module
node_http_clients -->|"HTTP requests"| node_instance_handler
node_manual_client -.->|"example requests"| node_instance_handler
node_instance_handler -->|"handles instances"| node_instances
node_instance_handler -->|"orchestrates"| node_whatsmeow_service
node_instance_handler -.->|"accesses session state"| node_sqlstore
node_whatsmeow_service -->|"persists sessions"| node_sqlstore
node_whatsmeow_service -->|"WhatsApp protocol"| node_whatsapp_network
node_sqlstore -->|"stores state"| node_sql_database
node_go_module -.->|"provides whatsmeow"| node_whatsmeow_service

click node_main "https://github.com/santosl2/go-api-whatsmeow/blob/main/main.go"
click node_instance_handler "https://github.com/santosl2/go-api-whatsmeow/blob/main/pkg/instances/handler/instances_handler.go"
click node_instances "https://github.com/santosl2/go-api-whatsmeow/tree/main/pkg/instances"
click node_whatsmeow_service "https://github.com/santosl2/go-api-whatsmeow/blob/main/pkg/services/whatsmeow.go"
click node_sqlstore "https://github.com/santosl2/go-api-whatsmeow/blob/main/pkg/services/sqlstore.go"
click node_go_module "https://github.com/santosl2/go-api-whatsmeow/blob/main/go.mod"
click node_manual_client "https://github.com/santosl2/go-api-whatsmeow/blob/main/client.http"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_instance_handler,node_instances,node_go_module,node_manual_client toneBlue
class node_whatsmeow_service,node_sqlstore toneAmber
class node_http_clients,node_whatsapp_network,node_sql_database toneMint
```

## Components

### Go Service

- [Composition Root](https://github.com/santosl2/go-api-whatsmeow/blob/main/main.go): Go entrypoint · Assembles services and binds HTTP routes. · `main.go`
- [Instance Handler](https://github.com/santosl2/go-api-whatsmeow/blob/main/pkg/instances/handler/instances_handler.go): HTTP handler · Translates instance API requests into lifecycle actions. · `pkg/instances/handler/instances_handler.go`
- [Instances](https://github.com/santosl2/go-api-whatsmeow/tree/main/pkg/instances): API concept · Primary WhatsApp instance domain boundary. · `pkg/instances`
- [Go Module](https://github.com/santosl2/go-api-whatsmeow/blob/main/go.mod): dependencies · Defines the service module and third-party dependencies. · `go.mod`
- [Request Examples](https://github.com/santosl2/go-api-whatsmeow/blob/main/client.http): HTTP client file · `client.http`

### Infrastructure Adapters

- [Whatsmeow Service](https://github.com/santosl2/go-api-whatsmeow/blob/main/pkg/services/whatsmeow.go): WhatsApp adapter · Owns WhatsApp client lifecycle and protocol transport. · `pkg/services/whatsmeow.go`
- [SQL Store](https://github.com/santosl2/go-api-whatsmeow/blob/main/pkg/services/sqlstore.go): persistence adapter · Persists device identity and WhatsApp session state. · `pkg/services/sqlstore.go`

### External Systems

- **HTTP Clients**: API callers
- **WhatsApp Network**: external protocol
- **SQL Database**: session storage

## Connections

- Composition Root → Instance Handler: binds routes
- Composition Root → Whatsmeow Service: constructs
- Composition Root → SQL Store: constructs
- Composition Root → Go Module: uses dependencies
- HTTP Clients → Instance Handler: HTTP requests
- Request Examples → Instance Handler: example requests
- Instance Handler → Instances: handles instances
- Instance Handler → Whatsmeow Service: orchestrates
- Instance Handler → SQL Store: accesses session state
- Whatsmeow Service → SQL Store: persists sessions
- Whatsmeow Service → WhatsApp Network: WhatsApp protocol
- SQL Store → SQL Database: stores state
- Go Module → Whatsmeow Service: provides whatsmeow
