prontuno 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- prontuno-0.1.0/.gitignore +14 -0
- prontuno-0.1.0/LICENSE.md +9 -0
- prontuno-0.1.0/PKG-INFO +536 -0
- prontuno-0.1.0/README.md +508 -0
- prontuno-0.1.0/pyproject.toml +93 -0
- prontuno-0.1.0/src/prontuno/__init__.py +33 -0
- prontuno-0.1.0/src/prontuno/_base.py +423 -0
- prontuno-0.1.0/src/prontuno/_client.py +226 -0
- prontuno-0.1.0/src/prontuno/_http.py +216 -0
- prontuno-0.1.0/src/prontuno/_pagination.py +74 -0
- prontuno-0.1.0/src/prontuno/_version.py +1 -0
- prontuno-0.1.0/src/prontuno/errors.py +144 -0
- prontuno-0.1.0/src/prontuno/py.typed +0 -0
- prontuno-0.1.0/src/prontuno/resources/__init__.py +28 -0
- prontuno-0.1.0/src/prontuno/resources/_resource.py +13 -0
- prontuno-0.1.0/src/prontuno/resources/api_keys.py +138 -0
- prontuno-0.1.0/src/prontuno/resources/domains.py +96 -0
- prontuno-0.1.0/src/prontuno/resources/emails.py +332 -0
- prontuno-0.1.0/src/prontuno/resources/smtp_credentials.py +156 -0
- prontuno-0.1.0/src/prontuno/resources/suppressions.py +121 -0
- prontuno-0.1.0/src/prontuno/resources/usage.py +21 -0
- prontuno-0.1.0/src/prontuno/resources/webhooks.py +184 -0
- prontuno-0.1.0/src/prontuno/types.py +639 -0
- prontuno-0.1.0/src/prontuno/webhooks.py +152 -0
- prontuno-0.1.0/tests/__init__.py +0 -0
- prontuno-0.1.0/tests/conftest.py +19 -0
- prontuno-0.1.0/tests/helpers.py +88 -0
- prontuno-0.1.0/tests/test_async.py +241 -0
- prontuno-0.1.0/tests/test_config.py +335 -0
- prontuno-0.1.0/tests/test_package.py +56 -0
- prontuno-0.1.0/tests/test_pagination.py +105 -0
- prontuno-0.1.0/tests/test_resources.py +617 -0
- prontuno-0.1.0/tests/test_responses.py +280 -0
- prontuno-0.1.0/tests/test_retries.py +319 -0
- prontuno-0.1.0/tests/test_webhooks.py +235 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) Prontuno
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
prontuno-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: prontuno
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python SDK for the Prontuno email API
|
|
5
|
+
Project-URL: Homepage, https://github.com/prontuno/prontuno-python#readme
|
|
6
|
+
Project-URL: Issues, https://github.com/prontuno/prontuno-python/issues
|
|
7
|
+
Project-URL: Source, https://github.com/prontuno/prontuno-python
|
|
8
|
+
Project-URL: Documentation, https://prontuno.com/api-reference/general/introduction/
|
|
9
|
+
Author-email: Prontuno Team <support@prontuno.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE.md
|
|
12
|
+
Keywords: email,prontuno,sdk,transactional-email
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
23
|
+
Classifier: Topic :: Communications :: Email
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.11
|
|
26
|
+
Requires-Dist: httpx<1,>=0.27
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# Prontuno Python SDK
|
|
30
|
+
|
|
31
|
+
Typed Python client for the [Prontuno API](https://prontuno.com/api-reference/general/introduction/). It supports email delivery, sending domains, suppressions, webhook endpoints, API keys, SMTP credentials, usage, cursor pagination, and signed webhook events, with both a sync and an async client.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install prontuno
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Python 3.11 or newer is required. The only runtime dependency is [httpx](https://www.python-httpx.org/).
|
|
40
|
+
|
|
41
|
+
## Quick start
|
|
42
|
+
|
|
43
|
+
Prontuno has two kinds of API key: a **sending key**, which can only send email from one verified domain, and a **management key**, which can do everything else. Give the client both and it uses the right one for each call:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import os
|
|
47
|
+
import uuid
|
|
48
|
+
|
|
49
|
+
from prontuno import Prontuno
|
|
50
|
+
|
|
51
|
+
prontuno = Prontuno(
|
|
52
|
+
sending_key=os.environ["PRONTUNO_SENDING_KEY"], # emails.send() and emails.send_batch()
|
|
53
|
+
management_key=os.environ["PRONTUNO_MANAGEMENT_KEY"], # everything else
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
result = prontuno.emails.send(
|
|
57
|
+
{
|
|
58
|
+
"from": "Acme <hello@mg.example.com>",
|
|
59
|
+
"to": "ada@example.com",
|
|
60
|
+
"subject": "Welcome to Acme",
|
|
61
|
+
"html": "<h1>Welcome!</h1>",
|
|
62
|
+
},
|
|
63
|
+
idempotency_key=str(uuid.uuid4()),
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
print(result["emails"])
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Requests and responses are plain `dict`s that use the API's snake_case field names, so they match the Prontuno API directly. They're described by `TypedDict`s in `prontuno.types` for type checkers and editors. Because `from` is a Python keyword, emails are always passed as dicts.
|
|
70
|
+
|
|
71
|
+
The client keeps a connection pool open. Use it as a context manager, or call `prontuno.close()` when you're done:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
with Prontuno(api_key=os.environ["PRONTUNO_API_KEY"]) as prontuno:
|
|
75
|
+
...
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## API keys and routing
|
|
79
|
+
|
|
80
|
+
| Method | Key used |
|
|
81
|
+
| ------------------------------------------------------------------ | ---------------- |
|
|
82
|
+
| `emails.send()`, `emails.send_batch()` | `sending_key` |
|
|
83
|
+
| Everything else, including `emails.list()` and `emails.retrieve()` | `management_key` |
|
|
84
|
+
|
|
85
|
+
- **One key for everything:** `Prontuno(api_key)` or `Prontuno(api_key=api_key)` uses that key for every request, for example a send-only app with just a sending key. `sending_key` and `management_key` take precedence over `api_key`.
|
|
86
|
+
- **Missing keys fail early:** calling a method whose key isn't configured raises a `ProntunoConfigError` before any request is made. A send-only client calling `domains.list()` gets "GET /domains requires a management API key; pass management_key (or api_key)" instead of a `403` from the API.
|
|
87
|
+
- **Blank keys count as unset**, so an empty environment variable doesn't send an empty `Authorization` header.
|
|
88
|
+
- **Several sending domains:** each sending key belongs to one domain. Use `with_sending_key()` to get a client for another domain that keeps all other settings, including the management key. The new client shares the original's connection pool, so close the original when you're done:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
billing = prontuno.with_sending_key(os.environ["PRONTUNO_BILLING_SENDING_KEY"])
|
|
92
|
+
billing.emails.send({"from": "billing@billing.example.com", ...})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Configuration
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
prontuno = Prontuno(
|
|
99
|
+
sending_key=os.environ["PRONTUNO_SENDING_KEY"],
|
|
100
|
+
management_key=os.environ["PRONTUNO_MANAGEMENT_KEY"],
|
|
101
|
+
base_url="https://api.prontuno.net/v1",
|
|
102
|
+
timeout=30.0, # seconds, per attempt
|
|
103
|
+
max_retries=2,
|
|
104
|
+
max_response_size=10 * 1024 * 1024, # bytes
|
|
105
|
+
app_info="my-app/1.2.0",
|
|
106
|
+
)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`app_info` identifies your application in Prontuno's logs. It's added after the SDK's own User-Agent, so requests are sent as `prontuno-python/0.1.0 my-app/1.2.0`.
|
|
110
|
+
|
|
111
|
+
The base URL defaults to `https://api.prontuno.net/v1`. To point every client at a different endpoint without code changes, set `PRONTUNO_BASE_URL` in the environment; an explicit `base_url` takes precedence, and a blank value falls back to the default.
|
|
112
|
+
|
|
113
|
+
Custom API endpoints must use HTTPS. Plain HTTP is accepted only for `localhost`, `127.0.0.1`, and `[::1]`.
|
|
114
|
+
|
|
115
|
+
GET and DELETE requests are retried for transient responses (`408`, `429`, and `5xx`), network errors, and timeouts. Sends are retried only when an `idempotency_key` is present; other POST and PATCH requests are never automatically retried. Retries back off exponentially with jitter, and `Retry-After` is honoured up to 60 seconds. The timeout applies to each attempt, including reading the response body, so waiting between retries doesn't count against it. If a DELETE is retried after an attempt whose outcome is unknown (a network error, timeout, `408`, or `5xx`) and the retry finds the resource already gone (`404`, or `409 already_revoked`), the call succeeds.
|
|
116
|
+
|
|
117
|
+
Identifiers are checked before any request, so an empty ID raises a `ProntunoConfigError`. Responses are limited to 10 MiB by default; use `max_response_size` to choose a different positive byte limit.
|
|
118
|
+
|
|
119
|
+
The metadata for the most recently completed response is available on `prontuno.last_response`, including the request ID and rate-limit values:
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
prontuno.domains.list()
|
|
123
|
+
print(prontuno.last_response.request_id, prontuno.last_response.rate_limit.remaining)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
To customise the HTTP layer (proxies, transports, event hooks), pass your own `httpx.Client` as `http_client`. The SDK sets its own timeout, headers, and URL on every request, and doesn't close a client you pass in.
|
|
127
|
+
|
|
128
|
+
## Emails
|
|
129
|
+
|
|
130
|
+
### Send one email
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from pathlib import Path
|
|
134
|
+
|
|
135
|
+
prontuno.emails.send(
|
|
136
|
+
{
|
|
137
|
+
"from": "Acme <hello@mg.example.com>",
|
|
138
|
+
"to": ["ada@example.com", "grace@example.com"],
|
|
139
|
+
"cc": "team@example.com",
|
|
140
|
+
"reply_to": "support@example.com",
|
|
141
|
+
"subject": "Your report",
|
|
142
|
+
"text": "The report is attached.",
|
|
143
|
+
"attachments": [
|
|
144
|
+
{
|
|
145
|
+
"filename": "report.pdf",
|
|
146
|
+
"content": Path("report.pdf").read_bytes(),
|
|
147
|
+
"content_type": "application/pdf",
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
}
|
|
151
|
+
)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
`bytes` attachment content is automatically base64-encoded. String content is read as base64 unless you set `encoding`, so pass `"encoding": "utf8"` for plain text. Every email needs a `text` body, an `html` body, or both. Each email can have up to 100 recipients across `to`, `cc`, and `bcc`, and must be 25 MB or less once encoded with its attachments (larger emails are rejected with `413 message_too_large`). A batch holds up to 1,000 emails; if any item is invalid, nothing is sent.
|
|
155
|
+
|
|
156
|
+
### Send a batch
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
prontuno.emails.send_batch(
|
|
160
|
+
[
|
|
161
|
+
{
|
|
162
|
+
"from": "hello@mg.example.com",
|
|
163
|
+
"to": "ada@example.com",
|
|
164
|
+
"subject": "Hello",
|
|
165
|
+
"text": "Hi Ada",
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
"from": "hello@mg.example.com",
|
|
169
|
+
"to": "grace@example.com",
|
|
170
|
+
"subject": "Hello",
|
|
171
|
+
"text": "Hi Grace",
|
|
172
|
+
},
|
|
173
|
+
],
|
|
174
|
+
idempotency_key=str(uuid.uuid4()),
|
|
175
|
+
)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Retrieve and list
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
email = prontuno.emails.retrieve("email_01kx...")
|
|
182
|
+
print(email["status"], email["text"], email["html"]) # bodies are None when absent or unavailable
|
|
183
|
+
|
|
184
|
+
page = prontuno.emails.list(limit=50, status="delivered", domain_id="dom_01kx...")
|
|
185
|
+
print(page["data"], page["pagination"]["next_cursor"])
|
|
186
|
+
|
|
187
|
+
for email in prontuno.emails.list_all(status="bounced"):
|
|
188
|
+
print(email["id"], email["rejection_reason"])
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
`list_all()` fetches pages lazily as you iterate. It rejects repeated cursors and inconsistent pagination metadata, and reads at most 1,000 pages by default; pass `max_pages` to choose a different positive limit.
|
|
192
|
+
|
|
193
|
+
`retrieve()` returns the full email, including its stored `text` and `html` bodies and its `attachments`. List results contain only the status and metadata, so call `retrieve()` when you need the content.
|
|
194
|
+
|
|
195
|
+
### Attachments
|
|
196
|
+
|
|
197
|
+
Attachments on a sent email come with a short-lived signed `download_url` that is valid for one hour, until `expires_at`. Both are `None` while a file hasn't finished uploading, and `emails.attachments.retrieve()` raises a `403 attachment_unavailable` error until it has.
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
import httpx
|
|
201
|
+
|
|
202
|
+
page = prontuno.emails.attachments.list("email_01kx...", limit=20)
|
|
203
|
+
|
|
204
|
+
for attachment in prontuno.emails.attachments.list_all("email_01kx..."):
|
|
205
|
+
print(attachment["filename"], attachment["size"], attachment["download_url"])
|
|
206
|
+
|
|
207
|
+
# Fetch a fresh link for one attachment, then download it.
|
|
208
|
+
attachment = prontuno.emails.attachments.retrieve("email_01kx...", "att_01kx...")
|
|
209
|
+
if attachment["download_url"]:
|
|
210
|
+
data = httpx.get(attachment["download_url"]).content
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Download links expire, so request a fresh one with `emails.attachments.retrieve()` (or `emails.retrieve()`) instead of storing them. Treat `download_url` as opaque: it may be served from Prontuno's CDN or directly from storage, so don't parse it or depend on its host or path. Fetch it directly, without your API key. `content_disposition` (`inline` or `attachment`) describes how the file was attached to the email, not how the link is served. A temporary storage outage returns a retryable `503 storage_unavailable`, which the SDK retries automatically.
|
|
214
|
+
|
|
215
|
+
## Domains
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
domain = prontuno.domains.create({"domain": "mg.example.com"})
|
|
219
|
+
print(domain["dns_records"])
|
|
220
|
+
|
|
221
|
+
current = prontuno.domains.retrieve(domain["id"])
|
|
222
|
+
page = prontuno.domains.list(limit=50)
|
|
223
|
+
|
|
224
|
+
for domain in prontuno.domains.list_all():
|
|
225
|
+
print(domain["domain"], domain["is_verified"])
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Suppressions
|
|
229
|
+
|
|
230
|
+
```python
|
|
231
|
+
suppression = prontuno.suppressions.create({"email": "user@example.com"})
|
|
232
|
+
|
|
233
|
+
prontuno.suppressions.retrieve(suppression["id"])
|
|
234
|
+
prontuno.suppressions.retrieve("user@example.com")
|
|
235
|
+
prontuno.suppressions.delete(suppression["id"])
|
|
236
|
+
|
|
237
|
+
for suppression in prontuno.suppressions.list_all(email="user@example.com"):
|
|
238
|
+
print(suppression["email"], suppression["type"])
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Only manual suppressions can be deleted through the API.
|
|
242
|
+
|
|
243
|
+
## Webhook endpoints
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
webhook = prontuno.webhooks.create(
|
|
247
|
+
{
|
|
248
|
+
"name": "Production app",
|
|
249
|
+
"url": "https://example.com/webhooks/prontuno",
|
|
250
|
+
"events": ["email.delivered", "email.bounced", "email.complained"],
|
|
251
|
+
# Omit domain_id for a team-wide webhook, or scope it to one verified domain:
|
|
252
|
+
# "domain_id": "dom_01kx...",
|
|
253
|
+
}
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
# The signing secret is returned only once. Store it securely.
|
|
257
|
+
print(webhook["secret"])
|
|
258
|
+
|
|
259
|
+
prontuno.webhooks.retrieve(webhook["id"])
|
|
260
|
+
prontuno.webhooks.update(webhook["id"], {"is_enabled": False})
|
|
261
|
+
# Switching scope needs is_global as well as domain_id:
|
|
262
|
+
prontuno.webhooks.update(webhook["id"], {"is_global": False, "domain_id": "dom_01kx..."})
|
|
263
|
+
|
|
264
|
+
secret = prontuno.webhooks.rotate_secret(webhook["id"])["secret"]
|
|
265
|
+
|
|
266
|
+
for item in prontuno.webhooks.list_all():
|
|
267
|
+
print(item["name"], item["events"])
|
|
268
|
+
|
|
269
|
+
prontuno.webhooks.delete(webhook["id"])
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### Event types
|
|
273
|
+
|
|
274
|
+
| Event | Sent when |
|
|
275
|
+
| ------------------ | ----------------------------------------------------------------------------------------------------------- |
|
|
276
|
+
| `email.sent` | Prontuno's outbound mail server accepted the email for delivery. |
|
|
277
|
+
| `email.delivered` | The recipient's mail server accepted the email. |
|
|
278
|
+
| `email.deferred` | The recipient's mail server temporarily refused the email. Delivery is retried. |
|
|
279
|
+
| `email.bounced` | The recipient's mail server permanently refused the email. |
|
|
280
|
+
| `email.expired` | Delivery was retried until the retry period ran out without success. |
|
|
281
|
+
| `email.rejected` | Prontuno refused the email before sending it, for example because the recipient is on the suppression list. |
|
|
282
|
+
| `email.complained` | The recipient reported the email as spam to their mailbox provider. |
|
|
283
|
+
|
|
284
|
+
Subscribe to any combination with `events`. Each event's `data` describes the email it relates to.
|
|
285
|
+
|
|
286
|
+
Rotating a secret invalidates the previous one immediately, so update your receiver right away.
|
|
287
|
+
|
|
288
|
+
## API keys
|
|
289
|
+
|
|
290
|
+
Managing API keys requires a `management` API key.
|
|
291
|
+
|
|
292
|
+
```python
|
|
293
|
+
# Key permissions (scopes) come from its type and can't be chosen individually.
|
|
294
|
+
sending_key = prontuno.api_keys.create(
|
|
295
|
+
{
|
|
296
|
+
"name": "Production sender",
|
|
297
|
+
"type": "sending", # email:send from one verified domain
|
|
298
|
+
"domain_id": "dom_01kx...",
|
|
299
|
+
}
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
# The plaintext key is returned only once. Store it securely.
|
|
303
|
+
print(sending_key["key"])
|
|
304
|
+
|
|
305
|
+
prontuno.api_keys.create({"name": "Support agent", "type": "mcp"})
|
|
306
|
+
prontuno.api_keys.create(
|
|
307
|
+
{
|
|
308
|
+
"name": "Agent that sends",
|
|
309
|
+
"type": "mcp",
|
|
310
|
+
"allow_email_sending": True,
|
|
311
|
+
"domain_id": "dom_01kx...",
|
|
312
|
+
}
|
|
313
|
+
)
|
|
314
|
+
prontuno.api_keys.create({"name": "Ops automation", "type": "management"})
|
|
315
|
+
|
|
316
|
+
prontuno.api_keys.retrieve(sending_key["id"]) # includes revoked keys
|
|
317
|
+
|
|
318
|
+
for key in prontuno.api_keys.list_all(status="all", type="sending"):
|
|
319
|
+
print(key["name"], key["status"], key["last_used_at"])
|
|
320
|
+
|
|
321
|
+
# Requests using a revoked key fail immediately. Revoking twice raises 409 already_revoked.
|
|
322
|
+
prontuno.api_keys.revoke(sending_key["id"])
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
| Type | Scopes |
|
|
326
|
+
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
327
|
+
| `sending` | `email:send` for one verified domain (`domain_id` required) |
|
|
328
|
+
| `mcp` | `domains:*`, `emails:read`, `suppressions:*`, `usage:read`, `webhooks:*`, plus `email:send` with `allow_email_sending: True` and a `domain_id` |
|
|
329
|
+
| `management` | The `mcp` account scopes without `email:send`, plus `api_keys:*` and `smtp_credentials:*` |
|
|
330
|
+
|
|
331
|
+
`list` and `list_all` return active keys by default; pass `status="revoked"` or `"all"` to include others.
|
|
332
|
+
|
|
333
|
+
## SMTP credentials
|
|
334
|
+
|
|
335
|
+
```python
|
|
336
|
+
credential = prontuno.smtp_credentials.create({"name": "Legacy CRM", "domain_id": "dom_01kx..."})
|
|
337
|
+
|
|
338
|
+
# Use these as the SMTP login. The password is returned only once.
|
|
339
|
+
print(credential["username"], credential["password"])
|
|
340
|
+
|
|
341
|
+
# The old password stops working immediately.
|
|
342
|
+
password = prontuno.smtp_credentials.reset_password(credential["id"])["password"]
|
|
343
|
+
|
|
344
|
+
prontuno.smtp_credentials.retrieve(credential["id"])
|
|
345
|
+
for item in prontuno.smtp_credentials.list_all(status="all"):
|
|
346
|
+
print(item["username"], item["status"])
|
|
347
|
+
|
|
348
|
+
prontuno.smtp_credentials.revoke(credential["id"]) # SMTP logins fail immediately
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
Creating API keys and SMTP credentials, and resetting passwords, are never retried automatically, so a timeout can't create a duplicate secret.
|
|
352
|
+
|
|
353
|
+
## Usage
|
|
354
|
+
|
|
355
|
+
```python
|
|
356
|
+
usage = prontuno.usage.retrieve()
|
|
357
|
+
|
|
358
|
+
if usage["subscription"] and usage["subscription"]["is_active"]:
|
|
359
|
+
print(usage["subscription"]["recipients_remaining"]) # None on the unlimited plan
|
|
360
|
+
print(usage["credits"]["balance"]) # prepaid credits; one covers one recipient
|
|
361
|
+
print(usage["limits"]["domains"]) # {"used": 12, "limit": 1000}, limit is None when unlimited
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
Sends draw on the subscription allowance first while it's active, then on prepaid credits. `subscription` is `None` when the team has no plan, and `recipients_remaining` is `0` while the subscription isn't active.
|
|
365
|
+
|
|
366
|
+
## Errors
|
|
367
|
+
|
|
368
|
+
```python
|
|
369
|
+
from prontuno import ProntunoApiError
|
|
370
|
+
|
|
371
|
+
try:
|
|
372
|
+
prontuno.emails.retrieve("email_missing")
|
|
373
|
+
except ProntunoApiError as error:
|
|
374
|
+
print(error.status) # 404
|
|
375
|
+
print(error.code) # "not_found"
|
|
376
|
+
print(error.message)
|
|
377
|
+
print(error.request_id) # x-request-id
|
|
378
|
+
print(error.retryable)
|
|
379
|
+
print(error.suggested_action)
|
|
380
|
+
print(error.details) # validation/batch details, when present
|
|
381
|
+
print(error.retry_after) # seconds, when the API sends Retry-After
|
|
382
|
+
print(error.rate_limit) # RateLimit(limit=..., remaining=..., reset=...)
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
Every exception the SDK raises is a `ProntunoError`:
|
|
386
|
+
|
|
387
|
+
| Exception | Raised when |
|
|
388
|
+
| ----------------------------------- | -------------------------------------------------------------------------------------------- |
|
|
389
|
+
| `ProntunoConfigError` | The client or a call is misconfigured: a missing key, an invalid option, or an empty ID. |
|
|
390
|
+
| `ProntunoApiError` | The API returned an error response. |
|
|
391
|
+
| `ProntunoConnectionError` | The request failed, or the response was empty, not JSON, or larger than `max_response_size`. |
|
|
392
|
+
| `ProntunoTimeoutError` | An attempt took longer than `timeout`. A subclass of `ProntunoConnectionError`. |
|
|
393
|
+
| `WebhookSignatureVerificationError` | A webhook signature is missing, malformed, doesn't match, or is too old. |
|
|
394
|
+
| `WebhookPayloadError` | A correctly signed webhook body isn't a valid event. |
|
|
395
|
+
|
|
396
|
+
## Webhooks
|
|
397
|
+
|
|
398
|
+
Prontuno signs the exact raw request body with HMAC-SHA256. Verify the raw bytes; don't parse and re-serialize the body first.
|
|
399
|
+
|
|
400
|
+
### Flask
|
|
401
|
+
|
|
402
|
+
```python
|
|
403
|
+
import os
|
|
404
|
+
|
|
405
|
+
from flask import Flask, request
|
|
406
|
+
|
|
407
|
+
from prontuno import WebhookPayloadError, WebhookSignatureVerificationError, construct_webhook_event
|
|
408
|
+
|
|
409
|
+
app = Flask(__name__)
|
|
410
|
+
webhook_secret = os.environ["PRONTUNO_WEBHOOK_SIGNING_SECRET"].strip()
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
@app.post("/webhooks/prontuno")
|
|
414
|
+
def prontuno_webhook():
|
|
415
|
+
try:
|
|
416
|
+
event = construct_webhook_event(
|
|
417
|
+
request.get_data(), # the raw body
|
|
418
|
+
request.headers.get("Prontuno-Signature"),
|
|
419
|
+
webhook_secret,
|
|
420
|
+
)
|
|
421
|
+
except (WebhookSignatureVerificationError, WebhookPayloadError):
|
|
422
|
+
return "Invalid webhook", 400
|
|
423
|
+
|
|
424
|
+
match event["type"]:
|
|
425
|
+
case "email.delivered":
|
|
426
|
+
print(event["data"]["email_id"], event["data"]["delivered_at"])
|
|
427
|
+
case "email.bounced":
|
|
428
|
+
print(event["data"]["reason"], event["data"]["bounce"]["type"])
|
|
429
|
+
case "email.complained":
|
|
430
|
+
print("Recipient marked the email as spam", event["data"]["to_email"])
|
|
431
|
+
case _:
|
|
432
|
+
# Event types added after this SDK version arrive here; acknowledge them.
|
|
433
|
+
pass
|
|
434
|
+
|
|
435
|
+
return "", 204
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### FastAPI
|
|
439
|
+
|
|
440
|
+
```python
|
|
441
|
+
import os
|
|
442
|
+
|
|
443
|
+
from fastapi import FastAPI, HTTPException, Request, Response
|
|
444
|
+
|
|
445
|
+
from prontuno import WebhookPayloadError, WebhookSignatureVerificationError, construct_webhook_event
|
|
446
|
+
|
|
447
|
+
app = FastAPI()
|
|
448
|
+
webhook_secret = os.environ["PRONTUNO_WEBHOOK_SIGNING_SECRET"].strip()
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
@app.post("/webhooks/prontuno", status_code=204)
|
|
452
|
+
async def prontuno_webhook(request: Request) -> Response:
|
|
453
|
+
try:
|
|
454
|
+
event = construct_webhook_event(
|
|
455
|
+
await request.body(), # the raw body
|
|
456
|
+
request.headers.get("prontuno-signature"),
|
|
457
|
+
webhook_secret,
|
|
458
|
+
)
|
|
459
|
+
except (WebhookSignatureVerificationError, WebhookPayloadError):
|
|
460
|
+
raise HTTPException(status_code=400, detail="Invalid webhook")
|
|
461
|
+
|
|
462
|
+
if event["type"] == "email.bounced":
|
|
463
|
+
print(event["data"]["reason"])
|
|
464
|
+
return Response(status_code=204)
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
The default timestamp tolerance is five minutes. Pass `tolerance=0` to skip the timestamp check. To verify without parsing, call `verify_webhook_signature(raw_body, signature, secret)` or `prontuno.webhooks.verify_signature(...)`. The standalone functions don't need an API key or a client.
|
|
468
|
+
|
|
469
|
+
Webhook requests include these useful headers:
|
|
470
|
+
|
|
471
|
+
- `Prontuno-Event`
|
|
472
|
+
- `Prontuno-Event-Id`
|
|
473
|
+
- `Prontuno-Webhook-Id`
|
|
474
|
+
- `Prontuno-Signature` (`t=<unix-seconds>,v1=<hex-hmac>`)
|
|
475
|
+
- `Prontuno-Delivery-Attempt`
|
|
476
|
+
- `Idempotency-Key` (the event ID)
|
|
477
|
+
|
|
478
|
+
See [Event types](#event-types) for every event and when it's sent.
|
|
479
|
+
|
|
480
|
+
Respond with any `2xx` status after processing. Use the event ID to make your handler idempotent because deliveries can be retried.
|
|
481
|
+
|
|
482
|
+
## Async usage
|
|
483
|
+
|
|
484
|
+
`AsyncProntuno` takes the same options and has the same resources and methods as `Prontuno`; await each call and iterate `list_all()` with `async for`:
|
|
485
|
+
|
|
486
|
+
```python
|
|
487
|
+
import asyncio
|
|
488
|
+
import os
|
|
489
|
+
|
|
490
|
+
from prontuno import AsyncProntuno
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
async def main() -> None:
|
|
494
|
+
async with AsyncProntuno(
|
|
495
|
+
sending_key=os.environ["PRONTUNO_SENDING_KEY"],
|
|
496
|
+
management_key=os.environ["PRONTUNO_MANAGEMENT_KEY"],
|
|
497
|
+
) as prontuno:
|
|
498
|
+
await prontuno.emails.send(
|
|
499
|
+
{"from": "hello@mg.example.com", "to": "ada@example.com", "subject": "Hi", "text": "Hi"}
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
async for email in prontuno.emails.list_all(status="delivered"):
|
|
503
|
+
print(email["id"])
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
asyncio.run(main())
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
Pass an `httpx.AsyncClient` as `http_client` to customise the async HTTP layer. The async client waits between retries with `asyncio.sleep`, so it needs an asyncio event loop.
|
|
510
|
+
|
|
511
|
+
## Request options
|
|
512
|
+
|
|
513
|
+
Every resource method accepts per-request keyword options for the timeout, retry count, response size limit, and extra headers. Sends also accept `idempotency_key`:
|
|
514
|
+
|
|
515
|
+
```python
|
|
516
|
+
prontuno.emails.retrieve(
|
|
517
|
+
"email_01kx...",
|
|
518
|
+
timeout=10.0,
|
|
519
|
+
max_retries=1,
|
|
520
|
+
max_response_size=2 * 1024 * 1024,
|
|
521
|
+
headers={"X-Correlation-Id": "job_123"},
|
|
522
|
+
)
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
## Development
|
|
526
|
+
|
|
527
|
+
```bash
|
|
528
|
+
uv sync
|
|
529
|
+
uv run ruff check . && uv run ruff format --check .
|
|
530
|
+
uv run mypy
|
|
531
|
+
uv run pytest --cov
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
## License
|
|
535
|
+
|
|
536
|
+
The Prontuno Python SDK is open-sourced software licensed under the [MIT license](LICENSE.md).
|