This is a small FastAPI backend that receives WhatsApp-like messages via a webhook, saves them into SQLite, and exposes a few endpoints to list them, see stats, and check health.
Requirements:
- Docker and Docker Compose installed
makeavailable
Start the app:
make upThe API will be available at:
To stop it:
make down-
POST /webhook
Receives a message JSON body.
RequiresX-Signatureheader with HMAC-SHA256 of the raw body usingWEBHOOK_SECRET.
On success returns:{"status": "ok"}. -
GET /messages
Lists stored messages with pagination and basic filters.
Query params:limit(default 50, max 100)offset(default 0)from(filter by sender)since(timestamp filter)q(search intext)
-
GET /stats
Returns total messages, number of senders, messages per sender (top 10), and first/last message timestamps. -
GET /health/live
Simple liveness check. -
GET /health/ready
Readiness check that only returns 200 when DB is reachable andWEBHOOK_SECRETis set. -
GET /metrics(optional but implemented)
Returns Prometheus-style metrics text with HTTP request counters and webhook outcome counters.
For /webhook, the app reads the raw request body bytes and computes an HMAC-SHA256 hex digest using the WEBHOOK_SECRET environment variable. It compares this to the X-Signature header using hmac.compare_digest. If it does not match, it returns 401 and does not insert anything into the database.
GET /messages supports limit and offset query parameters. limit defaults to 50 and is capped at 100. offset defaults to 0. Messages are always ordered by ts ascending and then message_id ascending. The response includes total, which is the total number of rows that match the filters, not just the number in the current page.
The /stats endpoint uses simple SQL queries to compute total messages, number of unique senders, messages per sender (top 10), and the first/last message timestamps.
The /metrics endpoint keeps in-memory counters (using Python dictionaries) that are updated on each request and then exposed in Prometheus text format, including a counter for total HTTP requests and a counter for webhook outcomes.
Developed with VSCode and Docker. Claude AI assistant was used occasionally to understand FastAPI, Docker, and HMAC, but the code and logic were implemented and understood by me.