A production-style FastAPI service for ingesting "WhatsApp-like" messages with exactly-once processing, HMAC validation, and observability.
- Ingestion:
/webhookendpoint accepts JSON messages with HMAC SHA256 signature validation. - Idempotency: Ensures exactly-once processing using message ID uniqueness.
- Retrieval:
/messagesendpoint with pagination and filtering. - Analytics:
/statsendpoint for simple message aggregations. - Observability:
/metrics: Prometheus-style metrics./health: Liveness and Readiness probes.- Structured JSON logging.
- Infrastructure: Docker, Docker Compose, SQLite.
- Docker and Docker Compose
- Make (optional)
To start the service and database:
make up
# OR
docker compose up -d --buildThe API will be available at http://localhost:8000.
Environment variables (defined in docker-compose.yml or .env):
DATABASE_URL: Connection string (default:sqlite:////data/app.db)WEBHOOK_SECRET: Secret key for HMAC validation (Required).LOG_LEVEL: Logging level (default:INFO).
Ingests a message.
- Header:
X-Signature: <hex HMAC-SHA256 of body> - Body: JSON with
message_id,from,to,ts,text.
List messages.
- Params:
limit,offset,from(filter by sender),since(filter by time),q(text search).
Returns message anayltics.
Health probes for Kubernetes/Orchestrators.
Prometheus metrics.
HMAC verification uses the standard libraries hmac and hashlib. The raw request body is read and hashed against the WEBHOOK_SECRET. Constant-time comparison (hmac.compare_digest) is used to prevent timing attacks.
Offset/Limit pagination is implemented using SQL OFFSET and LIMIT. Default limit is 50. Warning: Deep pagination with OFFSET can be slow on large datasets; cursor-based pagination would be better for high scale but OFFSET is sufficient for requirements.
We expose standard Prometheus metrics:
http_requests_total: Counter by status and path.webhook_requests_total: Counter by result (created, duplicate, etc.).request_latency_ms: Histogram of request duration.
Run tests locally:
# Install dependencies
pip install -r requirements.txt
# Run tests
WEBHOOK_SECRET=testsecret python -m pytest tests/ -vVSCode + Agentic Coding Assistant.