Skip to main content

Official AgentMail Python SDK — API-first email inbox for AI agents

Project description

AgentMail Python SDK

PyPI version Python

The official Python client library for AgentMail — the email inbox API built for AI agents.

Installation

pip install agentmail

With async support:

pip install agentmail[async]

Quickstart

from agentmail import AgentMail

client = AgentMail(api_key="am_live_...")

# Create an inbox for your agent
inbox = client.inboxes.create(username="my-agent")

# Send an email
message = client.send.send(
    inbox_id=inbox.id,
    to=["user@example.com"],
    subject="Your OTP Code",
    body_text="Your verification code is 847291.",
)

# Wait for a reply with a matching OTP
reply = client.inboxes.wait_for_message(
    inbox.id,
    timeout=60,
    from_addr="user@example.com",
)
print(f"OTP received: {reply.extracted_otp}")

Key Features

  • Auto-retry on rate limits (429) and server errors (5xx) — exponential backoff, max 3 attempts
  • Pagination via cursor — client.inboxes.list() returns an iterator
  • Typed models — all API objects are Pydantic v2 models with full type hints
  • py.typed marker — compatible with mypy and pyright
  • Context managerwith AgentMail(api_key="...") as client:

Resource Namespaces

Namespace Key Methods
client.inboxes .create(), .get(), .list(), .update(), .delete(), .wait_for_message()
client.messages .get(), .list(), .search(), .mark_read(), .mark_unread(), .reply(), .forward()
client.threads .get(), .list()
client.webhooks .create(), .get(), .list(), .delete(), .rotate_secret()
client.drafts .create(), .get(), .list(), .update(), .delete(), .send()
client.labels .create(), .get(), .list(), .add(), .remove(), .delete()
client.send .send()
client.account .get(), .stats(), .keys()
client.analytics .volume(), .bounce_rate()
client.quarantine .list(), .release(), .delete()
client.suppression .list(), .add(), .remove(), .get()
client.attachments .get(), .info()
client.keys .list(), .create(), .revoke()

Error Handling

from agentmail import AgentMail, AgentMailError, RateLimitError, NotFoundError

try:
    inbox = client.inboxes.get("invalid-id")
except NotFoundError as e:
    print(f"Inbox not found: {e.message}")
except RateLimitError as e:
    print(f"Rate limited — retry after: {e.message}")
except AgentMailError as e:
    print(f"API error [{e.error_code}]: {e.message}")

Examples

OTP Verification Flow

from agentmail import AgentMail

client = AgentMail(api_key="am_live_...")

# Create agent inbox
inbox = client.inboxes.create(username="otp-bot")

# Wait for email containing OTP (blocks up to 60s)
message = client.inboxes.wait_for_message(
    inbox.id,
    timeout=60,
    subject="Your verification code",
)
otp = message.extracted_otp
print(f"OTP: {otp}")  # e.g. "482917"

Multi-Agent Inbox Provisioning

# Bulk create inboxes for a team of agents
agent_names = ["sales-agent", "support-agent", "billing-agent"]
for name in agent_names:
    inbox = client.inboxes.create(username=name, agent_name=name)
    print(f"Created: {inbox.address}")

Webhook Registration + Delivery Log

import os

# Register a webhook for all message events
webhook = client.webhooks.create(
    url="https://my-agent.example.com/webhook",
    events=["message.received", "message.sent"],
)

# Inspect recent webhook deliveries
deliveries = client.webhooks.delivery_log(webhook.id)
for d in deliveries:
    print(f"{d['event']}: {d['status_code']} at {d['createdAt']}")

Configuration

client = AgentMail(
    api_key="am_live_...",
    base_url="https://api.agentmail.to",  # optional, for testing
    timeout=30.0,                          # request timeout (default 30s)
    max_retries=3,                         # auto-retry attempts (default 3)
    debug=True,                            # log requests/responses
)

License

MIT © AgentMail Team

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentmail_to-0.1.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agentmail_to-0.1.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file agentmail_to-0.1.0.tar.gz.

File metadata

  • Download URL: agentmail_to-0.1.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for agentmail_to-0.1.0.tar.gz
Algorithm Hash digest
SHA256 27e6f9038f0bb30b36067f4c9cd8bb2099cf7f0fc338e127718bf5e97ce1d534
MD5 392a299f2a662a39fdb14a8fc871a76c
BLAKE2b-256 6de738161b220772c0429045c0c66c8e5abc78a36ccf69a776054b2b151125e9

See more details on using hashes here.

File details

Details for the file agentmail_to-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: agentmail_to-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for agentmail_to-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f3fed808f9b2ddeed4792485248ecd4e4bb37e54d6ed7c92aa3366d0992b001
MD5 ac5e4cc70c564ea76658b448d8b92c50
BLAKE2b-256 8a7ee2d47288b11df805c5f28602e8b4e7b07222e8c17d711b2b9411ae112d05

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page