Make a link preview image for every article with one API request
A link shared without an image is easy to scroll past. This guide shows the request that turns an article’s title, author and photo into a 1200 × 630 preview image, and where to put the result.
Published
What a link preview is
When someone pastes a link into LinkedIn, X, WhatsApp or Slack, the app shows a card with a title and an image. The image comes from a line in the page’s HTML called og:image. If the line is missing, the card has no image, or the app picks one for you.
The usual size is 1200 × 630 pixels. Making one by hand for every article does not last, so most sites either reuse one image for everything or skip it. A template fixes that: the design stays the same and the title, author and photo come from the article.
The template
This guide uses The Margin, an article share image from the gallery. Copy it to your workspace and change the name, colors and fonts to match your site. These are the parts you can fill in for each article:
| Field | Type | What it holds |
|---|---|---|
title | Text | The headline. Cut off after three lines. |
summary | Text | One or two sentences under the headline. Cut off after two lines. |
section | Text | The label in the corner, such as Work or Climate. |
author | Text | The author line, such as By Hannah Kessler. |
author_photo | Image | A web address of the author’s photo. |
read_time | Text | Such as 6 min read. |
photo | Image | A web address of the article’s main photo. |
publication | Text | The name of your site. |
background | Color | The background color. |
You only send the fields you want to change. Anything you leave out keeps the value saved in the template.
The request
You need two things from the dashboard: an API key, from API keys, and the template ID, from the menu on the template. Then send this:
curl -X POST "https://api.pixmato.com/v1/renders" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"template": "YOUR_TEMPLATE_ID",
"modifications": {
"section": "Work",
"title": "The four-day week, two years in",
"summary": "What 61 companies kept, what they dropped, and the one thing none of them expected.",
"author": "By Hannah Kessler",
"read_time": "6 min read",
"author_photo": "https://themargin.co/authors/hannah-kessler.jpg"
},
"output": { "format": "png" }
}'The reply arrives when the image is ready:
{
"id": "yxt8lnd64jubtahhxu1qgo7v",
"status": "completed",
"output_url": "https://storage.pixmato.com/workspaces/.../renders/yxt8lnd64jubtahhxu1qgo7v.png",
"duration_ms": 196,
"template": "YOUR_TEMPLATE_ID"
}output_url is the address of the image. Anyone with the address can open it, which is what a link preview needs.
Long titles
Article titles vary a lot in length, and you will not be looking at each image. This template cuts the title off at the end of a word after three lines and adds “…”. The summary does the same after two.
If you would rather show every word, change the title’s Fit to Shrink in the editor. Keep long text inside its box explains the choices.
Add it to the page
Put the address in the page’s <head>:
<meta property="og:image" content="https://storage.pixmato.com/workspaces/.../renders/yxt8lnd64jubtahhxu1qgo7v.png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />Apps keep a copy of the image the first time they see a link. If you change an article’s title later, make a new image and use its new address, so the apps fetch it again.
When to make the image
Make it once, when the article is published or its title changes, and save the address with the article. Do not make it each time the page is viewed: that uses a credit per view and slows the page down.
In JavaScript, with no extra packages:
async function previewImageFor(article) {
const response = await fetch('https://api.pixmato.com/v1/renders', {
method: 'POST',
headers: {
Authorization: 'Bearer ' + process.env.PIXMATO_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
template: process.env.PIXMATO_TEMPLATE_ID,
modifications: {
section: article.section,
title: article.title,
summary: article.summary,
author: 'By ' + article.author,
read_time: article.minutes + ' min read',
},
output: { format: 'png' },
}),
})
const result = await response.json()
if (!response.ok) throw new Error(result.error)
return result.output_url
}Three things to handle in real use:
- Slow renders. If an image takes longer than 30 seconds, the reply is
202with apoll_urlinstead of the image address. Ask that address again until the status iscompleted. - Many articles at once. A workspace can start 60 renders a minute. Over that, the reply is
429with aRetry-Afterheader that says how long to wait. - The API key. Keep it on your server. Never put it in code that runs in the browser.
Without code
If your site has an RSS feed, an automation can do the same job. New articles to share images has the steps for n8n, Make and Zapier.
Try it
Copy The Margin template to your workspace, open Test template, and choose the Use via API tab. It shows this request with your own template ID already filled in.


