-
Notifications
You must be signed in to change notification settings - Fork 14
fix: rename http response headers key to http_headers to avoid collision #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,44 +1,80 @@ | ||||||||
| """ | ||||||||
| Example demonstrating how to access response headers. | ||||||||
| Example demonstrating the three different types of headers in the Resend Python SDK: | ||||||||
|
|
||||||||
| Response headers include useful information like rate limits, request IDs, etc. | ||||||||
| 1. Email headers (SendParams["headers"]): Custom MIME headers added to the outgoing | ||||||||
| email itself, visible to the recipient's mail client (e.g. X-Entity-Ref-ID). | ||||||||
|
|
||||||||
| 2. HTTP response headers (response["http_headers"]): HTTP-level metadata returned | ||||||||
| by the Resend API, such as rate limit info and request IDs. These are injected | ||||||||
| by the SDK and are never part of the email content. | ||||||||
|
|
||||||||
| 3. Inbound email MIME headers (email["headers"]): MIME headers present on a received | ||||||||
| email, returned as part of the API response body (e.g. X-Mailer, DKIM-Signature). | ||||||||
| """ | ||||||||
|
|
||||||||
| import os | ||||||||
|
|
||||||||
| import resend | ||||||||
|
|
||||||||
| if not os.environ["RESEND_API_KEY"]: | ||||||||
| raise EnvironmentError("RESEND_API_KEY is missing") | ||||||||
| resend.api_key = os.environ["RESEND_API_KEY"] | ||||||||
|
|
||||||||
| # --- Example 1: Custom email headers (part of the outgoing email itself) --- | ||||||||
|
|
||||||||
| params: resend.Emails.SendParams = { | ||||||||
| "from": "onboarding@resend.dev", | ||||||||
| "to": ["delivered@resend.dev"], | ||||||||
| "subject": "Hello from Resend", | ||||||||
| "html": "<strong>Hello, world!</strong>", | ||||||||
| "headers": { | ||||||||
| "X-Entity-Ref-ID": "123456789", | ||||||||
| }, | ||||||||
| } | ||||||||
|
|
||||||||
| resp: resend.Emails.SendResponse = resend.Emails.send(params) | ||||||||
| print(f"Email sent! ID: {resp['id']}") | ||||||||
|
|
||||||||
| if "headers" in resp: | ||||||||
| print(f"Request ID: {resp['headers'].get('x-request-id')}") | ||||||||
| print(f"Rate limit: {resp['headers'].get('x-ratelimit-limit')}") | ||||||||
| print(f"Rate limit remaining: {resp['headers'].get('x-ratelimit-remaining')}") | ||||||||
| print(f"Rate limit reset: {resp['headers'].get('x-ratelimit-reset')}") | ||||||||
| # --- Example 2: HTTP response headers (SDK metadata, not part of the email) --- | ||||||||
|
|
||||||||
| if "http_headers" in resp: | ||||||||
| print(f"Rate limit: {resp['http_headers'].get('ratelimit-limit')}") | ||||||||
| print(f"Rate limit remaining: {resp['http_headers'].get('ratelimit-remaining')}") | ||||||||
| print(f"Rate limit reset: {resp['http_headers'].get('ratelimit-reset')}") | ||||||||
|
|
||||||||
| # --- Example 3: Inbound email MIME headers (from a received email response body) --- | ||||||||
|
|
||||||||
| # Replace with a real received email ID | ||||||||
| received_email_id = os.environ.get("RECEIVED_EMAIL_ID", "") | ||||||||
|
|
||||||||
| if received_email_id: | ||||||||
| received: resend.ReceivedEmail = resend.Emails.Receiving.get( | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Custom agent: API Key Permission Check SDK Methods
Prompt for AI agents
Suggested change
|
||||||||
| email_id=received_email_id | ||||||||
| ) | ||||||||
|
|
||||||||
| # email["headers"] — MIME headers of the inbound email, part of the API response body. | ||||||||
| # Completely separate from http_headers injected by the SDK. | ||||||||
| if received.get("headers"): | ||||||||
| print("Inbound email MIME headers:") | ||||||||
| for name, value in received["headers"].items(): | ||||||||
| print(f" {name}: {value}") | ||||||||
|
|
||||||||
| # http_headers are also available on received email responses | ||||||||
| if received.get("http_headers"): | ||||||||
| print( | ||||||||
| f"Rate limit remaining: {received['http_headers'].get('ratelimit-remaining')}" | ||||||||
| ) | ||||||||
| else: | ||||||||
| print("Set RECEIVED_EMAIL_ID env var to run the inbound email headers example.") | ||||||||
|
|
||||||||
| print("\n") | ||||||||
| print("Example 3: Rate limit tracking") | ||||||||
| # --- Example 4: Rate limit tracking via HTTP response headers --- | ||||||||
|
|
||||||||
|
|
||||||||
| def send_with_rate_limit_check(params: resend.Emails.SendParams) -> str: | ||||||||
| """Example function showing how to track rate limits.""" | ||||||||
| response = resend.Emails.send(params) | ||||||||
|
|
||||||||
| # Access headers via dict key | ||||||||
| headers = response.get("headers", {}) | ||||||||
| remaining = headers.get("x-ratelimit-remaining") | ||||||||
| limit = headers.get("x-ratelimit-limit") | ||||||||
| http_headers = response.get("http_headers", {}) | ||||||||
| remaining = http_headers.get("ratelimit-remaining") | ||||||||
| limit = http_headers.get("ratelimit-limit") | ||||||||
|
|
||||||||
| if remaining and limit: | ||||||||
| print(f"Rate limit usage: {int(limit) - int(remaining)}/{limit}") | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| __version__ = "2.24.0" | ||
| __version__ = "2.25.0" | ||
|
|
||
|
|
||
| def get_version() -> str: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.