Skip to content

Public v2 API

The v2 surface (/public/v2) is the worker-optimized read API consumed by the cms-cf-worker (Cloudflare Worker). It was designed in AISD-115 and built in AISD-116. It is mounted alongside v1 (/public/v1), which stays in place during rollout and is retired once v2 is live in production.

  • Routes: src/cms_api/public/routes/v2/ -- one file per endpoint, aggregated onto public_v2_app in routes/v2/__init__.py and mounted in main.py behind validate_worker_api_key.
  • Schemas: src/cms_api/public/schemas/v2.py -- every response model sets extra="forbid", so any drift between the store output and the declared schema fails fast in tests rather than silently shipping an unexpected field.
  • Auth: the whole block requires the worker API key (see Auth flow).

Endpoints

Every DB-backed endpoint emits ETag, Last-Modified, and Cache-Control, and honors If-None-Match / If-Modified-Since with a 304 (see Request lifecycle).

# Method + path Purpose s-maxage / swr
1 GET /public/v2/ping Liveness; no DB, no caching. Returns {"status": "ok", "time": <iso>}. --
2 GET /public/v2/sites/{site}/settings Site identity + merged settings + light categories/tags/authors + resolved templates + active_template + active_style + page_types + ads_txt/robots_txt. 300 / 86400
3 GET /public/v2/sites/{site_id}/pages/{slug} One article + flat SEO + author + breadcrumbs + related. ?version=N reads content_history (preview-gated). 300 / 600
4 GET /public/v2/sites/{site_id}/authors/{slug} Author profile + computed SEO + article stats. 300 / 600
5 GET /public/v2/sites/{site_id}/lists?kind=... Metadata-only content cards, cursor-paginated. 120 / 600
6 GET /public/v2/sites/{site_id}/sitemap Flagged content URLs + static-page URLs, cursor-paginated. 3600 / 3600
7 GET /public/v2/templates/{slug}/css A template's DB-stored stylesheet as text/css. edge (s-maxage=86400)
8 GET /public/v2/styles/{slug}/css A registry style's DB-stored stylesheet as text/css (AISD-133). edge (s-maxage=86400)
9 GET /public/v2/scripts/{name}?domain=... A scripts-registry row resolved for one site (its override, else the global row); raw=1 returns the file bytes. edge (s-maxage=86400)

Parameters by endpoint

/settings

Param In Notes
site path Site id, acronym, or domain (see the resolver).
language query Optional language filter.

Returns a SettingsBundle: site (SiteIdentity), site_settings (SiteSettingsBlob -- EAV merge of site over global defaults, with light categories/tags/authors, empty templates map, active_template, active_style, static *_html keys, and page_types), and meta. See Styles and templates for how active_style / active_template are resolved from site_settings.template_style_id.

Derived navigation and category slugs (AISD-229)

site_settings.navigation is not stored EAV. On every settings read it is derived from active site_categories (ascending sort_order), capped by effective nav_limit, as [{ "name", "slug" }, …] where slug = name.strip().lower().replace(" ", "-").

categories[] in the same blob uses the same slugify for slug (display name stays in name). List cards emit slugified category_slug so worker links like /exclusive-interviews/... stay consistent with nav hrefs.

Admin writes that include a navigation field are rejected with 422.

The active_style block is {id, slug, version, critical_css} (the id was added in AISD-168 so the worker can key off the style id; the CSS URL still uses slug/version). critical_css carries the style's above-the-fold CSS -- built by cms-cf-worker/scripts/generate-critical-css.mjs (penthouse) and stored on the style row -- so the worker can inline it instead of blocking on the stylesheet fetch. It defaults to "", so a style with no generated critical CSS, and any caller predating the field, keeps working unchanged. It comes from the selected template_styles pairing, not from a sites.style_id column (removed AISD-226). Missing/inactive pairing → 424 template_style_not_configured.

layout_customization (AISD-190)

Optional JSON blob on site_settings for config-driven layout preferences (consumed by the worker in a follow-up ticket under epic AISD-189). Stored as an EAV site_settings key, same path as navigation.

v1 shape (flat, site-wide only):

{
  "sidebar": {
    "position": "left",
    "editors_picks": { "visible": true },
    "categories": { "visible": true },
    "tags": { "visible": true }
  }
}
  • sidebar.position: left | right | none (optional).
  • Each of editors_picks / categories / tags may carry an optional visible boolean.
  • null / missing / {} means no customization (worker no-op).
  • Unknown keys are rejected with HTTP 422 on admin write (extra="forbid"). Page-type nesting (article, homepage, defaults) is deferred and also rejected in v1.
  • Admin write: PATCH /admin/v1/sites/{id}/settings and PATCH /admin/v1/global-settings with a layout_customization field.
  • Conceptual mapping to existing sidebar block settings: block show_categories / show_tags overlap with layout_customization.sidebar.categories.visible / .tags.visible. This ticket does not migrate block settings; which source wins is decided by the consuming worker ticket.

Static page HTML (*_html, AISD-227)

Optional long-text fields on site_settings for legal/footer pages:

Key Page
privacy_policy_html Privacy Policy
do_not_sell_html Do Not Sell
terms_of_use_html Terms of Use
about_html About
contact_us_html Contact Us
  • Merged like every other EAV key: per-site non-null override wins over the site_id=-1 default; the worker falls back to the S3 shell block only when the merged value is empty.
  • Admin write: PATCH /admin/v1/sites/{id}/settings or PATCH /admin/v1/global-settings. Values are capped at 65 000 UTF-8 bytes (under MySQL TEXT) and rejected with 422 when over — character count alone is not enough for utf8mb4. Prefer GET .../settings/overrides for edit forms so a full-object save does not pin the -1 default onto the site.
  • These keys add ~6 KB of HTML to the ETag'd /settings bundle; editing any one invalidates the whole settings response for that site.

