Marketing newsletter console API
Console routes for newsletter series, editions, schedules, lifecycle, inputs, revisions, and publication attestation under /api/marketing.
What this API area is for
A newsletter series holds recurring publication work. Preparing a preview and producing an actual delivery are different states, and their route contracts should be read accordingly.
The marketing newsletter console API exposes read and write boundaries for weekly newsletter series tied to marketing campaigns. Routes live on a router with prefix /api/marketing. The routes module docstring says newsletter preview/read boundaries are here, and that migration and activation stay disabled.
Read routes return series configuration, paginated editions, single-edition detail, publication history, publication-operation events, and period rollups. Write routes adopt a series, change lifecycle, refresh edition inputs, approve revisions, cancel editions, attest publication operations, and preview or update schedules and lifecycle transitions. Several mutating handlers call require_console_write_trust before work proceeds. Handlers that call _require_schema raise HTTP 503 when schema_available is false; history and event reads use has_history_schema, and newsletter_series returns available: false without 503.
Routes
All paths are relative to /api/marketing.
| Method | Path | Permission | Handler | What it does |
|---|---|---|---|---|
| GET | /campaigns/{campaign_key}/newsletter | marketing.read | newsletter_series | Returns newsletter availability flags. With the schema present, generation and effects flags come from newsletter_generation_enabled and from newsletter_effects_enabled and marketing_publication_effects_enabled; with it absent, both flags are false. Optional series row plus schedule policy, and observed_at. |
| PUT | /campaigns/{campaign_key}/newsletter | marketing.write | save_newsletter_series | Requires write trust and verified schema; adopts a newsletter series for the campaign via adopt_series. |
| POST | /campaigns/{campaign_key}/newsletter/lifecycle | marketing.write | newsletter_lifecycle | Requires write trust and schema; resolves series for campaign, then applies change_lifecycle with app generation setting. |
| GET | /campaigns/{campaign_key}/editions | marketing.read | newsletter_editions | Requires schema; returns paginated editions via editions_page (limit, optional cursor query params). |
| GET | /editions/{edition_key} | marketing.read | newsletter_edition | Requires schema; returns edition detail via read_edition. |
| GET | /campaigns/{campaign_key}/publication-history | marketing.read | newsletter_publication_history | Requires shared publication history schema; paginated history via history_page. |
| POST | /editions/{edition_key}/inputs/refresh | marketing.write | newsletter_refresh_inputs | Requires write trust and schema; tenant-gated daily report capture, then returns manifest key and refreshed edition. |
| POST | /editions/{edition_key}/revisions/approve | marketing.write | newsletter_approve_revision | Requires write trust and schema; calls approve_revision. |
| GET | /publication-operations/{operation_key}/events | marketing.read | newsletter_publication_events | Requires shared publication history schema; paginated events via events_page. |
| POST | /editions/{edition_key}/cancel | marketing.write | newsletter_cancel_edition | Requires write trust and schema; cancels edition via cancel_edition. |
| POST | /publication-operations/{operation_key}/attest | marketing.write | newsletter_attest_publication | Requires write trust and schema; attests operation via attest_operation, JSON body fields from request model. |
| GET | /campaigns/{campaign_key}/newsletter/period | marketing.read | newsletter_period | Requires schema; period rollup via period_rollup (since, until query params). |
| POST | /campaigns/{campaign_key}/newsletter/schedule/preview | marketing.write | newsletter_preview_schedule | Requires write trust; schedule preview via preview_schedule after resolving series key. |
| PUT | /campaigns/{campaign_key}/newsletter/schedule | marketing.write | newsletter_update_schedule | Requires write trust; schedule update via update_schedule. |
| POST | /campaigns/{campaign_key}/newsletter/lifecycle/preview | marketing.write | newsletter_preview_lifecycle | Requires write trust; lifecycle preview via lifecycle_preview on an open DB connection. |
Request and response models
Handlers declare JSON request bodies where noted. Types are imported from recox.marketing_newsletter_contracts except where domain code in this repo shows additional fields.
Request bodies
| Model | Used on | Fields evidenced in module code |
|---|---|---|
AdoptSeries | PUT /campaigns/{campaign_key}/newsletter | adopt_series uses expected_version, idempotency_key, project_key, owner, and policy on AdoptSeries |
PreviewedLifecycle | POST .../newsletter/lifecycle | Passed to change_lifecycle as lifecycle command; domain reads idempotency_key, expected_version, action, optional preview_hash |
LifecycleCommand | POST .../newsletter/lifecycle/preview | The preview route passes LifecycleCommand to lifecycle_preview; change_lifecycle reads idempotency_key, expected_version, and action, and reads preview_hash only when that attribute is present |
VersionedCommand | POST /editions/{edition_key}/inputs/refresh | expected_version, idempotency_key (passed to capture_inputs) |
ApproveRevision | POST /editions/{edition_key}/revisions/approve | Body type only; forwarded to approve_revision |
CancelEdition | POST /editions/{edition_key}/cancel | idempotency_key, expected_version, reason |
AttestPublication | POST /publication-operations/{operation_key}/attest | expected_version, edition_revision_key, statement, evidence_ref, idempotency_key |
SchedulePreview | POST .../newsletter/schedule/preview | Body type only; forwarded to preview_schedule |
ScheduleUpdate | PUT /campaigns/{campaign_key}/newsletter/schedule | Body type only; forwarded to update_schedule |
SchedulePolicy (nested under adoption and schedule domain logic) includes at least: destination_account, planned_disposition, resume_missing, recovery_from, recovery_through, eligibility_time, deadline_offset_days, provider_mode, as validated in domain lifecycle and edition code.
Query parameters
| Route | Parameters |
|---|---|
newsletter_editions, newsletter_publication_history, newsletter_publication_events | limit (int, default 25), cursor (str | None) |
newsletter_period | since (str), until (str) |
Response shapes (where defined in handlers or domain)
| Handler | Response |
|---|---|
newsletter_series | dict with available, migration_applied, generation_enabled, effects_enabled, series (row dict plus policy when present, or None), observed_at |
save_newsletter_series | adopt_series result: series_key, version, lifecycle |
newsletter_lifecycle | change_lifecycle result: series_key, version, lifecycle |
newsletter_refresh_inputs | manifest_key (str), edition (read_edition dict) |
newsletter_cancel_edition | edition_key, version, canceled (true) |
newsletter_attest_publication | JSON object from attest_operation Pydantic model_dump(mode='json') |
Paginated reads (editions, publication-history, events) | dict from respective query helpers |
newsletter_edition | dict from read_edition |
newsletter_period | dict from period_rollup |
| Schedule and lifecycle preview/update | dict from control helpers |
Behaviour and errors
Schema and availability
_require_schema()connects to the app database and, whenschema_availableis false, raises 503 withdetail.codenewsletter_schema_unverifiedand a message that migrations are held pending verification.newsletter_seriesdoes not call_require_schemaon the hot path; if schema is missing it returnsavailable: falseand disables flags without 503.newsletter_publication_historyandnewsletter_publication_eventsrequirehas_history_schema; otherwise 503 with detailShared publication schema is not verified/applied.
Trust and tenant gates
Mutating routes listed in the route table call require_console_write_trust(request) first (schedule preview/update and lifecycle preview included; they do not call _require_schema in the handler body).
newsletter_refresh_inputs returns 404 with detail Daily AI reports are not available for this company when app.settings.tenant != 'recohut'. It also returns 404 Edition not found when the edition row is absent.
Domain errors mapped to HTTP
| Exception / condition | Status | Typical detail |
|---|---|---|
| Missing campaign (series GET) | 404 | Campaign not found |
| Missing series for campaign | 404 | Series not found |
LookupError from domain/query helpers | 404 | str(exc) |
NewsletterConflict | 409 | str(exc) |
PublicationTransitionError (attest) | 409 | str(exc) |
ValueError | 422 | str(exc) |
Representative domain conflict messages include idempotency mismatch, series already exists, campaign or series version drift, activation disabled pending verification, pending publication blocking cancel, and lifecycle preview hash mismatch when preview_hash is supplied.
Idempotency and versioning
Write paths that accept idempotency_key and expected_version defer to domain command recording (marketing_newsletter_command) for replay and optimistic concurrency, as implemented in adopt_series, change_lifecycle, cancel_edition, and capture_inputs.
How the permission model applies
Console route permissions are declared in route_roles.py. Each handler name maps to a permission string; gate refuses signed-in users whose role lacks that permission (403 with a denied error object). Unlisted endpoints also yield 403. The table is a ceiling: capabilities and handlers may narrow access further but not widen it.
For this module, read handlers require marketing.read and write handlers require marketing.write. When sign-in is off, route role checks follow the console boundary behaviour described in the permission module docstring (OS admin context).
Permissions for all fifteen handlers in this module are listed in ROUTE_PERMISSIONS under the marketing_newsletter_routes.* keys matching handler function names.
How is this page?
Last updated on