AgentMail Python SDK
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.typedmarker — compatible with mypy and pyright- Context manager —
with 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
Metadata
Release files for agentmail-to 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| agentmail_to-0.1.0.tar.gz | 12.8 kB | Without provenance | Details for agentmail_to-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| agentmail_to-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for agentmail_to-0.1.0-py3-none-any.whl |
Total release size: 29.9 kB
Release files / agentmail_to-0.1.0.tar.gz
| Download URL | agentmail_to-0.1.0.tar.gz |
|---|---|
| Size | 12.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
27e6f9038f0bb30b36067f4c9cd8bb2099cf7f0fc338e127718bf5e97ce1d534
|
|
BLAKE2b-256 checksum How to use checksums |
6de738161b220772c0429045c0c66c8e5abc78a36ccf69a776054b2b151125e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / agentmail_to-0.1.0-py3-none-any.whl
| Download URL | agentmail_to-0.1.0-py3-none-any.whl |
|---|---|
| Size | 17.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8f3fed808f9b2ddeed4792485248ecd4e4bb37e54d6ed7c92aa3366d0992b001
|
|
BLAKE2b-256 checksum How to use checksums |
8a7ee2d47288b11df805c5f28602e8b4e7b07222e8c17d711b2b9411ae112d05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for agentmail-to 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.