post.published webhook, image written back through the Admin API.
Ghost fires a webhook on publish. You receive it, generate the image and write it back as the post `og_image` through the Admin API — without touching the theme at all.
webhook.js
import GhostAdminAPI from '@tryghost/admin-api';
const ghost = new GhostAdminAPI({
url: process.env.GHOST_URL,
key: process.env.GHOST_ADMIN_KEY,
version: 'v5.0',
});
// Ghost calls this endpoint on the post.published event.
export default async function handler(req, res) {
const post = req.body.post.current;
const image = await fetch('https://imageapi.dev/v1/images?wait=true', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.IMAGE_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
template: 'blog-og',
modifications: {
title: post.title,
author: post.primary_author?.name ?? '',
site: new URL(process.env.GHOST_URL).host,
},
}),
}).then((r) => r.json());
await ghost.posts.edit({
id: post.id,
updated_at: post.updated_at, // Ghost requires this for version control
og_image: image.url,
});
res.status(204).end();
}
Ghost rejects an edit without a current `updated_at` — that is its guard against overwriting somebody else's changes. Pass the value from the webhook, not one you fetched a minute ago.
With ?wait=true the finished image arrives on the same connection — usually in about a second.
{
"id": "img_01m08s3ez1zcv28h335g9923bw",
"status": "completed",
"url": "https://cdn.imageapi.dev/renders/img_01m08s3ez1zcv28h335g9923bw.png",
"duration_ms": 912
}
A hundred images a month free, then $9 for two thousand. The same post rendered twice returns the existing URL and uses no quota — so rebuilding your site costs nothing.