/pages/{slug}

Param In Notes
site_id path Resolver input.
slug path Article slug.
language query Optional; a slug published in another language is skipped, not 404'd.
version query, >=1 Reads a content_history snapshot; preview-gated.
related_limit query, 0..50 Related-article count (default 6).
X-CMS-Preview header Shared secret required when version is set.

Only content_type in ARTICLE_CONTENT_TYPES (currently ("homepage_article",)) is served as an article. A versioned read keeps the live row's identity columns and overlays only the snapshot's mutable business columns (the HISTORY_BUSINESS_FIELDS merge policy in v2_common). Returns an ArticlePage.

/authors/{slug}

Param In Notes
site_id path Resolver input.
slug path Author slug (scoped to the site, active only).

Returns an AuthorProfile: the author slice, a computed Person SEO block, and stats (article_count, latest_published_at).

/lists

Param In Notes
site_id path Resolver input.
kind query, required One of homepage, category, author, featured, trending, related, recent, search.
category query Required when kind=category (alias: category_slug). Accepts the slugified URL key (exclusive-interviews) or the display name; matching is by strip().lower().replace(" ", "-") against ContentCategory.category (AISD-229).
category_slug query Alias for category.
author_slug query Required when kind=author.
related_to query Required when kind=related.
q query Required when kind=search (LIKE substring on title + description).
language query Optional language filter.
limit query, 1..100 Page size (default 20).
cursor query Opaque cursor; response carries next_cursor.

A missing required parameter for the chosen kind, or an unknown kind, raises a 400 validation_error. Returns a ListResponse of metadata-only ListItem cards.

kind=trending additionally carries site_acronym and scripts_split_config; every other kind leaves both null rather than growing the common list payload. The trending worker renders those pages itself (it reads the article from this API, and /trending/* is its route, not the CMS worker's), so it needs the owning site's acronym and the script traffic split -- carrying them on the feed it already calls saves it a second request to /settings per render.

/sitemap

Param In Notes
site_id path Resolver input.
since query RFC1123 or ISO date; only content published at/after it.
limit query, 1..50000 Page size (default 5000).
cursor query Opaque cursor. Static-page entries are emitted only on the first page (cursor unset).

Includes content flagged is_featured OR show_in_homepage, plus static-page URLs derived from site_settings.navigation whose path maps to a type='page' template element (the homepage / is always included). Returns a SitemapResponse.

Template and style CSS

Two endpoints serve DB-stored stylesheets as text/css, cached at the edge (s-maxage=86400):

  • GET /public/v2/templates/{slug}/css -- a template's stylesheet, keyed by the template slug.
  • GET /public/v2/styles/{slug}/css -- a registry style's stylesheet, keyed by the style slug (AISD-133). The /settings bundle's active_style.slug and active_style.version point the worker at the right style + cache-bust key (see Styles and templates).

Scripts by name + domain

GET /public/v2/scripts/{name}?domain=<host> resolves one scripts-registry row for a site: the site's own row wins over the global (site_id IS NULL) row of the same name, and only active rows are considered -- so deactivating an override falls back to the global row instead of 404ing. domain is the documented key; an id or acronym resolves too.

Param In Notes
name path The row's name (e.g. nwp-header.js). Not slug: slug is unique per row, so an override and the global row it replaces can only share name.
domain query Required -- without a site there is no override to resolve.
raw query 1 returns the file bytes instead of the JSON metadata.

The JSON body (ScriptResource) carries scope (site / global), version, definition, site_id, and url. A consumer decides from version whether its cached copy is stale and only then fetches the bytes from url -- which is this same route with raw=1, not an S3 link: the cms-scripts bucket is private, so a presigned URL would expire and could not be cached or stored.

Two distinct 404s: resource_not_found means no such site or script name, while script_file_missing (bytes path only) means the row exists but has no file uploaded yet. On the JSON path that same state is a 200 with url: null, so a consumer can tell "not uploaded yet" from "no such script".

Split percentages for these scripts ride on site_settings.scripts_split_config in the /settings bundle, and on the kind=trending list response.

The site resolver

All site-keyed v2 endpoints resolve the {site} / {site_id} path parameter through resolve_site_any: it tries int(site) against sites.id first, then falls back to the v1 acronym OR domain predicate. An inactive or unknown site yields a 404 resource_not_found.

Schema convention

Response shapes are declared in cms_api.public.schemas.v2 with model_config = ConfigDict(extra="forbid") on every model. Because the store populates these models directly, an extra or renamed field surfaces as a validation error in tests rather than silently shipping. See API reference / Public v2 schemas.

Recorded deviations

These were recorded against the AISD-115 design at implementation time:

  • Sync, not async. The design's asyncio.gather / async-SQLAlchemy language does not apply: the store is synchronous (session_scope() + session.query), so the v2 methods are synchronous def, mirroring v1.
  • feature_flags is an empty object. The current site_settings schema has no feature_flags column; the field is returned as {} until one exists.
  • meta.etag in the body is null. The authoritative ETag is the HTTP ETag header; computing it into the body would be self-referential.
  • search is a LIKE substring match, not MySQL MATCH ... AGAINST -- no fulltext index is declared in hq.sql_models.