Architecture
The pipeline runs in three phases: gather, generate, and test.- The Gather step takes in a domain name and collects context about the brand and “design tokens” to set the LLM up to generate relevant and consistent designs.
- The Generate step calls an agent to generate the actual email template in HTML and inline CSS.
- The Test phase is just a bunch of pre-flight checks to ensure the email template will render correctly in the end user’s inbox.
Prerequisites
- A Context.dev API key. Grab one from the dashboard and export it as
CONTEXT_DEV_API_KEY. - An Anthropic API key for the generation step. Grab one from the Anthropic Console, export it as
ANTHROPIC_API_KEY, and install the Anthropic SDK (@anthropic-ai/sdk,anthropic, oranthropic-sdk-go). - The Context.dev SDK for your backend:
Step 1. Gather the brand context
These four APIs have all the context our agent needs:- Brand API: the brand profile.
logos[]: logo and icon variants, each withurl,type, andmodebackdrops[]: hero and background imagerylinks: standard page URLs (pricing,blog,login,signup,careers,contact,privacy,terms)title,description,slogan,socials[],address, andindustries
- Styleguide API: the homepage’s design tokens.
mode:lightordarkcolors:accent,background, andtexttypography:headings.h1toh4andp, each withfontFamily,fontFallbacks,fontSize,fontWeight, andlineHeightelementSpacing: anxstoxlspacing scaleshadows:smtoxlplusinnerbox-shadow valuescomponents:buttonandcard, each with ready-to-pastecssfontLinks: downloadable font files keyed by family
- Screenshot API: a hosted render of each page.
screenshot: a CDN URL for the captured PNGscreenshotType:viewportorfullPagewidthandheight: the captured dimensions
- Image Scraping API: each page’s image manifest.
images[]: every image on the page, each withsrc,element(img,svg,css,background, and more),type, andaltimages[].enrichment(optional):width,height,hostedUrl, andclassification
brand.links (pricing, blog, login, and the rest of the standard pages); running the screenshot and image scrape on a few of those too is useful extra context for the model, but optional.
Step 2. Generate the template with an LLM
Now, we feed the gathered context into an LLM call. We recommend Claude Opus 4.8 for visual design tasks like these. But you can experiment with models as you like. Here’s the system prompt we’ll be using. It includes a description of the schema of the design tokens we’re providing and some email HTML/CSS rendering best practices. Save it as a file namedsystem-prompt.txt.
System prompt for the email generator
Step 3. Test the rendered email
Email HTML/CSS is slightly harder to get right. That makes it very important to test before you send. This pipeline runs two tests:- Lints to find email-client compatibility issues (oversized HTML, script tags, flexbox/grid, missing image attributes)
- Rendered Preview to find aesthetic inconsistencies
Lint the code
Catch issues in code:Render across real clients
The standard way to verify how an email actually looks is a cross-client preview service like Litmus or Email on Acid. These services take in the HTML and send back real screenshots of how your email looks on popular email clients across Desktop and Mobile screen sizes. Litmus’s Instant API takes the HTML and hands back anemail_guid you then pull per-client screenshots from:
Full implementation
Here is the whole pipeline (gather, generate, lint) as one runnable script per language. Each one readsCONTEXT_DEV_API_KEY and ANTHROPIC_API_KEY from the environment, expects the system-prompt.txt from Step 2 alongside it, takes a domain and a brief as arguments, and writes email.html.
Every Context.dev call below was run against the live API, the Anthropic request shape was verified, and each file was type-checked or compiled before publishing. Run with, e.g.,
npx tsx branded-email.ts stripe.com "a welcome email".Related resources
Brand API
Logos, backdrops, slogans, socials, and standard page links from a domain.
Styleguide API
Typography, colors, spacing, shadows, and component CSS in one call.
Screenshot API
Capture the homepage and standard pages as hosted PNGs for visual reference.
Best Practices
Caching, error handling, and key hygiene across the API.