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

# FastAPI

## FastAPI

Generate a unique OG image for every page, or any on-demand graphic, from a single Imejis template. The pattern below uses a FastAPI **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 bytes. It uses `httpx` for a non-blocking request, and because the key is sent server-side it never reaches the client.

```python showLineNumbers title="main.py"
import os

import httpx
from fastapi import FastAPI, Response

app = FastAPI()


@app.get("/og")
async def og_image(title: str = "Untitled"):
    async with httpx.AsyncClient() as client:
        response = await client.post(
            f"https://render.imejis.io/v1/{os.environ['IMEJIS_DESIGN_ID']}",
            headers={
                "dma-api-key": os.environ["IMEJIS_API_KEY"],
                "Content-Type": "application/json",
            },
            json={"headline.text": title},
        )
    response.raise_for_status()

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

### 2. Point your metadata at it

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

```html showLineNumbers title="template.html"
<meta
  property="og:image"
  content="https://your-site.com/og?title=Hello%20from%20FastAPI"
/>
```

**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.
