|
| 1 | +import os |
| 2 | +from typing import List |
| 3 | + |
| 4 | +import resend |
| 5 | + |
| 6 | +if not os.environ["RESEND_API_KEY"]: |
| 7 | + raise EnvironmentError("RESEND_API_KEY is missing") |
| 8 | + |
| 9 | +# Create a new segment |
| 10 | +create_params: resend.Segments.CreateParams = { |
| 11 | + "name": "VIP Newsletter Subscribers", |
| 12 | +} |
| 13 | +segment: resend.Segments.CreateSegmentResponse = resend.Segments.create(create_params) |
| 14 | +print(f"\n✓ Created segment: {segment['id']}") |
| 15 | +print(f" Name: {segment['name']}") |
| 16 | + |
| 17 | +# Get segment details |
| 18 | +seg: resend.Segment = resend.Segments.get(segment["id"]) |
| 19 | +print(f"\n✓ Retrieved segment: {seg['name']}") |
| 20 | +print(f" Created at: {seg['created_at']}") |
| 21 | + |
| 22 | +# List all segments |
| 23 | +segments: resend.Segments.ListResponse = resend.Segments.list() |
| 24 | +print(f"\n✓ List of segments: {[s['name'] for s in segments['data']]}") |
| 25 | +print(f" Has more: {segments['has_more']}") |
| 26 | + |
| 27 | +# List with pagination |
| 28 | +if segments["data"]: |
| 29 | + paginated_params: resend.Segments.ListParams = { |
| 30 | + "limit": 5, |
| 31 | + "after": segments["data"][0]["id"], |
| 32 | + } |
| 33 | + paginated_segments: resend.Segments.ListResponse = resend.Segments.list( |
| 34 | + params=paginated_params |
| 35 | + ) |
| 36 | + print(f"\n✓ Paginated results: {len(paginated_segments['data'])} segments") |
| 37 | + |
| 38 | +# First, create a contact (using the legacy audience_id approach) |
| 39 | +# Note: Contacts API still uses audience_id, not segment_id |
| 40 | +contact_params: resend.Contacts.CreateParams = { |
| 41 | + "audience_id": segment["id"], # Segments and audiences are now the same |
| 42 | + "email": "vip@example.com", |
| 43 | + "first_name": "VIP", |
| 44 | + "last_name": "User", |
| 45 | +} |
| 46 | +contact = resend.Contacts.create(contact_params) |
| 47 | +print(f"\n✓ Created contact: {contact['id']}") |
| 48 | + |
| 49 | +# Add contact to segment (using the new Contacts.segments sub-API) |
| 50 | +add_params: resend.ContactSegments.AddParams = { |
| 51 | + "segment_id": segment["id"], |
| 52 | + "contact_id": contact["id"], |
| 53 | +} |
| 54 | +add_response = resend.Contacts.Segments.add(add_params) |
| 55 | +print(f"\n✓ Added contact to segment: {add_response['id']}") |
| 56 | + |
| 57 | +# Alternative: Add by email instead of contact_id |
| 58 | +add_by_email_params: resend.ContactSegments.AddParams = { |
| 59 | + "segment_id": segment["id"], |
| 60 | + "email": "another-vip@example.com", |
| 61 | +} |
| 62 | +# add_response2 = resend.Contacts.Segments.add(add_by_email_params) |
| 63 | +# print(f"✓ Added contact (by email) to segment: {add_response2['id']}") |
| 64 | + |
| 65 | +# List all segments for a contact |
| 66 | +list_params: resend.ContactSegments.ListParams = { |
| 67 | + "contact_id": contact["id"], |
| 68 | +} |
| 69 | +contact_segments = resend.Contacts.Segments.list(list_params) |
| 70 | +print(f"\n✓ Contact is in {len(contact_segments['data'])} segment(s):") |
| 71 | +for cs in contact_segments["data"]: |
| 72 | + print(f" - {cs['name']} (ID: {cs['id']})") |
| 73 | + |
| 74 | +# Alternative: List by email |
| 75 | +list_by_email_params: resend.ContactSegments.ListParams = { |
| 76 | + "email": "vip@example.com", |
| 77 | +} |
| 78 | +# contact_segments2 = resend.Contacts.Segments.list(list_by_email_params) |
| 79 | + |
| 80 | +# Remove contact from segment |
| 81 | +remove_params: resend.ContactSegments.RemoveParams = { |
| 82 | + "segment_id": segment["id"], |
| 83 | + "contact_id": contact["id"], |
| 84 | +} |
| 85 | +remove_response = resend.Contacts.Segments.remove(remove_params) |
| 86 | +print(f"\n✓ Removed contact from segment") |
| 87 | +print(f" Deleted: {remove_response['deleted']}") |
| 88 | + |
| 89 | +# Clean up: Remove the contact |
| 90 | +resend.Contacts.remove(audience_id=segment["id"], id=contact["id"]) |
| 91 | +print(f"\n✓ Deleted contact: {contact['id']}") |
| 92 | + |
| 93 | +# Clean up: Remove the segment |
| 94 | +rmed: resend.Segments.RemoveSegmentResponse = resend.Segments.remove(id=segment["id"]) |
| 95 | +print(f"\n✓ Deleted segment: {segment['id']}") |
| 96 | +print(f" Deleted: {rmed['deleted']}") |
0 commit comments