Official AgentMail Python SDK — API-first email inbox for AI agents
Project description
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
Project details
Release history Release notifications | RSS feed
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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27e6f9038f0bb30b36067f4c9cd8bb2099cf7f0fc338e127718bf5e97ce1d534
|
|
| MD5 |
392a299f2a662a39fdb14a8fc871a76c
|
|
| BLAKE2b-256 |
6de738161b220772c0429045c0c66c8e5abc78a36ccf69a776054b2b151125e9
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f3fed808f9b2ddeed4792485248ecd4e4bb37e54d6ed7c92aa3366d0992b001
|
|
| MD5 |
ac5e4cc70c564ea76658b448d8b92c50
|
|
| BLAKE2b-256 |
8a7ee2d47288b11df805c5f28602e8b4e7b07222e8c17d711b2b9411ae112d05
|