← Back to Home

Serverless Architecture

System Design

API

Architecting Empathy: A Serverless Affirmation API for Dynamic UIs

Architecting Empathy: A Serverless Affirmation API for Dynamic UIs

Delivering Daily Positivity via Google Sheets & Cloudflare Workers • Status: Production Ready • v2.1

Objective

Build a zero-friction, headless CMS that serves daily encouragement wherever a UI needs it. Google Sheets holds 500+ curated quotes across 7 categories; a platform-agnostic JSON payload makes that content available to Framer or any other frontend without coupling the database to the presentation layer.

Product Vision & Origin Story

The system started as a shared, collaborative tool for daily encouragement. My partner needed a reliable boost of positivity, and contributing content needed to feel as natural as editing a spreadsheet—not maintaining a database or learning a new publishing platform. That simple use case shaped a lean API built for ease of use.

I originally engineered this as a dedicated solution to give my partner a reliable, daily boost of encouragement.

Architecture & Data Flow

Database (Google Sheets)

A familiar, collaborative editing surface stores the curated affirmation library and categories. Published CSV keeps content management accessible without a custom admin interface.

Compute (Cloudflare Workers)

The edge worker fetches and parses the sheet, selects an affirmation, and returns a normalized JSON response. This layer decouples content storage from delivery and presentation.

Frontend (Framer)

The UI requests the endpoint and maps the response into its own typography and category treatment. The same contract can power other clients without changing the content source.

Data Parsing & Edge Case Handling

A naive comma split breaks when an affirmation contains punctuation inside a quoted CSV field. The parser identifies structural commas outside paired quotation marks, keeping the text intact before removing wrapping quotes and sanitizing the values.

CSV STRUCTURAL COMMA SPLIT

split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/)

The +2 array index offset accounts for the header row and the spreadsheet’s one-based row numbering. This maps each JSON ID directly to its Google Sheet row, so an editor can locate a quote without searching the entire library.

The Data Contract (Payload Schema)

The endpoint exposes a small, decoupled JSON contract. Consumers depend on stable field names—not spreadsheet columns or the worker’s internal parsing logic.

GET /api/fetch-affirmation?key=<API_KEY>

RESPONSE PAYLOAD (200 OK)

{ "status": "success", "data": { "id": "142", "affirmation": "I do enough, I have enough, I am enough.", "category": "Personal Power" } }

UI/UX Content Requirements

State 1: The Loading State (Network Latency)

Display a skeleton loader or gently pulsing text while the request resolves. Reserve space for the affirmation so the layout stays stable.

State 2: The Success State (Data Received)

Map data.affirmation to the primary typography and data.category to a secondary pill or eyebrow tag. Keep content separate from the surrounding interface styling.

State 3: The Error State (Graceful Degradation)

Catch 500 responses, timeouts, and failed requests. Render the fallback string “Keep pushing forward. Great things take time.” so the UI never breaks or leaves an empty content area.

Scope, Trade-Offs & Future Roadmap

Anti-Goals

Keep this a lean, single-utility service. Multi-tenant authentication, complex account management, and a custom CMS would add maintenance without improving the core daily encouragement experience.

Assumptions & Shortcomings

The parser assumes a stable sheet schema; renamed or reordered columns can break the contract. Fetching live CSV also introduces network latency and a dependency on the publishing source.

Improvement Opportunities

Cache sanitized responses in Cloudflare KV at the edge and invalidate them through a webhook when content changes. Add a sanitized submission form so new contributions can be reviewed without exposing the source sheet.