|
1 | 1 | """ |
2 | | -Example demonstrating how to access response headers. |
| 2 | +Example demonstrating the three different types of headers in the Resend Python SDK: |
3 | 3 |
|
4 | | -Response headers include useful information like rate limits, request IDs, etc. |
| 4 | +1. Email headers (SendParams["headers"]): Custom MIME headers added to the outgoing |
| 5 | + email itself, visible to the recipient's mail client (e.g. X-Entity-Ref-ID). |
| 6 | +
|
| 7 | +2. HTTP response headers (response["http_headers"]): HTTP-level metadata returned |
| 8 | + by the Resend API, such as rate limit info and request IDs. These are injected |
| 9 | + by the SDK and are never part of the email content. |
| 10 | +
|
| 11 | +3. Inbound email MIME headers (email["headers"]): MIME headers present on a received |
| 12 | + email, returned as part of the API response body (e.g. X-Mailer, DKIM-Signature). |
5 | 13 | """ |
6 | 14 |
|
7 | 15 | import os |
8 | 16 |
|
9 | 17 | import resend |
10 | 18 |
|
11 | | -if not os.environ["RESEND_API_KEY"]: |
12 | | - raise EnvironmentError("RESEND_API_KEY is missing") |
| 19 | +resend.api_key = os.environ["RESEND_API_KEY"] |
| 20 | + |
| 21 | +# --- Example 1: Custom email headers (part of the outgoing email itself) --- |
13 | 22 |
|
14 | 23 | params: resend.Emails.SendParams = { |
15 | 24 | "from": "onboarding@resend.dev", |
16 | 25 | "to": ["delivered@resend.dev"], |
17 | 26 | "subject": "Hello from Resend", |
18 | 27 | "html": "<strong>Hello, world!</strong>", |
| 28 | + "headers": { |
| 29 | + "X-Entity-Ref-ID": "123456789", |
| 30 | + }, |
19 | 31 | } |
20 | 32 |
|
21 | 33 | resp: resend.Emails.SendResponse = resend.Emails.send(params) |
22 | 34 | print(f"Email sent! ID: {resp['id']}") |
23 | 35 |
|
24 | | -if "headers" in resp: |
25 | | - print(f"Request ID: {resp['headers'].get('x-request-id')}") |
26 | | - print(f"Rate limit: {resp['headers'].get('x-ratelimit-limit')}") |
27 | | - print(f"Rate limit remaining: {resp['headers'].get('x-ratelimit-remaining')}") |
28 | | - print(f"Rate limit reset: {resp['headers'].get('x-ratelimit-reset')}") |
| 36 | +# --- Example 2: HTTP response headers (SDK metadata, not part of the email) --- |
| 37 | + |
| 38 | +if "http_headers" in resp: |
| 39 | + print(f"Rate limit: {resp['http_headers'].get('ratelimit-limit')}") |
| 40 | + print(f"Rate limit remaining: {resp['http_headers'].get('ratelimit-remaining')}") |
| 41 | + print(f"Rate limit reset: {resp['http_headers'].get('ratelimit-reset')}") |
| 42 | + |
| 43 | +# --- Example 3: Inbound email MIME headers (from a received email response body) --- |
| 44 | + |
| 45 | +# Replace with a real received email ID |
| 46 | +received_email_id = os.environ.get("RECEIVED_EMAIL_ID", "") |
| 47 | + |
| 48 | +if received_email_id: |
| 49 | + received: resend.ReceivedEmail = resend.Emails.Receiving.get( |
| 50 | + email_id=received_email_id |
| 51 | + ) |
| 52 | + |
| 53 | + # email["headers"] — MIME headers of the inbound email, part of the API response body. |
| 54 | + # Completely separate from http_headers injected by the SDK. |
| 55 | + if received.get("headers"): |
| 56 | + print("Inbound email MIME headers:") |
| 57 | + for name, value in received["headers"].items(): |
| 58 | + print(f" {name}: {value}") |
| 59 | + |
| 60 | + # http_headers are also available on received email responses |
| 61 | + if received.get("http_headers"): |
| 62 | + print( |
| 63 | + f"Rate limit remaining: {received['http_headers'].get('ratelimit-remaining')}" |
| 64 | + ) |
| 65 | +else: |
| 66 | + print("Set RECEIVED_EMAIL_ID env var to run the inbound email headers example.") |
29 | 67 |
|
30 | | -print("\n") |
31 | | -print("Example 3: Rate limit tracking") |
| 68 | +# --- Example 4: Rate limit tracking via HTTP response headers --- |
32 | 69 |
|
33 | 70 |
|
34 | 71 | def send_with_rate_limit_check(params: resend.Emails.SendParams) -> str: |
35 | 72 | """Example function showing how to track rate limits.""" |
36 | 73 | response = resend.Emails.send(params) |
37 | 74 |
|
38 | | - # Access headers via dict key |
39 | | - headers = response.get("headers", {}) |
40 | | - remaining = headers.get("x-ratelimit-remaining") |
41 | | - limit = headers.get("x-ratelimit-limit") |
| 75 | + http_headers = response.get("http_headers", {}) |
| 76 | + remaining = http_headers.get("ratelimit-remaining") |
| 77 | + limit = http_headers.get("ratelimit-limit") |
42 | 78 |
|
43 | 79 | if remaining and limit: |
44 | 80 | print(f"Rate limit usage: {int(limit) - int(remaining)}/{limit}") |
|
0 commit comments