Next.js OG image
Next.js OG image: metadata file vs ImageResponse vs hosted API
A Next.js OG image can be a file in the route, JSX from ImageResponse and @vercel/og, or a CDN URL from a hosted API. This page helps Next.js and Vercel developers choose. Use the metadata file convention when Next.js should own the tags for one segment. Use ImageResponse when the layout is JSX you want in the app. Use OG Pilot when the same branded card has to work outside Next.js, off Vercel, from a template library, or from an agent. Nothing below invents a latency or price benchmark. Official limits are cited. Anything we have not measured is marked NEED VERIFY or NEED CITE.
Get an API key to sign CDN URLs, read the API docs for JWT claims and the JavaScript SDK, use MCP when an agent should generate the card, or generate one image before you wire production auth.
When to use the opengraph-image metadata file
Next.js App Router treats opengraph-image and twitter-image as file conventions. A static .jpg, .jpeg, .png, or .gif in the segment makes Next.js add og:image, og:image:type, og:image:width, and og:image:height. Alt text lives in opengraph-image.alt.txt or twitter-image.alt.txt. The build fails if a static opengraph-image is over 8MB or a static twitter-image is over 5MB. That path fits a marketing page, an about page, or a campaign with one designed card.
A code file, opengraph-image.js, .ts, or .tsx, default-exports a function that returns a Response. ImageResponse satisfies that return type. Optional exports are alt, size, and contentType. Generated images are statically optimized unless they use request-time APIs or uncached data. generateImageMetadata can return more than one image from the same file. Since Next.js 16, params is a Promise. The file convention shipped in v13.3.0. You do not also set openGraph.images for a file in the same segment, because Next.js already emits the tags. Reach for generateMetadata when the image URL is external.
When to use ImageResponse and @vercel/og
ImageResponse from next/og uses @vercel/og, Satori, and Resvg to convert JSX into PNG. The default size is 1200 by 630, which is also the size Vercel documents as the recommended OG image size. Only flexbox and a CSS subset work. display:grid does not. The bundle, including JSX, CSS, fonts, and images, must stay under 500KB. Font formats are ttf, otf, and woff, and ttf or otf are preferred. The supported CSS list is in the Satori documentation.
App Router projects already include @vercel/og, so you do not install it again. Other frameworks install the package. Vercel documents @vercel/og as compatible with any framework deployed on Vercel, and as supported on the Node.js runtime. Pages Router plus the Node.js runtime does not support return new Response with vercel/og. App Router with Node.js or the Edge runtime, and Pages Router with the Edge runtime, do. In production, @vercel/og sets cache-control to public, immutable, no-transform, max-age=31536000. Change the URL when the artwork must change, because that header marks the response immutable. If crawlers need to fetch a custom route such as /api/og, allow that path in robots.txt.
Use a colocated opengraph-image.tsx when the card belongs to that segment and Next.js should write the meta tags. Use a Route Handler such as app/api/og/route.tsx when many pages share one ImageResponse endpoint, then point generateMetadata openGraph.images and twitter.images at the absolute URL. Pass the title as a query parameter if the headline is the only thing that changes.
When a hosted OG image API wins
OG Pilot returns a CDN URL you place in og:image. Signing stays on your server. The JavaScript SDK reads OG_PILOT_API_KEY and OG_PILOT_DOMAIN, signs a JWT, and createImage returns the URL. buildPathFromNextProps fills the path claim from App Router props. Pass iat when you want the documented 24-hour cache window from that timestamp. Omit iat and the cache does not expire on its own. Social crawlers fetch the image from the CDN. That is the caching model when the site is not a single Vercel project.
Pick the hosted API when Rails, Astro, WordPress, Python, or PHP need the same templates, when editors should pick blog_post, product, or page instead of maintaining Satori CSS, and when an agent should call the remote MCP endpoint at https://ogpilot.com/mcp. Documented tools include generate_og_image, debug_open_graph, compress_image, and extract_color_palette. Connector JSON differs by client, so follow the MCP guide instead of guessing a config. Keep the API key out of client components. Prefer the SDK over hand-rolled JWT signing. The raw shape is POST https://ogpilot.com/api/v1/images with a JSON body field token.
Feature comparison
| Dimension | Metadata file | ImageResponse / @vercel/og | Hosted OG Pilot API |
|---|---|---|---|
| What you add | opengraph-image or twitter-image in the route segment | JSX returned from ImageResponse | A CDN URL from generateMetadata |
| Meta tags | Next.js writes og:image, type, width, height, and alt | The file convention writes them. A custom route does not. | You set openGraph.images and twitter.images |
| Caching | Statically optimized unless the file uses request-time APIs | Production cache-control public, immutable, no-transform, max-age=31536000 | CDN URL. Optional iat refreshes for 24 hours from that timestamp. |
| Other stacks | Next.js App Router only | Any framework deployed on Vercel. MCP is yours to build. | JS, Ruby, Python, PHP, WordPress, and MCP |
| Latency and cost | NEED VERIFY. NEED CITE from your host. | NEED VERIFY. NEED CITE from your function and bandwidth plan. | NEED VERIFY. NEED CITE. Plans are in billing after signup. |
Decision checklist
- One unchanging card for the segment? Add a static opengraph-image file and an alt text file.
- Card follows the route, and Next.js should write the tags? Use opengraph-image.tsx with ImageResponse.
- Many routes, one JSX layout, still on Next.js? Use a Route Handler and generateMetadata.
- More than one framework, a non-Vercel host, or an agent? Use the hosted API and MCP.
- Put an absolute og:image URL in the HTML, then re-scrape after the artwork changes.
Cold versus warm latency is NEED VERIFY on your own pages. A warm social cache is a different number from the first fetch. Cost per thousand images is NEED CITE from your host pricing page and from OG Pilot billing after signup. This page does not publish a round number. The engineering cost is the part a price table misses: Satori CSS, font subsetting, and the 500KB cap, versus a template you do not host.
After you choose, confirm the card in the Open Graph debugger. The same hosted API is what the Rails OG image guide and the Astro OG image guide use. WordPress sites can use the WordPress open graph plugin instead of generateMetadata. Sign up for an API key, install MCP, and read the docs before you hard-code a JWT.