|
| 1 | +# Introduction |
| 2 | + |
| 3 | +This API provides email automation. |
| 4 | + |
| 5 | +## Capabilities |
| 6 | + |
| 7 | +- Composing transactional mails, broadcasts, sequences and templates |
| 8 | +- Managing (CRUD) contacts, including tag/subscription/email-based segmentation |
| 9 | +- Sending emails (BullMQ + nodemailer), through a per-team ESP if configured |
| 10 | +- Automation based on events (tag added/removed, new subscriber, scheduled broadcasts) |
| 11 | +- Email open/click tracking and unsubscribe handling |
| 12 | +- Per-team ESP (email sending provider) configuration and test sends |
| 13 | +- Teams: an OAuth account can own/belong to any number of teams; every other |
| 14 | + resource (contacts, templates, sequences, ESP config, API keys) is scoped to |
| 15 | + a team, not the account — see "Teams" below. |
| 16 | + |
| 17 | +## Architecture |
| 18 | + |
| 19 | +- The API is protected by OAuth2 (PKCE, dynamic client registration, email-OTP |
| 20 | + passwordless login, JWT access/refresh tokens) — ported from |
| 21 | + [MediaLit's API](../../../medialit/apps/api/src/oauth). See |
| 22 | + `../../ARCHITECTURE.md` for the full mapping. |
| 23 | +- REST endpoints for contacts, templates, sequences/broadcasts — defined once |
| 24 | + as a `ts-rest` contract (`packages/api-contract`, `@sendlit/api-contract`), |
| 25 | + not hand-annotated per route. That contract is the single source of truth |
| 26 | + for request/response validation (`@ts-rest/express`, at runtime), the |
| 27 | + OpenAPI document (`@ts-rest/open-api`, see `src/swagger-generator.ts`), and |
| 28 | + `apps/web`'s typed client (`@ts-rest/core`) — so the docs can't describe a |
| 29 | + shape the server doesn't actually accept/return. Every `routes.ts` in this |
| 30 | + app is a *thin adapter*: it extracts `params`/`query`/`body` (already |
| 31 | + validated) and delegates to the unchanged, framework-agnostic functions in |
| 32 | + that domain's `queries.ts`, which holds all the actual business logic. If |
| 33 | + Express is ever swapped for something else, only these adapter files |
| 34 | + should need to change. |
| 35 | +- Two always-running loops (`src/automation/start.ts`): |
| 36 | + - `process-rules.ts` — fires date-scheduled broadcasts |
| 37 | + - `process-ongoing-sequences.ts` — polls due sequence/broadcast deliveries and |
| 38 | + hands them to a BullMQ queue, processed by `process-ongoing-sequence.ts` |
| 39 | + (renders the email, adds an open-tracking pixel + click-tracked links, |
| 40 | + sends it, and schedules the next email) |
| 41 | +- Tag/subscriber-added triggers are event-driven (`automation/fire-event.ts`), |
| 42 | + called directly from the contacts routes instead of being polled. |
| 43 | +- An OAuth/API-key-protected MCP server (`POST /mcp`), ported from |
| 44 | + [MediaLit's MCP server](../../../medialit/apps/api/src/mcp) — exposes the same |
| 45 | + contacts/templates/sequences/ESP/team capabilities as the REST API as MCP |
| 46 | + tools (see `src/mcp/tools/*`). |
| 47 | +- Each team can configure its own ESP (`src/esp/*`): any provider that |
| 48 | + exposes an SMTP relay works (SendGrid, Mailgun, Postmark, SES, Resend, or a |
| 49 | + custom server). Credentials are encrypted at rest |
| 50 | + (`src/utils/secret-crypto.ts`, AES-256-GCM) and never returned to clients. |
| 51 | + `src/mail/transport.ts` resolves the right transporter per team, falling |
| 52 | + back to the platform's `EMAIL_HOST`/etc when a team hasn't configured one. |
| 53 | + |
| 54 | +### Teams |
| 55 | + |
| 56 | +- An `account` (`src/account/*`) is purely a login identity (one email = one |
| 57 | + account, OTP-based). Every other resource is scoped by `teamId` |
| 58 | + (`src/team/*`), not `accountId` — a team is the actual tenant/data-scope, |
| 59 | + and holds its own sending identity (from name/email, mailing address) and |
| 60 | + mail quota. |
| 61 | +- Every account gets a default team on creation, and can create as many more |
| 62 | + as it wants (`POST /teams`). An account can belong to several teams |
| 63 | + (`team_members`, currently always with role `owner` — member invitations |
| 64 | + are a follow-up). |
| 65 | +- **API keys are team-scoped, not account-scoped** — a team can hold several, |
| 66 | + independently named/revocable keys (`src/apikey/*`, `POST/GET/DELETE |
| 67 | + /teams/:teamId/keys`). A key always resolves to exactly one team; there's no |
| 68 | + ambiguity for API/MCP clients authenticated this way. |
| 69 | +- **OAuth-authenticated (browser) requests** resolve their team from an |
| 70 | + explicit `X-Sendlit-Team-Id` header, validated against team membership on |
| 71 | + every call (`src/auth/require-team.ts`) — this is what lets the web |
| 72 | + dashboard switch teams instantly, without re-authenticating. If the header |
| 73 | + is omitted and the account belongs to exactly one team, that team is used |
| 74 | + automatically. |
| 75 | +- `POST /provisioning/teams` is a separate, secret-guarded, server-to-server |
| 76 | + endpoint for multi-tenant consumers (e.g. CourseLit provisioning one SendLit |
| 77 | + team per one of its own tenants) to find-or-create a team at any point after |
| 78 | + both stacks have booted, keyed by a consumer-supplied `externalId` rather |
| 79 | + than email (a consumer's own tenants may share an owner email) — see |
| 80 | + `src/provisioning/routes.ts`. |
| 81 | +- `SUPER_ADMIN_EMAIL` is a *different*, boot-time-only convenience (mirrors |
| 82 | + MediaLit's admin-bootstrap script): on startup, if set and no account exists |
| 83 | + for it yet, creates one (with its default team + key) and logs the key once |
| 84 | + — useful for local dev/self-hosting, not for provisioning a multi-tenant |
| 85 | + consumer's many tenants over time. |
| 86 | + |
| 87 | +See the root [`ARCHITECTURE.md`](../../ARCHITECTURE.md) for what has been |
| 88 | +ported from CourseLit/MediaLit so far and what's still on the roadmap. |
| 89 | + |
| 90 | +## Environment variables |
| 91 | + |
| 92 | +See `.env.example`. |
| 93 | + |
| 94 | +## Running the app |
| 95 | + |
| 96 | +1. Start Postgres and Redis (e.g. via Docker). |
| 97 | +2. Copy `.env.example` to `.env` and fill in the values. |
| 98 | +3. Push the schema to your database: `pnpm --filter @sendlit/api db:push` |
| 99 | +4. Start the server: `pnpm --filter @sendlit/api dev` |
| 100 | + |
| 101 | +The API listens on `PORT` (default `80`) and exposes: |
| 102 | + |
| 103 | +- `GET /health` — liveness check |
| 104 | +- `GET /openapi.json`, `GET /docs` — OpenAPI spec / Swagger UI |
| 105 | +- `GET /.well-known/oauth-authorization-server` — OAuth2 metadata |
| 106 | +- `GET/POST/PATCH/DELETE /teams`, `/teams/:teamId` — team management |
| 107 | + (OAuth-authenticated only) |
| 108 | +- `GET/POST/DELETE /teams/:teamId/keys` — API keys for a team |
| 109 | +- `POST /contacts`, `GET /contacts`, ... — contacts |
| 110 | +- `POST /templates`, `GET /templates`, ... — email templates |
| 111 | +- `POST /sequences`, `GET /sequences`, ... — broadcasts & sequences |
| 112 | +- `GET /track/open`, `GET /track/click`, `GET /unsubscribe/:token` — tracking |
| 113 | +- `GET/PUT/DELETE /esp-config`, `POST /esp-config/test` — per-team ESP config |
| 114 | +- `POST /provisioning/teams` — server-to-server team provisioning for |
| 115 | + multi-tenant consumers (guarded by `X-Sendlit-Provisioning-Secret`, not |
| 116 | + OAuth/API-key auth) |
| 117 | +- `POST /mcp` — MCP server (JSON-RPC over HTTP, `Mcp-Session-Id` header for |
| 118 | + session continuation); authenticate the same way as REST (`Authorization: |
| 119 | + Bearer <token>` or `x-sendlit-apikey`) |
0 commit comments