Skip to content

OG Images in Astro — Integration Guide

Add dynamic Open Graph images to your Astro site. Works with both static and SSR modes.

Terminal window
npm install @atypical-consulting/og-engine-sdk

Create src/pages/api/og.ts:

import type { APIRoute } from 'astro'
import { OGEngine } from '@atypical-consulting/og-engine-sdk'
const og = new OGEngine(import.meta.env.OG_ENGINE_KEY)
export const GET: APIRoute = async ({ url }) => {
const title = url.searchParams.get('title') ?? 'My Site'
const description = url.searchParams.get('description') ?? ''
const image = await og.render({
format: 'og',
title,
description,
style: { accent: '#38ef7d', font: 'Outfit', layout: 'left' },
})
return new Response(image, {
headers: {
'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=86400',
},
})
}
src/pages/blog/[slug].astro
---
const { slug } = Astro.params
const post = await getPost(slug)
const ogUrl = `/api/og?title=${encodeURIComponent(post.title)}&description=${encodeURIComponent(post.excerpt)}`
---
<html>
<head>
<meta property="og:image" content={ogUrl} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
</head>

For fully static sites, generate images during astro build:

scripts/generate-og.ts
import { OGEngine } from '@atypical-consulting/og-engine-sdk'
import { writeFile } from 'fs/promises'
import { getCollection } from 'astro:content'
const og = new OGEngine(import.meta.env.OG_ENGINE_KEY)
const posts = await getCollection('blog')
for (const post of posts) {
const image = await og.render({
format: 'og',
title: post.data.title,
description: post.data.description,
})
await writeFile(`public/og/${post.slug}.png`, image)
}

Add to .env:

OG_ENGINE_KEY=oge_sk_your_key_here

Access with import.meta.env.OG_ENGINE_KEY in server-side code.