Skip to content

Repository files navigation

Commune Backend

Open-source email infrastructure for AI agents. Receive, send, and process email programmatically with built-in security, encryption, and deliverability management.

Architecture · API Reference · Security

What This Does

Commune handles the full email lifecycle for AI agent systems:

  • Inbound processing — Receive email via Resend webhooks with spam detection, prompt injection analysis, attachment scanning, and structured data extraction
  • Outbound sending — Send email with automatic threading, recipient validation, suppression management, and RFC-compliant headers
  • Delivery tracking — Bounce, complaint, and delivery event processing with automatic suppression list management
  • Webhook forwarding — Deliver processed email to your endpoints with HMAC-SHA256 signatures, exponential backoff retry, and circuit breakers
  • Encryption at rest — AES-256-GCM field-level encryption for all email content, subjects, participants, and attachments
  • Real-time notifications — WebSocket push to connected dashboard clients

Prerequisites

  • Node.js 18+
  • MongoDB (local or hosted — e.g., MongoDB Atlas)
  • Resend account with an API key and at least one verified domain
  • Redis (optional — falls back to in-memory for rate limiting and caching)

Quick Start

1. Clone and install

cd backend
npm install

2. Configure environment

cp .env.example .env

Edit .env with your values. The minimum required variables:

# Database
MONGO_URL=mongodb://localhost:27017/commune
# Email provider
RESEND_API_KEY=re_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Authentication (generate a strong random string, ≥32 chars)
JWT_SECRET=your-secure-random-string-at-least-32-characters
# Encryption at rest (generate with: node -e "console.log(require('crypto').randomBytes(32).toString('hex'))")
EMAIL_ENCRYPTION_KEY=64-hex-character-key-here
# Public URL for webhook callbacks
PUBLIC_WEBHOOK_BASE_URL=https://your-backend-url.com
# Frontend URL (for email verification links)
FRONTEND_BASE_URL=http://localhost:3000

3. Run

# Development (with hot reload via ts-node)
npm run dev
# Production
npm run build
npm start

The server starts on port 8000 (configurable via PORT).

4. Verify

curl http://localhost:8000/health
# {"status":"ok"}

API Structure

The backend exposes two API surfaces:

