Official PostMX Python SDK
Project description
PostMX Python SDK
Official Python SDK for the PostMX API.
- Async-first with sync wrapper
- Full type hints (PEP 561)
- Single dependency (
httpx) - Automatic retries with exponential backoff
- Webhook signature verification
Requires Python 3.9+.
Install
pip install postmx
Quick Start
Async
from postmx import PostMX
async def main():
async with PostMX("pmx_live_...") as postmx:
# Create a temporary inbox
inbox = await postmx.create_inbox({
"label": "signup-test",
"lifecycle_mode": "temporary",
"ttl_minutes": 15,
})
print(inbox["email_address"])
# List active inboxes
result = await postmx.list_inboxes()
print(result["inboxes"])
# List messages
result = await postmx.list_messages(inbox["id"])
# Or list messages by exact recipient email
recipient_feed = await postmx.list_messages_by_recipient(inbox["email_address"])
# Get full message detail with OTP extraction
detail = await postmx.get_message(result["messages"][0]["id"])
print(detail["otp"]) # "482910"
print(detail["intent"]) # "login_code"
Sync
from postmx import PostMXSync
postmx = PostMXSync("pmx_live_...")
inbox = postmx.create_inbox({"label": "test", "lifecycle_mode": "temporary"})
print(inbox["email_address"])
API Reference
PostMX(api_key, *, base_url, max_retries, timeout)
| Parameter | Type | Default | Description |
|---|---|---|---|
base_url |
str |
https://api.postmx.co |
API base URL |
max_retries |
int |
2 |
Max retry attempts on 429/5xx |
timeout |
float |
30.0 |
Request timeout in seconds |
Methods
await postmx.list_inboxes(*, limit=None, cursor=None) # → ListInboxesResult
await postmx.create_inbox(params, *, idempotency_key=None) # → Inbox
await postmx.list_messages(inbox_id, *, limit=None, cursor=None) # → ListMessagesResult
await postmx.list_messages_by_recipient(recipient_email, *, limit=None, cursor=None) # → ListMessagesResult
await postmx.get_message(message_id) # → MessageDetail
await postmx.create_webhook(params, *, idempotency_key=None) # → CreateWebhookResult
await postmx.wait_for_message(inbox_id, *, interval=1.0, timeout=60.0) # → MessageDetail
POST methods accept an optional idempotency_key. If not provided, one is auto-generated to make retries safe.
Error Handling
from postmx import PostMXApiError, PostMXNetworkError
try:
await postmx.get_message("bad_id")
except PostMXApiError as err:
print(err.status) # 404
print(err.code) # "not_found"
print(err.request_id) # "req_abc123"
print(err.retry_after_seconds) # None or int
except PostMXNetworkError as err:
print(err.__cause__) # original httpx error
Webhook Verification
from postmx import verify_webhook_signature, PostMXWebhookVerificationError
# In your webhook handler (e.g., FastAPI)
@app.post("/webhooks/postmx")
async def handle_webhook(request: Request):
body = await request.body()
try:
event = verify_webhook_signature(
payload=body,
signature=request.headers["x-postmx-signature"],
timestamp=request.headers["x-postmx-timestamp"],
signing_secret=os.environ["POSTMX_WEBHOOK_SECRET"],
)
print(event["data"]["message"]["otp"])
return {"ok": True}
except PostMXWebhookVerificationError:
raise HTTPException(400)
License
MIT
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
postmx-0.1.0.tar.gz
(11.2 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
postmx-0.1.0-py3-none-any.whl
(10.3 kB
view details)
File details
Details for the file postmx-0.1.0.tar.gz.
File metadata
- Download URL: postmx-0.1.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ade331f5d6680570719b64261d288b8d610ddabe48046886e39e1e16afe7777b
|
|
| MD5 |
8091b779c651b1c0dc2038ff6de3d276
|
|
| BLAKE2b-256 |
6f57462a27eb26c38506834e47b73427adffae43bca2d9d5b62d77c9d61859fd
|
File details
Details for the file postmx-0.1.0-py3-none-any.whl.
File metadata
- Download URL: postmx-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd9a9806e89a4738a238e24cff970452c082bdc3a73014113b91483a87da2f01
|
|
| MD5 |
851ff72d6fa7f55151e6ecec18adfce8
|
|
| BLAKE2b-256 |
0fed11e5c21df6a683d42b5fbf7d99dfd3a3a582fa0b16ebbf3032cb3a72e891
|