---
title: "getOgImageUrl()"
description: "Build an OG image URL inside a Nitro server handler."
canonical_url: "https://nuxtseo.com/docs/og-image/nitro-api/get-og-image-url"
last_updated: "2026-09-23T06:59:59.086Z"
---

Use `getOgImageUrl()`{lang="ts"} to build the absolute OG image URL for a page from Nitro code, such as server routes and middleware. You don't need a Nuxt app context.

```ts [server/api/og-image.get.ts]
export default defineEventHandler((event) => {
  const url = getOgImageUrl(event, '/blog/hello', {
    component: 'OgImagePost',
    props: { title: 'My post' },
  })
  return { url }
})
```

## Syntax

```ts
function getOgImageUrl(event: H3Event, options?: Partial<OgImageOptions>): string
function getOgImageUrl(event: H3Event, pagePath: string, options?: Partial<OgImageOptions>): string
```

- `event`: The current `H3Event`. Always pass it. On Cloudflare Workers, `NUXT_OG_IMAGE_SECRET` is an env binding, and env bindings resolve only through the event.
- `pagePath`: The page to build the URL for, for example `/blog/hello`. If you omit it, the current request path is used, without the app `baseURL` or query.
- `options`: The same options as `defineOgImage()`{lang="ts"}. If you omit `component`, the first OG image component in your project is used.

If a secret is set, the URL is a signed `/_og/d/` URL. The secret comes from `NUXT_OG_IMAGE_SECRET` at runtime, or `security.secret` at build time.

The origin matches the page's `og:image`: your [site URL](/docs/site-config/getting-started/installation), or the request origin if you have not set one.

## Omitting the page path

::warning
In an API route, always pass `pagePath`. The request path there is the API route, so `getOgImageUrl(event)`{lang="ts"} in `server/api/og-image.get.ts` builds an image for `/api/og-image`.
::

In server middleware, skip requests that are not pages, such as assets and the `/_og/` image requests:

```ts [server/middleware/og-image.ts]
export default defineEventHandler((event) => {
  if (!getRequestHeader(event, 'accept')?.includes('text/html'))
    return
  // read later in your own server code, for example a Nitro render hook
  event.context.ogImageUrl = getOgImageUrl(event, { props: { title: 'My page' } })
})
```

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