Dashboard API (/api/*)

Used by the frontend dashboard. Authenticated via JWT (Bearer token from /auth/signin).

EndpointPurpose
POST /auth/signupCreate org + user, sends verification email
POST /auth/verifyVerify email with token
POST /auth/signinSign in, returns JWT
POST /api/domainsCreate a domain
POST /api/domains/:id/verifyVerify domain DNS records
POST /api/domains/:id/inboxesCreate an inbox on a domain
POST /api/email/sendSend an email
GET /api/messagesQuery messages

Public API (/v1/*)

Used by SDKs and external integrations. Authenticated via API key (Bearer token from key creation).

EndpointPurpose
POST /v1/messages/sendSend an email
GET /v1/threadsList conversation threads
GET /v1/threads/:id/messagesGet messages in a thread
GET /v1/domainsList domains
POST /v1/domains/:id/inboxesCreate an inbox
GET /v1/delivery/metricsDelivery statistics
GET /v1/delivery/eventsDelivery event history
GET /v1/searchSemantic search across messages

See docs/PUBLIC_API.md for the full API reference.

Domain Setup

  1. Create a domain: POST /api/domains with { "name": "mail.yourdomain.com" }
  2. The response includes DNS records (MX, SPF, DKIM) to add at your DNS provider
  3. Verify: POST /api/domains/:id/verify — checks DNS propagation
  4. Create an inbox: POST /api/domains/:id/inboxes with { "localPart": "support", "displayName": "Support Agent" }

Inbound email to support@mail.yourdomain.com will now be processed and forwarded to your webhook endpoint.

Webhook Integration

Configure a webhook on your inbox to receive processed emails:

POST /api/domains/:domainId/inboxes/:inboxId/webhook
{
"endpoint": "https://your-app.com/webhooks/email"
}

Every inbound email is delivered to your endpoint as a signed POST request with headers:

HeaderValue
x-commune-signaturev1={HMAC-SHA256(secret, "{timestamp}.{body}")}
x-commune-timestampUnix milliseconds
x-commune-delivery-idUnique delivery ID
x-commune-attemptAttempt number (1-8)

The payload includes the full message, attachments metadata, spam analysis, and prompt injection analysis. See docs/security/webhook-signatures.md for verification examples.

Structured Data Extraction

Extract structured JSON from emails using AI. Configure a JSON Schema on your inbox:

PUT /api/domains/:domainId/inboxes/:inboxId/extraction-schema
{
"name": "invoice_extraction",
"description": "Extract invoice details from emails",
"enabled": true,
"schema": {
"type": "object",
"properties": {
"invoiceNumber": { "type": "string" },
"amount": { "type": "number" },
"dueDate": { "type": "string" }
},
"required": ["invoiceNumber", "amount"]
}
}

Extracted data appears in the webhook payload under extractedData and in the stored message under metadata.extracted_data. Extraction is conversation-aware — it uses the full thread context for multi-turn email chains. Requires AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY, and AZURE_OPENAI_DEPLOYMENT in your environment.

Security

The security architecture is documented in detail in ARCHITECTURE.md. Key highlights:

  • Encryption at rest — AES-256-GCM with per-field encryption, key rotation support, and three-layer key guard (fingerprint lock, decryption canary, startup halt)
  • Inbound threat detection — 5-layer spam detection, prompt injection analysis (5 signal categories), ClamAV + heuristic attachment scanning
  • Deliverability protection — Per-org sending health circuit breaker (bounce/complaint rate monitoring), 14-day domain warmup schedule, automatic suppression list management
  • API security — Bcrypt-hashed API keys, JWT authentication, sliding-window rate limiting (Redis + Lua), audit logging with auto-expiry
  • Transport security — HSTS, security headers via Helmet, Svix webhook signature verification, HMAC-signed outbound webhooks with timing-safe comparison

See the docs/security/ directory for detailed documentation on each security subsystem.

Environment Variables

VariableRequiredDescription
MONGO_URLYesMongoDB connection string
RESEND_API_KEYYesResend API key
JWT_SECRETYes (prod)JWT signing secret (≥32 chars)
EMAIL_ENCRYPTION_KEYYes (prod)64 hex chars for AES-256-GCM
PUBLIC_WEBHOOK_BASE_URLYesPublic URL for Resend webhook callbacks
FRONTEND_BASE_URLYesFrontend URL for verification emails
REDIS_URLNoRedis for rate limiting (falls back to in-memory)
AZURE_OPENAI_ENDPOINTNoAzure OpenAI for structured extraction
AZURE_OPENAI_API_KEYNoAzure OpenAI key
AZURE_OPENAI_DEPLOYMENTNoAzure OpenAI deployment name
CLOUDINARY_CLOUD_NAMENoCloudinary for attachment storage
CLOUDINARY_API_KEYNoCloudinary API key
CLOUDINARY_API_SECRETNoCloudinary secret
CLAMAV_HOSTNoClamAV daemon for attachment scanning
QDRANT_URLNoQdrant for vector search

See .env.example for the complete list with descriptions.

Build

npm run build # Compiles TypeScript to dist/
npm start # Runs the compiled server

Ecosystem

PackageDescription
communeEmail & SMS infrastructure — self-hostable backend
commune-aiTypeScript/Node.js SDK
commune-pythonPython SDK
commune-mcpMCP server for Claude Desktop, Cursor, Windsurf
commune-cliCommand-line interface

License

Apache License 2.0 — see LICENSE.md.

About

Open-source email & SMS infrastructure for AI agents. Programmatic inboxes, BullMQ queuing, webhook delivery, prompt injection detection, vector search. Self-hostable.

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages