Python is the dominant language for AI, automation, and scripting. This guide shows you how to use AgentMailr to send and receive email from Python with minimal boilerplate.

Install the SDK

pip install agentmailr

Or use plain httpx if you prefer no dependencies:

pip install httpx

Create an Inbox

import httpx

API_KEY = "ak_your_key"
BASE = "https://api.agentmailr.com"

def create_inbox(name=None):
    r = httpx.post(
        f"{BASE}/v1/inboxes",
        headers={"X-API-Key": API_KEY},
        json={"name": name}
    )
    r.raise_for_status()
    return r.json()

inbox = create_inbox("signup-bot")
print(inbox["address"])  # e.g. signup-bot@agentmailr.com

Wait for an OTP

def wait_for_otp(inbox_id, timeout=60):
    r = httpx.get(
        f"{BASE}/v1/inboxes/{inbox_id}/otp",
        headers={"X-API-Key": API_KEY},
        params={"timeout": timeout},
        timeout=timeout + 5
    )
    r.raise_for_status()
    return r.json()["code"]

otp = wait_for_otp(inbox["id"])
print(f"Got OTP: {otp}")

Send an Email

def send_email(inbox_id, to, subject, text):
    r = httpx.post(
        f"{BASE}/v1/inboxes/{inbox_id}/send",
        headers={"X-API-Key": API_KEY},
        json={"to": to, "subject": subject, "text": text}
    )
    r.raise_for_status()
    return r.json()

send_email(inbox["id"], "user@example.com", "Hello", "Test message")

Async Version

For use with asyncio or frameworks like FastAPI:

import asyncio, httpx

async def wait_for_otp_async(inbox_id, timeout=60):
    async with httpx.AsyncClient() as client:
        r = await client.get(
            f"{BASE}/v1/inboxes/{inbox_id}/otp",
            headers={"X-API-Key": API_KEY},
            params={"timeout": timeout},
            timeout=timeout + 5
        )
        r.raise_for_status()
        return r.json()["code"]

That Is It

No IMAP, no SMTP setup, no OAuth. Just HTTP calls. The API handles delivery, parsing, and OTP extraction for you.