Open Graph Image Generation: Automate OG Images with a Screenshot API
Build a dynamic OG image service that generates beautiful social media preview cards on demand — without image libraries or design files.
Why OG Images Matter
When someone shares a link on Twitter, LinkedIn, Slack, or iMessage, the platform fetches the og:image meta tag to display a preview card. Pages without a custom OG image fall back to a generic placeholder — or nothing at all. Personalized OG images showing the article title, author, and brand increase click-through rates significantly because they stand out in crowded social feeds.
The traditional approach is to pre-generate a static OG image at publish time using a design tool or image manipulation library like Sharp, Pillow, or ImageMagick. This works for blog posts but breaks down for dynamic content: user profile pages, product listings, search results, and any page where content changes frequently or is personalized per viewer.
The Screenshot API Approach
The most flexible OG image generation approach uses an HTML template rendered as a screenshot. You create a hidden page or route in your application that renders the OG card design — title, description, branding, avatar or thumbnail — then use SnapAPI to capture it as a 1200x630 JPEG. The screenshot becomes the OG image. No image library, no design file, no font embedding code.
// og-image API route (Node.js / Express)
app.get("/og", async (req, res) => {
const { title, author } = req.query;
// 1. Generate the OG card HTML page URL
const cardUrl = `https://your-app.com/og-card?title=${encodeURIComponent(title)}&author=${encodeURIComponent(author)}`;
// 2. Screenshot it at 1200x630
const params = new URLSearchParams({
url: cardUrl,
width: "1200",
height: "630",
format: "jpeg",
quality: "90",
full_page: "false",
});
const snap = await fetch(`https://api.snapapi.pics/v1/screenshot?${params}`, {
headers: { "X-Api-Key": process.env.SNAPAPI_KEY },
});
res.setHeader("Content-Type", "image/jpeg");
res.setHeader("Cache-Control", "public, max-age=86400");
snap.body.pipeTo(new WritableStream({
write(chunk) { res.write(chunk); },
close() { res.end(); },
}));
});
The /og-card route renders a minimal HTML page that displays the OG card design using CSS and your application data. It can include web fonts, gradients, custom artwork, and dynamic content. SnapAPI renders it exactly as a browser would, so every CSS feature works as expected.
Caching OG Images
Generating a screenshot on every OG image request is wasteful. Add a Redis or CDN cache layer keyed on the page identifier. On the first request, generate the screenshot and cache the image bytes with a 24-hour TTL. Subsequent requests for the same article OG image are served from cache in milliseconds without hitting SnapAPI at all.
For published content that rarely changes, you can pre-generate OG images at publish time — trigger a SnapAPI call in your CMS publish webhook and store the result in S3. Return the S3 URL in the og:image meta tag. This eliminates runtime latency entirely for static content.
Invalidating Social Media Caches
Twitter and Facebook cache OG images aggressively. After updating an OG image, use the platform debug tools to force re-crawling: Twitter Card Validator at cards-dev.twitter.com/validator, and Facebook Sharing Debugger at developers.facebook.com/tools/debug. For LinkedIn, use their Post Inspector tool. Scraping these tools programmatically is against their terms, so manual invalidation is required after significant design changes.
Sign up at snapapi.pics for 200 free captures per month to start building your OG image service. The screenshot endpoint supports the 1200x630 dimensions recommended by the Open Graph protocol and returns JPEG with configurable quality for fast social media loading.