Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions examples/webhook_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ def webhook_handler() -> Tuple[Dict[str, Union[str, bool]], int]:
"signature": request.headers.get("svix-signature", ""),
}

# Verify the webhook
# Verify the webhook. On success, returns the parsed payload.
try:
resend.Webhooks.verify(
payload = resend.Webhooks.verify(
{
"payload": body,
"headers": headers,
Expand All @@ -66,27 +66,23 @@ def webhook_handler() -> Tuple[Dict[str, Union[str, bool]], int]:
print(f"Webhook verification failed: {e}")
return {"error": "Webhook verification failed"}, 400

# Parse the verified payload
try:
payload = json.loads(body)
except json.JSONDecodeError as e:
print(f"Error parsing JSON: {e}")
return {"error": "Invalid JSON payload"}, 400

# Process the webhook event
event_type = payload.get("type")
print("Webhook verified successfully!")
print("Webhook verified successfully!")
print(f"Event Type: {event_type}")
print(f"Payload: {json.dumps(payload, indent=2)}")

print(f"Event: {payload.get('data', {}).get('email_id')}")
data = payload.get("data", {})
print(f"Event email_id: {data.get('email_id')}")
if data.get("message_id"):
print(f"Event message_id: {data.get('message_id')}")

return {"success": True}, 200


if __name__ == "__main__":
port = int(os.getenv("PORT", 5000))
print(f"🚀 Webhook receiver listening on http://localhost:{port}/webhook")
print(f"Webhook receiver listening on http://localhost:{port}/webhook")
print("Send a POST request with Resend webhook headers to test verification")
print(f"Using webhook secret: {WEBHOOK_SECRET[:15]}...")
app.run(host="0.0.0.0", port=port, debug=True)
38 changes: 37 additions & 1 deletion resend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from .contacts._contact_topic import ContactTopic, TopicSubscriptionUpdate
from .contacts._contacts import Contacts
from .contacts._topics import Topics as ContactsTopics
from .contacts.imports._contact_import import ContactImport, ContactImportCounts
from .contacts.imports._contact_import import (ContactImport,
ContactImportCounts)
from .contacts.imports._contact_imports import ContactImports
from .contacts.segments._contact_segment import ContactSegment
from .contacts.segments._contact_segments import ContactSegments
Expand Down Expand Up @@ -61,6 +62,19 @@
from .version import __version__, get_version
from .webhooks._webhook import (VerifyWebhookOptions, Webhook, WebhookEvent,
WebhookHeaders, WebhookStatus)
from .webhooks._webhook_event import (BaseEmailEventData, ContactCreatedEvent,
ContactDeletedEvent, ContactEventData,
ContactUpdatedEvent, DomainCreatedEvent,
DomainDeletedEvent, DomainEventData,
DomainUpdatedEvent, EmailBouncedEvent,
EmailClickedEvent, EmailComplainedEvent,
EmailDeliveredEvent,
EmailDeliveryDelayedEvent,
EmailFailedEvent, EmailOpenedEvent,
EmailReceivedEvent, EmailScheduledEvent,
EmailSentEvent, EmailSuppressedEvent,
ReceivedEmailEventData,
WebhookEventPayload)
from .webhooks._webhooks import Webhooks

# Type for clients that support both sync and async
Expand Down Expand Up @@ -143,9 +157,31 @@
"Variable",
"Webhook",
"WebhookEvent",
"WebhookEventPayload",
"WebhookHeaders",
"WebhookStatus",
"VerifyWebhookOptions",
"BaseEmailEventData",
"ReceivedEmailEventData",
"ContactEventData",
"DomainEventData",
"EmailSentEvent",
"EmailScheduledEvent",
"EmailDeliveredEvent",
"EmailDeliveryDelayedEvent",
"EmailComplainedEvent",
"EmailBouncedEvent",
"EmailOpenedEvent",
"EmailClickedEvent",
"EmailReceivedEvent",
"EmailFailedEvent",
"EmailSuppressedEvent",
"ContactCreatedEvent",
"ContactUpdatedEvent",
"ContactDeletedEvent",
"DomainCreatedEvent",
"DomainUpdatedEvent",
"DomainDeletedEvent",
"Topic",
"OAuthGrant",
"OAuthGrantClient",
Expand Down
Loading
Loading