Skip to content

Commit 1386a6d

Browse files
authored
feat: contact properties implementation + contact topics get/update (#176)
1 parent f2db828 commit 1386a6d

11 files changed

Lines changed: 1022 additions & 0 deletions

File tree

examples/contact_properties.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
3+
import resend
4+
5+
if not os.environ["RESEND_API_KEY"]:
6+
raise EnvironmentError("RESEND_API_KEY is missing")
7+
8+
# Create a contact property
9+
create_params: resend.ContactProperties.CreateParams = {
10+
"key": "age",
11+
"type": "number",
12+
"fallback_value": 0,
13+
}
14+
create_response: resend.ContactProperties.CreateResponse = (
15+
resend.ContactProperties.create(create_params)
16+
)
17+
print(f"Created contact property: {create_response}")
18+
19+
# List all contact properties
20+
list_response: resend.ContactProperties.ListResponse = resend.ContactProperties.list()
21+
print(f"Contact properties: {list_response}")
22+
23+
# List with pagination
24+
list_params: resend.ContactProperties.ListParams = {"limit": 10}
25+
paginated_response: resend.ContactProperties.ListResponse = (
26+
resend.ContactProperties.list(list_params)
27+
)
28+
print(f"Limited contact properties: {paginated_response}")
29+
print(f"Has more: {paginated_response.get('has_more', False)}")
30+
31+
# Get a specific contact property
32+
property_id: str = create_response["id"]
33+
property_details: resend.ContactProperty = resend.ContactProperties.get(property_id)
34+
print(f"Contact property details: {property_details}")
35+
36+
# Update a contact property
37+
update_params: resend.ContactProperties.UpdateParams = {
38+
"id": property_id,
39+
"fallback_value": 18,
40+
}
41+
update_response: resend.ContactProperties.UpdateResponse = (
42+
resend.ContactProperties.update(update_params)
43+
)
44+
print(f"Updated contact property: {update_response}")
45+
46+
# Remove a contact property
47+
remove_response: resend.ContactProperties.RemoveResponse = (
48+
resend.ContactProperties.remove(property_id)
49+
)
50+
print(f"Removed contact property: {remove_response}")

examples/contacts.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,85 @@
6565
else:
6666
print("No contacts available for pagination example")
6767

68+
print("\n--- Contact Topics Examples ---")
69+
70+
print("\nCreating a topic...")
71+
topic_create_params: resend.Topics.CreateParams = {
72+
"name": "Product Updates",
73+
"default_subscription": "opt_in",
74+
"description": "Latest product updates and features",
75+
}
76+
created_topic: resend.Topics.CreateTopicResponse = resend.Topics.create(
77+
topic_create_params
78+
)
79+
print(f"Created topic with ID: {created_topic['id']}")
80+
81+
topic_create_params_2: resend.Topics.CreateParams = {
82+
"name": "Newsletter",
83+
"default_subscription": "opt_out",
84+
"description": "Weekly newsletter",
85+
}
86+
created_topic_2: resend.Topics.CreateTopicResponse = resend.Topics.create(
87+
topic_create_params_2
88+
)
89+
print(f"Created topic with ID: {created_topic_2['id']}")
90+
91+
print("\nListing topics for contact by ID...")
92+
topics_response: resend.ContactsTopics.ListResponse = resend.Contacts.Topics.list(
93+
contact_id=contact["id"]
94+
)
95+
print(f"Found {len(topics_response['data'])} topics for contact")
96+
for topic in topics_response["data"]:
97+
print(f" - {topic['name']}: {topic['subscription']}")
98+
99+
print("\nListing topics for contact by email...")
100+
topics_by_email: resend.ContactsTopics.ListResponse = resend.Contacts.Topics.list(
101+
email="sw@exmple.com"
102+
)
103+
print(f"Found {len(topics_by_email['data'])} topics for contact")
104+
105+
# Update topic subscriptions for a contact by ID
106+
print("\nUpdating topic subscriptions by contact ID...")
107+
update_topics_params: resend.ContactsTopics.UpdateParams = {
108+
"id": contact["id"],
109+
"topics": [
110+
{"id": created_topic["id"], "subscription": "opt_in"},
111+
{"id": created_topic_2["id"], "subscription": "opt_out"},
112+
],
113+
}
114+
update_topics_response: resend.ContactsTopics.UpdateResponse = (
115+
resend.Contacts.Topics.update(update_topics_params)
116+
)
117+
print(f"Updated topics for contact: {update_topics_response['id']}")
118+
119+
# Update topic subscriptions for a contact by email
120+
print("\nUpdating topic subscriptions by contact email...")
121+
update_topics_by_email: resend.ContactsTopics.UpdateParams = {
122+
"email": "sw@exmple.com",
123+
"topics": [
124+
{"id": created_topic["id"], "subscription": "opt_in"},
125+
{"id": created_topic_2["id"], "subscription": "opt_in"},
126+
],
127+
}
128+
update_by_email_response: resend.ContactsTopics.UpdateResponse = (
129+
resend.Contacts.Topics.update(update_topics_by_email)
130+
)
131+
print(f"Updated topics for contact by email: {update_by_email_response['id']}")
132+
133+
# List topics again to see the updates
134+
print("\nListing topics after updates...")
135+
updated_topics: resend.ContactsTopics.ListResponse = resend.Contacts.Topics.list(
136+
contact_id=contact["id"]
137+
)
138+
for topic in updated_topics["data"]:
139+
print(f" - {topic['name']}: {topic['subscription']}")
140+
141+
# Clean up: remove the topics we created
142+
print("\nCleaning up topics...")
143+
resend.Topics.remove(created_topic["id"])
144+
resend.Topics.remove(created_topic_2["id"])
145+
print("Topics removed")
146+
68147
# remove by email
69148
rmed: resend.Contacts.RemoveContactResponse = resend.Contacts.remove(
70149
audience_id=audience_id, email=cont_by_email["email"]

resend/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
from .audiences._audiences import Audiences
77
from .broadcasts._broadcast import Broadcast
88
from .broadcasts._broadcasts import Broadcasts
9+
from .contact_properties._contact_properties import ContactProperties
10+
from .contact_properties._contact_property import ContactProperty
911
from .contacts._contact import Contact
12+
from .contacts._contact_topic import ContactTopic, TopicSubscriptionUpdate
1013
from .contacts._contacts import Contacts
14+
from .contacts._topics import Topics as ContactsTopics
1115
from .domains._domain import Domain
1216
from .domains._domains import Domains
1317
from .emails._attachment import Attachment, RemoteAttachment
@@ -48,13 +52,17 @@
4852
"Batch",
4953
"Audiences",
5054
"Contacts",
55+
"ContactProperties",
5156
"Broadcasts",
5257
"Templates",
5358
"Webhooks",
5459
"Topics",
5560
# Types
5661
"Audience",
5762
"Contact",
63+
"ContactProperty",
64+
"ContactTopic",
65+
"TopicSubscriptionUpdate",
5866
"Domain",
5967
"ApiKey",
6068
"Email",
@@ -80,6 +88,7 @@
8088
# Receiving types (for type hints)
8189
"EmailsReceiving",
8290
"EmailAttachments",
91+
"ContactsTopics",
8392
# Default HTTP Client
8493
"RequestsClient",
8594
]

resend/contact_properties/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)