Publish event, queued job, URL in the database.
In Laravel the queue is the natural home: publishing a post dispatches a job, the job calls the API and stores the URL. The page never waits on a render, and a failed render retries itself.
app/Jobs/GenerateOgImage.php
final class GenerateOgImage implements ShouldQueue
{
use Queueable;
public int $tries = 3;
public function __construct(private readonly int $postId) {}
public function handle(): void
{
$post = Post::findOrFail($this->postId);
$response = Http::withToken(config('services.imageapi.key'))
->timeout(20)
->post('https://imageapi.dev/v1/images?wait=true', [
'template' => 'blog-og',
'locale' => 'en',
'modifications' => [
'title' => $post->title,
'author' => $post->author->name,
'date' => $post->published_at->toDateString(),
'reading_time' => $post->reading_minutes,
'site' => config('app.domain'),
],
])
->throw();
$post->update(['og_image' => $response->json('url')]);
}
}
// Dispatch it on publish:
// GenerateOgImage::dispatch($post->id)->afterCommit();
`afterCommit()` matters here. Without it the job can start before the transaction that saved the post commits, and `findOrFail` blows up on a row that does not exist yet.
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.