opengraph-image.ts) or using Route Handlers.
Option 1: File Conventions (Recommended)
Next.js has built-in support for generating images usingopengraph-image.ts. This maps directly to OGify’s renderer.
1
Create opengraph-image.ts
Place this file in any route segment (e.g.,
app/opengraph-image.ts or app/posts/[slug]/opengraph-image.ts).While Next.js provides
ImageResponse, OGify returns a standard Buffer, which you can simply return in a standard Response object.Option 2: Route Handlers
If you need a dynamic endpoint (e.g.,/api/og?title=...) that enables you to generate images on demand for various pages, use a Route Handler.
1
Create the Route Handler
Create
app/api/og/route.ts:2
Use in Metadata