Skip to main content
Use the official Python SDK to call the Mailrify API from server-side apps.

Prerequisites

Before you start, make sure you have:
  • A Mailrify secret API key (sk_...) for server-side API requests
  • A Mailrify public API key (pk_...) for event tracking (/v1/track)
  • At least one verified sending domain for email sending
Setup links:

Install

pip install mailrify

Initialize clients

Create one SDK client for your secret key and one for your public key:
from mailrify import AsyncMailrify, Mailrify

secret_client = Mailrify("sk_your_secret_key")
public_client = Mailrify("pk_your_public_key")

async_secret_client = AsyncMailrify("sk_your_secret_key")
async_public_client = AsyncMailrify("pk_your_public_key")
Set environment variables:
export MAILRIFY_SECRET_KEY="sk_your_secret_key"
export MAILRIFY_PUBLIC_KEY="pk_your_public_key"

Configure base URL, timeout, and retries

Use this when testing against staging or local environments:
from mailrify import Mailrify

client = Mailrify(
    "sk_your_secret_key",
    base_url="https://api.mailrify.com",
    timeout=30.0,
    max_retries=3,
)

Key usage rules

The Python SDK enforces key restrictions automatically:
  • pk_* keys can only call events.track
  • sk_* keys can call all non-track endpoints, including events.get_names
from mailrify import Mailrify
from mailrify.exceptions import AuthenticationError

secret_client = Mailrify("sk_your_secret_key")

try:
    secret_client.events.track(email="[email protected]", event="purchase")
except AuthenticationError as error:
    print(error.message)
finally:
    secret_client.close()

Close clients

Use context managers or call close() / await close() explicitly:
from mailrify import AsyncMailrify, Mailrify

with Mailrify("sk_your_secret_key") as client:
    verification = client.emails.verify("[email protected]")
    print(verification.valid)


async def run_async() -> None:
    async with AsyncMailrify("sk_your_secret_key") as client:
        verification = await client.emails.verify("[email protected]")
        print(verification.valid)

Next pages