Official Python SDK for MailGenius — sync + async clients, typed responses, idempotency-aware retries, webhook verification.
Project description
mailgenius — Python SDK
The official Python SDK for MailGenius. Sync and async clients, typed responses (Pydantic), automatic idempotency, retry-on-429/5xx with exponential back-off, and webhook signature verification.
Install
pip install mailgenius
Requires Python 3.9+ and httpx>=0.27, pydantic>=2.6.
Quickstart (sync)
from mailgenius import MailGenius
with MailGenius(api_key="mg_live_...") as mg:
ws = mg.workspace.get()
print(ws.name, "on", ws.plan)
send = mg.templates.send(
"welcome",
to="aki@example.com",
variables={"firstName": "Aki"},
)
print("queued:", send.id)
The API key is also picked up from the MAILGENIUS_API_KEY env var,
matching the Node SDK and CLI.
Quickstart (async)
import asyncio
from mailgenius import AsyncMailGenius
async def main():
async with AsyncMailGenius() as mg: # MAILGENIUS_API_KEY env var
await mg.events.emit(
"subscription.cancelled",
email="aki@example.com",
traits={"tier": "pro", "reason": "moving to weekly"},
)
asyncio.run(main())
Webhook verification (Flask)
from flask import Flask, request, abort
from mailgenius import verify_webhook, WebhookSignatureError
app = Flask(__name__)
@app.post("/mailgenius/webhook")
def webhook():
try:
delivery = verify_webhook(
request.get_data(as_text=True),
request.headers,
secret=os.environ["MAILGENIUS_WEBHOOK_SECRET"],
)
except WebhookSignatureError as e:
abort(400, str(e))
if delivery.type == "send.delivered":
... # handle it
return "", 204
verify_webhook rejects out-of-window timestamps (default ±5 min) and
performs constant-time signature comparison. The same module exposes
sign_webhook for local testing.
Error handling
from mailgenius import (
MailGeniusRateLimitError,
MailGeniusValidationError,
MailGeniusAuthError,
)
try:
mg.events.emit("checkout.completed", email="aki@example.com")
except MailGeniusRateLimitError as e:
time.sleep(e.retry_after_seconds)
except MailGeniusValidationError as e:
log.warning("422: %s", e.body)
except MailGeniusAuthError:
rotate_my_key()
All SDK errors inherit from MailGeniusError and expose .code,
.status, .request_id, .body.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mailgenius-0.1.0.tar.gz.
File metadata
- Download URL: mailgenius-0.1.0.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
137ace559fe1f34f3f8850a80436422c08adab8b02f74a35cb4a1c3e5f09b1d4
|
|
| MD5 |
ec10d024b627b95a591ea254a2c3eea1
|
|
| BLAKE2b-256 |
846b4d6f6e7270d598c8e05ab1843e1a609f9dd3bc7bb2ac766ecfc66b0b68e4
|
File details
Details for the file mailgenius-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mailgenius-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37bdd5deb0d3b35ed048969c887763335972d3db555e665006ce2c9c24c2d41e
|
|
| MD5 |
b20f2aa2a880b2ade02d6c686970bc83
|
|
| BLAKE2b-256 |
dd84edc62ec9d536a99d5ee611b1f295ceb9d0775666d094ad35215f75036cba
|