How to Generate Open Graph Images Automatically

Stop designing OG images manually. Learn how to generate them dynamically for every page on your site.

đź“‹ Table of Contents

What Are Open Graph Images?

When you share a link on Twitter, Facebook, LinkedIn, or Slack, the platform fetches metadata from the page to display a rich preview. The most important piece of this preview is the Open Graph image (og:image).

This is the large image that accompanies your link. It's the first thing people see. A compelling OG image can dramatically increase your click-through rate (CTR).

<!-- Example OG Meta Tags -->
<meta property="og:title" content="My Awesome Blog Post">
<meta property="og:description" content="Learn something new today.">
<meta property="og:image" content="https://example.com/og-images/my-post.png">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">

Why OG Images Matter for SEO & Engagement

Studies show that posts with images get 2.3x more engagement than those without. But it's not just about having any image—it's about having a relevant, branded, and informative image.

The Problem with Manual Creation

Manually creating an OG image for every blog post is tedious and doesn't scale. Consider a site with 500 articles—that's 500 images to design, export, upload, and link. And if you want to update your brand's visual style? You'd have to redo them all.

The Dynamic Solution: Automated OG Image Generation

The modern approach is to generate OG images on-the-fly. Here's the concept:

  1. Create an HTML template for your OG image (e.g., a styled card with the title).
  2. For each page, inject the page's title and other metadata into this template.
  3. Take a screenshot of the rendered template at 1200x630 pixels.
  4. Serve this screenshot as the og:image.

This way, every page gets a unique, branded OG image automatically.

Implementation Guide

Here's a simple approach using a dedicated route and SnapAPI.

Step 1: Create an OG Image Template Page

Create a simple HTML page that takes query parameters and renders a styled card.

<!-- /og-template?title=My%20Post&author=John -->
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            width: 1200px;
            height: 630px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea, #764ba2);
            font-family: 'Inter', sans-serif;
            color: white;
        }
        .card {
            text-align: center;
            padding: 60px;
        }
        h1 { font-size: 64px; margin: 0; }
        p { font-size: 28px; opacity: 0.8; }
    </style>
</head>
<body>
    <div class="card">
        <h1>{{title}}</h1>
        <p>By {{author}}</p>
    </div>
</body>
</html>

Step 2: Screenshot the Template with SnapAPI

Now, use a screenshot API to capture this template page.

const ogImageUrl = `https://snapapi.alterra-solutions.com/v1/screenshot
    ?url=${encodeURIComponent('https://yoursite.com/og-template?title=My+Post')}
    &width=1200
    &height=630
    &api_key=YOUR_API_KEY`;

// Use this URL directly in your og:image meta tag
// The API returns the image binary, which can be cached.

Step 3: Use a CDN for Caching

To avoid regenerating the image on every request, put the screenshot API behind a CDN like Cloudflare. The CDN will cache the image, and you only pay for the first render.

đź’ˇ Pro Tip: Set your API response headers to include Cache-Control: max-age=86400 to cache the image for 24 hours.

Conclusion

Automating OG image generation saves time, ensures consistency, and scales effortlessly. With a simple HTML template and a screenshot API, you can give every page on your site a professional, shareable preview image.

Generate OG Images with SnapAPI

100 free screenshots per month. No credit card required.

Get Started Free →