---
title: "Astro"
description: "Generate dynamic OG images and on-demand graphics in Astro with the Imejis.io API. A secure endpoint pattern that keeps your API key server-side and returns a cached image."
url: "https://www.imejis.io/apis/astro"
published: "2026-07-27"
---

# Astro

## Astro

Generate a unique OG image for every page, or any on-demand graphic, from a single Imejis template. The pattern below uses an Astro **API endpoint** as a thin proxy: your API key stays on the server, and the browser (and social crawlers) only ever see your own URL.

Set two environment variables first: `IMEJIS_API_KEY` (from your dashboard) and `IMEJIS_DESIGN_ID` (any design's ID).

```bash title=".env"
IMEJIS_API_KEY=your_api_key
IMEJIS_DESIGN_ID=your_design_id
```

### 1. An endpoint that renders the image

This endpoint takes a `title` query param, POSTs it to Imejis as a `component.property` override, and returns the finished image. Because the key is sent server-side, it never reaches the client.

```typescript showLineNumbers title="src/pages/og.ts"
import type { APIRoute } from "astro"

export const GET: APIRoute = async ({ url }) => {
  const title = url.searchParams.get("title") ?? "Untitled"

  const res = await fetch(
    `https://render.imejis.io/v1/${import.meta.env.IMEJIS_DESIGN_ID}`,
    {
      method: "POST",
      headers: {
        "dma-api-key": import.meta.env.IMEJIS_API_KEY,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ "headline.text": title }),
    }
  )

  return new Response(res.body, {
    headers: {
      "Content-Type": "image/jpeg",
      // Renders are deterministic, so cache aggressively at the edge/CDN.
      "Cache-Control": "public, immutable, no-transform, max-age=31536000",
    },
  })
}
```

### 2. Point your metadata at it

Reference the endpoint from your layout's `<head>`, passing each page's real data. Social platforms fetch `/og?title=…` from your own domain, no key exposed.

```astro showLineNumbers title="src/layouts/Layout.astro"
---
const { title } = Astro.props
const ogImage = `https://your-site.com/og?title=${encodeURIComponent(title)}`
---
<head>
  <title>{title}</title>
  <meta property="og:image" content={ogImage} />
  <meta name="twitter:card" content="summary_large_image" />
  <meta name="twitter:image" content={ogImage} />
</head>
```

**Override any field.** Each dynamic component exposes keys like `headline.text`, `title.color`, or `image.image`. Send only the ones you want to change. A design's overridable fields are listed in its Share / API panel.
