Dynamic OG Images: How to Generate Social Previews Automatically
Learn how to generate dynamic OG images automatically for blogs, SaaS, and e-commerce. Covers API-based generation, Satori, headless Chrome, and edge rendering.
Quick Answer
Dynamic OG images are social preview images generated automatically from page data (title, description, author) instead of being manually designed. The three main approaches are: API-based generation (fastest, no infrastructure), Satori/Vercel Edge (self-hosted, React-based), and headless Chrome (most flexible, slowest).
What Are Dynamic OG Images?
A dynamic OG image is a social preview image generated programmatically from page data, rather than manually designed in a tool like Figma or Canva.
Instead of creating a unique image for every blog post, product page, or documentation page, you define a template once and generate images on the fly from the page title, description, author, date, or any other data.
This is essential for sites with dozens or hundreds of pages where manual design doesn't scale.
Three Approaches to Dynamic OG Images
1. API-Based Generation (Recommended)
Send your data to an API endpoint and receive a PNG image. No infrastructure to manage, no server-side rendering.
// Just set your og:image meta tag to:
https://ogimg.xyz/api/og?title=My+Blog+Post&template=gradient&pattern=dots
Pros: Fastest setup (minutes), no dependencies, edge-rendered in <200ms
Cons: Less customization than self-hosted solutions
Best for: Blogs, landing pages, SaaS products, documentation sites
2. Satori + @vercel/og (Self-Hosted)
Vercel's open-source library renders React/JSX components to SVG, then converts to PNG. Runs on Edge Runtime.
import { ImageResponse } from '@vercel/og';
export async function GET(request: Request) {
return new ImageResponse(
<div style={{ display: 'flex' }}>Hello World</div>,
{ width: 1200, height: 630 }
);
}
Pros: Full control over design, free, open source
Cons: Limited CSS support (no box-shadow, no position:absolute), must self-host
Best for: Developers who want full control and are already on Vercel
3. Headless Chrome / Puppeteer
Render a full HTML page in a headless browser and take a screenshot.
Pros: Any CSS/HTML works, pixel-perfect rendering
Cons: Slow (1-5 seconds per image), heavy infrastructure, expensive at scale
Best for: Complex designs that require full CSS support
Performance Comparison
| Approach | Latency | Setup Time | Cost | CSS Support |
|---|---|---|---|---|
| API (ogimg.xyz) | <200ms | 5 minutes | Free tier available | Template-based |
| Satori/@vercel/og | 50-200ms | 1-2 hours | Free (self-hosted) | Flexbox only |
| Headless Chrome | 1-5 seconds | 4-8 hours | Server costs | Full CSS |
Getting Started with Dynamic OG Images
The fastest way to add dynamic OG images to your site:
- Sign up at ogimg.xyz (free, no credit card)
- Get your API key from the dashboard
- Set your og:image meta tag to point to the API with your page data
- Test with Twitter Card Validator or Facebook Sharing Debugger
That's it — no npm packages, no build steps, no server infrastructure. Your OG images are generated on Vercel's Edge Network in under 200ms worldwide.
Frequently Asked Questions
Which approach is best for a Next.js blog?
For most Next.js blogs, an API-based solution like ogimg.xyz is the fastest to set up. If you want full control and are already on Vercel, @vercel/og is a good self-hosted alternative.
Do dynamic OG images hurt page performance?
No. OG images are only fetched by social media crawlers, not by regular visitors. They don't affect your page load time, Core Web Vitals, or Lighthouse score.
Can I cache dynamic OG images?
Yes. Most API-based solutions (including ogimg.xyz) cache generated images automatically. If you self-host with @vercel/og, add Cache-Control headers to your API route.
Related Guides
Compare with Alternatives
See how OG Image Generator stacks up against other tools.
Start Generating OG Images
Free API for developers. 50 images/month, 10 templates, no credit card required.