Use Python's standard library to generate dynamic OG images in any Flask route.
Start IntegratingJust urlencode and you're good to go. No extra dependencies needed.
Pass the OG URL to your templates. Works seamlessly with Flask's render_template.
Works with any ORM. Use your existing Post, User, or Article models.
from urllib.parse import urlencode
from flask import render_template
import os
@app.route("/blog/<slug>")
def blog_post(slug):
post = Post.query.filter_by(slug=slug).first_or_404()
og_params = urlencode({
"title": post.title,
"template": "modern",
"api_key": os.environ["IMAGO_API_KEY"],
})
og_url = f"https://imagoapi.com/api/og/generate?{og_params}"
return render_template("post.html", 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 Flask app.
Try Imago API Free