Dynamic OG images / Nuxt
Dynamic OG images in Nuxt
Give every page of a Nuxt app its own social card: sign one image URL in the server route that loads the post and set it with useSeoMeta. The key stays in private runtimeConfig.
The editor is free and needs no signup. Saving a template and creating an API key take a free account.
useFetch, minus the surprises
?description=Keys%2C+dedupe+and+the+payload.&title=useFetch%2C+minus+the+surprises&s=...
Upgrading to Nuxt 4
?description=The+app%2F+folder+and+what+broke.&title=Upgrading+to+Nuxt+4&s=...
Smaller server routes
?description=Nitro+did+more+than+we+expected.&title=Smaller+server+routes&s=...
One template, three posts. Each card is a signed URL that carries the post's own title; the design never changes. Start from this design or pick another in the template gallery.
Nuxt apps usually get og:image from one static file, or from a module that renders images inside the app with its own fonts and templates. A signed URL needs neither: the card is designed once in the editor, and the server route that already loads your post also builds a URL with the post's title in it. The image is rendered on betterimage.io the first time a crawler asks for it and cached at the edge after that.
You need one package, one entry in runtimeConfig and a few lines in the route that loads your posts.
How it works
Design the template once
Colors, fonts, layout and logo live in a saved template in the editor.
Sign one URL per page
Your Nuxt code puts the page's title in the URL and signs it with your key.
Crawlers get the card
The first fetch renders the 1200x630 PNG; after that it is served from the edge cache.
-
1 Save a template and create an API key
Design the card in the editor and save it as a template with a free account. The template's slug, for example blog-card, is what the code below refers to. Then create a key under Account settings > API. Keys look like bi_<key_id>_<secret>: the key id appears in every image URL, the secret only signs them and never leaves your server.
-
2 Add the key to runtimeConfig
Keys at the top level of runtimeConfig are private: they exist on the server only. Leave the default empty and set the real value through the environment as NUXT_BETTERIMAGE_API_KEY, in .env locally and in your host's settings for production. Never put it under runtimeConfig.public.
// nuxt.config.ts export default defineNuxtConfig({ runtimeConfig: { // Private: only on the server. Set it with NUXT_BETTERIMAGE_API_KEY. betterimageApiKey: "", }, }); # .env NUXT_BETTERIMAGE_API_KEY=bi_<key_id>_<secret> npm install @betterimage/cards -
3 Sign the URL in the server route that loads the post
The page's script setup also runs in the browser on client-side navigation, where private runtimeConfig is not available, so the URL is signed in the server route. The route looks the post up itself rather than signing whatever title it is sent, so nobody can use it to put their own text on your cards.
// server/api/posts/[slug].get.ts import { parseApiKey, signedImageUrl } from "@betterimage/cards"; export default defineEventHandler(async (event) => { // getPost: your own loader in server/utils, auto-imported. const post = await getPost(getRouterParam(event, "slug")!); if (!post) throw createError({ statusCode: 404 }); const { betterimageApiKey } = useRuntimeConfig(event); const ogImage = await signedImageUrl({ ...parseApiKey(betterimageApiKey), template: "blog-card", fields: { title: post.title, description: post.excerpt }, }); return { ...post, ogImage }; }); -
4 Set the tags with useSeoMeta
useFetch runs during server rendering, so the tags are in the HTML that crawlers receive. The error line matters: see the gotchas below.
<!-- app/pages/blog/[slug].vue --> <script setup lang="ts"> const route = useRoute(); const { data: post, error } = await useFetch(`/api/posts/${route.params.slug}`); if (error.value) throw createError(error.value); useSeoMeta({ title: () => post.value?.title, ogImage: () => post.value?.ogImage, ogImageWidth: 1200, ogImageHeight: 630, twitterCard: "summary_large_image", twitterImage: () => post.value?.ogImage, }); </script> <template> <h1>{{ post?.title }}</h1> </template> -
5 Check what crawlers see
Build with npm run build, start .output/server/index.mjs with the key in the environment, open a post and view the page source. Once it is deployed, paste a post's URL into the link preview checker: it downloads the image the way X, Facebook, LinkedIn, Slack and Discord do and shows each preview.
Check a deployed page with the link preview checker. The full signing reference, with code for Node, Python, Ruby, Go and PHP, is in the API docs.
Things to watch for
The variable has to be NUXT_BETTERIMAGE_API_KEY
Nuxt only fills runtimeConfig.betterimageApiKey from NUXT_BETTERIMAGE_API_KEY. Set BETTERIMAGE_API_KEY instead and the value stays empty, so the route fails with: Not a betterimage.io API key: expected bi_<keyId>_<secret>.
Without the error line, a broken route renders a blank page with a 200
useFetch does not throw. When the route fails, data is null and the page renders with an empty title, no og:image and status 200, so a crawler stores a preview with no image. Throwing createError(error.value) turns that into a real error page and a 500, and a missing post into a 404.
One og:image per page
If a module or a layout already sets ogImage, remove it there so the signed URL is the only one.
Special characters are already handled
Titles like "C++ tips & tricks" are the classic way to break a signed URL. The package signs the raw text and encodes the query itself, so the title arrives on the card exactly as written.
Frequently asked questions
Which Nuxt versions does this work with?
The code was written and run on Nuxt 4, where pages live in app/pages. We have not tested Nuxt 3. runtimeConfig, useFetch, useSeoMeta and server routes exist there too, so it should work, with the page file in pages/ at the project root instead of app/pages.
Do I need an image module or Satori for this?
No. The image is rendered by betterimage.io from a template you designed in the editor, so there are no fonts or templates to ship inside your app. Your server route only builds the URL.
Does every page view cost a render?
No. A URL is rendered the first time something fetches it and then cached at the edge for a day, and cached fetches do not count against your quota. A new title makes a new URL, which renders once.
Can I use a different picture for every post?
Yes. Add image_url to fields with the post's cover photo. It is fetched server-side and drawn in the image slot of your template, keeping the template's crop framing.
What happens if the quota runs out?
The URL keeps answering with an image and a 200: the last card rendered for it, or a plain betterimage.io card for a URL never rendered before. Link previews keep working.
More guides
Give every page its own card
Design the template in the editor, save it, and the code above does the rest. Without a plan the cards carry a small watermark; an API plan removes it.