The API is plain HTTPS and JSON, so the popular requests library is all you need.
Setup
pip install requests
export ROBUSTEMAIL_API_KEY="rb_test_..."
Send an email
import os
import requests
API = "https://robustemail.com/api/v2"
def send_email(email: dict, idempotency_key: str | None = None) -> str:
headers = {"Authorization": f"Bearer {os.environ['ROBUSTEMAIL_API_KEY']}"}
if idempotency_key:
headers["Idempotency-Key"] = idempotency_key
res = requests.post(f"{API}/emails", json=email, headers=headers, timeout=15)
body = res.json()
if not res.ok:
raise RuntimeError(f"{body['name']}: {body['message']}")
return body["id"]
email_id = send_email(
{
"from": "Acme <hello@acme.com>",
"to": "delivered@sandbox.robustemail.dev",
"subject": "Welcome to Acme",
"html": "<h1>Welcome!</h1><p>Thanks for signing up.</p>",
},
idempotency_key="welcome-user-1042",
)
print("Queued", email_id)
Attach a file
import base64
with open("invoice-2041.pdf", "rb") as f:
pdf = base64.b64encode(f.read()).decode()
send_email(
{
"from": "Acme Billing <billing@acme.com>",
"to": "jane@example.com",
"subject": "Your invoice #2041",
"html": "<p>Your invoice is attached.</p>",
"attachments": [
{"filename": "invoice-2041.pdf", "content": pdf, "content_type": "application/pdf"}
],
},
idempotency_key="invoice-2041",
)
Django and Flask
Call send_email from a background job (Celery, RQ or Django-Q) rather than inside the web request, and pass an idempotency key built from your database ID so retried jobs never send twice.