Skip to main content

mailgenius — Python SDK

PyPI version

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

Metadata

Release files for mailgenius 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mailgenius 0.1.0
File Size Uploaded Provenance Actions
mailgenius-0.1.0.tar.gz 15.3 kB Without provenance Details for mailgenius-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mailgenius 0.1.0
File Interpreter ABI Platform Actions
mailgenius-0.1.0-py3-none-any.whl Python 3 none any Details for mailgenius-0.1.0-py3-none-any.whl

Total release size: 35.2 kB

Release files / mailgenius-0.1.0.tar.gz

Download URL mailgenius-0.1.0.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
137ace559fe1f34f3f8850a80436422c08adab8b02f74a35cb4a1c3e5f09b1d4
BLAKE2b-256 checksum
How to use checksums
846b4d6f6e7270d598c8e05ab1843e1a609f9dd3bc7bb2ac766ecfc66b0b68e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / mailgenius-0.1.0-py3-none-any.whl

Download URL mailgenius-0.1.0-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
37bdd5deb0d3b35ed048969c887763335972d3db555e665006ce2c9c24c2d41e
BLAKE2b-256 checksum
How to use checksums
dd84edc62ec9d536a99d5ee611b1f295ceb9d0775666d094ad35215f75036cba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Security for mailgenius 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page