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,
"message_analysis": {
"mode": "all",
"recipients": [],
},
})
print(inbox["email_address"])
print(inbox["message_analysis"]["mode"])
# 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"
print(detail["analysis"]["status"]) # "queued" | "complete" | ...
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.2.tar.gz
(12.1 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.2-py3-none-any.whl
(10.9 kB
view details)
File details
Details for the file postmx-0.1.2.tar.gz.
File metadata
- Download URL: postmx-0.1.2.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9c2ab2dffe1f453321867c05f88ab6bfeb30cbd4dc2ec0274658f7c5959809c
|
|
| MD5 |
866895739532f16c0231df5a097e4597
|
|
| BLAKE2b-256 |
fe7e8be2605875b3ea160f2ddcc593408557b50616159d42d1434c4fcb499bb5
|
File details
Details for the file postmx-0.1.2-py3-none-any.whl.
File metadata
- Download URL: postmx-0.1.2-py3-none-any.whl
- Upload date:
- Size: 10.9 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 |
64439e6abe12b9953ccdaae5e06c9dd72a2322281ae22dac24cc848085078576
|
|
| MD5 |
a116dc663032fa6b3e4123849168205b
|
|
| BLAKE2b-256 |
c39beef3aa836df172243f4fc4379159f0569bb9fdc84b658dcb3749a229877b
|