ScreenAPI

Guides · Lire en français

Generate dynamic Open Graph images for every page

Create a unique social sharing image (og:image) for every article or product from an HTML template. Works with Next.js, Laravel, Django or a static site.

When a link is shared on LinkedIn, X, Slack or WhatsApp, the preview shows the page's og:image. A unique image with the title of each article gets noticeably more clicks than the same logo everywhere. Designing them one by one does not scale; generating them does.

The idea in three steps

  1. Build one HTML page on your site that draws the card from URL parameters, for example /og-template?title=....
  2. Point a signed ScreenAPI URL at that page, at 1200 × 630 pixels.
  3. Put that URL in each page's og:image tag. The image is rendered on the first request and served from cache afterwards.

1. The template page

<!-- /og-template?title=... -->
<body style="margin:0;width:1200px;height:630px;display:flex;align-items:center;
  padding:80px;box-sizing:border-box;background:#0f1720;color:#fff;font:700 64px system-ui">
  <h1 id="t"></h1>
  <script>
    document.getElementById("t").textContent =
      new URLSearchParams(location.search).get("title");
  </script>
</body>

2. A signed image URL (Node.js)

A signed URL can go in a public page without exposing your API key: anyone can load it, no one can change it. The signing secret is in your dashboard.

import { createHmac } from "node:crypto";

export function ogImageUrl(title) {
  const query = new URLSearchParams({
    url: "https://example.com/og-template?title=" + encodeURIComponent(title),
    width: "1200",
    height: "630",
    format: "png",
    cache_ttl: "2592000", // 30 days: later views are free
    key_id: process.env.SCREENAPI_KEY_ID,
  }).toString();
  const signature = createHmac("sha256", process.env.SCREENAPI_SIGNING_SECRET)
    .update(query)
    .digest("hex");
  return `https://getscreenapi.com/api/v1/screenshot?${query}&signature=${signature}`;
}

3. The meta tags

<meta property="og:image" content="<%= ogImageUrl(post.title) %>" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />

Tips

  • Keep titles under about 70 characters on the card, or reduce the font size for long ones.
  • Use web fonts by absolute URL in the template; they load like on any page.
  • Test the result with LinkedIn Post Inspector or the Facebook Sharing Debugger, which also refresh their cache.
  • No template page? Send the HTML directly with POST and the html parameter, then store the image yourself. See HTML to image.

Each card costs one capture, then is served from cache for free.

Get a free key