Add OG image generation to any FastAPI route with Python's standard urllib. Works with Jinja2 templates.
Start IntegratingFull async/await support. Integrate seamlessly with your async database queries.
Works with Pydantic models for automatic serialization and validation.
Pass the OG URL to your Jinja2 templates. Works with FastAPI's TemplateResponse.
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from urllib.parse import urlencode
import os
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get("/blog/{slug}")
async def blog_post(slug: str, request: Request):
post = await get_post(slug)
og_url = "https://imagoapi.com/api/og/generate?" + urlencode({
"title": post.title,
"template": "modern",
"api_key": os.environ["IMAGO_API_KEY"],
})
return templates.TemplateResponse("post.html", {
"request": request,
"post": post,
"og_url": og_url
})
# post.html: <meta property="og:image" content="{{ og_url }}">
Start using Imago API today and add beautiful social cards to your FastAPI app.
Try Imago API Free