How to integrate EmailTemplate from Python
The API is plain REST, so Python only needs an HTTP client. This guide uses httpx to create a template, render it with variables and export it, then integrates it into a FastAPI endpoint and a webhook receiver with HMAC signature verification.
How do I call the EmailTemplate API from Python step by step?
-
Install the HTTP client
httpx works both synchronously and asynchronously, with sane timeouts and retries. requests works too.
bashpip install httpx -
Create a minimal client
A small class wraps the base URL and the API key. The key is read from an environment variable, never from code. The example uses Python 3.10+ syntax.
python# src/email_template.py import os import httpx class EmailTemplateClient: def __init__(self, api_key: str | None = None, base_url: str | None = None): self.base_url = (base_url or os.environ["EMAIL_TEMPLATE_API_URL"]).rstrip("/") self.api_key = api_key or os.environ["EMAIL_TEMPLATE_API_KEY"] def _headers(self) -> dict[str, str]: return {"Content-Type": "application/json", "x-api-key": self.api_key} def create_template(self, name: str, subject: str, html: str) -> dict: r = httpx.post(self.base_url + "/templates", headers=self._headers(), json={"name": name, "subject": subject, "blocks": [], "html": html}, timeout=10) r.raise_for_status() return r.json() def render(self, template_id: str, variables: dict[str, str]) -> dict: r = httpx.post(self.base_url + f"/templates/{template_id}/render", headers=self._headers(), json={"variables": variables}, timeout=10) r.raise_for_status() return r.json() def export(self, template_id: str, fmt: str = "klaviyo") -> dict: r = httpx.post(self.base_url + f"/templates/{template_id}/export", headers=self._headers(), json={"format": fmt}, timeout=10) r.raise_for_status() return r.json() -
Create and render a template
The render returns html, text, subject and usedVariables. Store the HTML in your database or send it directly.
pythonclient = EmailTemplateClient() created = client.create_template( name="Order confirmation", subject="Your order {{ order.id }} is on its way", html="<h1>Hi {{ user.name }}</h1><p>Order {{ order.id }}</p>", ) template_id = created["id"] result = client.render(template_id, {"user.name": "Ana", "order.id": "A-1024"}) print(result["html"]) print(result["usedVariables"]) -
Export to Klaviyo or Resend
The klaviyo adapter returns a snippet with {% track_link %}, {% unsubscribe %} and {{ track_pixel }}. The resend adapter returns React Email JSX.
pythonklaviyo = client.export(template_id, "klaviyo") print(klaviyo["content"]) # paste it into Klaviyo resend = client.export(template_id, "resend") print(resend["content"]) # React Email JSX component -
Integrate into FastAPI
An async endpoint that renders and sends. The API key stays on the server and the client only sends order data.
python# src/main.py from fastapi import FastAPI, HTTPException from .email_template import EmailTemplateClient app = FastAPI() client = EmailTemplateClient() @app.post("/orders/{order_id}/notify") async def notify_order(order_id: str, email: str, user_name: str): try: result = client.render("64f8a1b2c3d4e5f6a7b8c9d1", { "user.name": user_name, "order.id": order_id, }) except httpx.HTTPStatusError as exc: raise HTTPException(status_code=exc.response.status_code, detail="Render failed") # send result["html"] with Resend/Klaviyo here return {"ok": True, "subject": result["subject"]} -
Verify webhooks with HMAC
EmailTemplate signs every webhook with HMAC-SHA256 and the webhook secret. Verify the X-Signature header before processing.
python# src/webhooks.py import hmac import hashlib from fastapi import Request, HTTPException async def receive(request: Request, secret: str): body = await request.body() signature = request.headers.get("X-Signature", "") expected = hmac.new(secret.encode(), body, hashlib.sha256).hexdigest() if not hmac.compare_digest(signature, expected): raise HTTPException(status_code=401, detail="Invalid signature") payload = await request.json() # payload["event"] -> template.created | version.created | ... return payload
| Item | Free | Pro | Team | Enterprise |
|---|---|---|---|---|
| Monthly price (EUR, VAT excluded) | €0 | €19 | €59 | €149 |
| Renders per month | 500 | 25,000 | 100,000 | 500,000 |
| Concurrent templates | 3 | 25 | Unlimited | Unlimited |
| Users | 1 | 2 | 10 | 25 |
| AI credits per month | 50 | 500 | 2,000 | 10,000 |
| AI operation cost | generate: 10 credits · suggest: 5 credits. Charged to the credit wallet (they do not consume renders) and the same on every plan. Packs: 500 credits / €9, 2,000 / €29, 5,000 / €69, 10,000 / €119. | |||
| Stack provider export | format=klaviyo returns HTML with {% track_link %}, {% unsubscribe %} and {{ track_pixel }}; format=resend returns React Email JSX; format=mailchimp returns JSON. | |||
Subscription prices in USD/month; credit packs are charged in EUR. Product-published data, not estimates.
Frequently asked questions
Is there an official Python SDK?
No. The API is REST and works with httpx or requests. The client in this guide is all you need for the create → render → export cycle.
How do I verify a webhook signature?
Compute HMAC-SHA256 of the raw body with the webhook secret and compare it in constant time against the X-Signature header.
Where do I store the API key?
In a server environment variable (EMAIL_TEMPLATE_API_KEY). Never in the repository or in client-side code.