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
150 changes: 150 additions & 0 deletions examples/async/automations_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import asyncio
import os

import resend

if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")


async def main() -> None:
# Create and publish a template to use in the automation
print("--- Create and publish template ---")
tpl: resend.Templates.CreateResponse = await resend.Templates.create_async(
{
"name": "welcome-email",
"subject": "Welcome!",
"html": "<strong>Welcome to our service!</strong>",
}
)
await resend.Templates.publish_async(tpl["id"])
print(f"Template: {tpl['id']}")

# --- Create a simple automation (trigger → send_email) ---
print("\n--- Create automation ---")
simple: resend.Automations.CreateResponse = await resend.Automations.create_async(
{
"name": "Welcome Flow",
"status": "disabled",
"steps": [
{
"key": "trigger_1",
"type": "trigger",
"config": {"event_name": "user.created"},
},
{
"key": "send_1",
"type": "send_email",
"config": {"template": {"id": tpl["id"]}},
},
],
"connections": [
{"from": "trigger_1", "to": "send_1"},
],
}
)
automation_id = simple["id"]
print(f"Created automation: {automation_id}")

# --- Get automation ---
print("\n--- Get automation ---")
automation: resend.Automation = await resend.Automations.get_async(automation_id)
print(f"Name: {automation['name']}, status: {automation['status']}")
for step in automation["steps"]:
print(f" Step key={step['key']} type={step['type']}")

# --- Update automation ---
print("\n--- Update automation ---")
updated: resend.Automations.UpdateResponse = await resend.Automations.update_async(
{
"automation_id": automation_id,
"status": "enabled",
}
)
print(f"Updated: {updated['id']}")

# --- List automations ---
print("\n--- List automations ---")
list_resp: resend.Automations.ListResponse = await resend.Automations.list_async()
print(f"Total: {len(list_resp['data'])}, has_more: {list_resp['has_more']}")

# --- Stop automation ---
print("\n--- Stop automation ---")
stopped: resend.Automations.StopResponse = await resend.Automations.stop_async(
automation_id
)
print(f"Stopped: {stopped['id']}, status: {stopped['status']}")

# --- List runs ---
print("\n--- List runs ---")
runs: resend.Automations.Runs.ListResponse = (
await resend.Automations.Runs.list_async(automation_id)
)
print(f"Total runs: {len(runs['data'])}")
if runs["data"]:
run_id = runs["data"][0]["id"]
run: resend.AutomationRun = await resend.Automations.Runs.get_async(
automation_id, run_id
)
print(f"Run status: {run['status']}")

# --- Multi-step automation: delay + wait_for_event ---
print("\n--- Create multi-step automation (delay + wait_for_event) ---")
multi: resend.Automations.CreateResponse = await resend.Automations.create_async(
{
"name": "Onboarding Flow",
"status": "disabled",
"steps": [
{
"key": "trigger_1",
"type": "trigger",
"config": {"event_name": "user.created"},
},
{
"key": "delay_1",
"type": "delay",
# duration is a human-readable string; "seconds" (int) is also accepted
"config": {"duration": "30 minutes"},
},
{
"key": "wait_1",
"type": "wait_for_event",
# timeout is a human-readable string; timeout_seconds is NOT supported
"config": {"event_name": "user.verified", "timeout": "1 hour"},
},
{
"key": "send_1",
"type": "send_email",
"config": {"template": {"id": tpl["id"]}},
},
],
"connections": [
{"from": "trigger_1", "to": "delay_1"},
{"from": "delay_1", "to": "wait_1"},
{"from": "wait_1", "to": "send_1", "type": "event_received"},
{"from": "wait_1", "to": "send_1", "type": "timeout"},
],
}
)
multi_id = multi["id"]
print(f"Created: {multi_id}")

retrieved: resend.Automation = await resend.Automations.get_async(multi_id)
for step in retrieved["steps"]:
print(f" Step key={step['key']} type={step['type']} config={step['config']}")

# --- Cleanup ---
print("\n--- Cleanup ---")
del1: resend.Automations.DeleteResponse = await resend.Automations.remove_async(
automation_id
)
print(f"Deleted automation {del1['id']}: {del1['deleted']}")
del2: resend.Automations.DeleteResponse = await resend.Automations.remove_async(
multi_id
)
print(f"Deleted automation {del2['id']}: {del2['deleted']}")
await resend.Templates.remove_async(tpl["id"])
print(f"Deleted template {tpl['id']}")


asyncio.run(main())
95 changes: 95 additions & 0 deletions examples/async/events_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import asyncio
import os

import resend

if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")


async def main() -> None:
# Create a contact to use with event sends
print("--- Create contact ---")
contact: resend.Contacts.CreateContactResponse = await resend.Contacts.create_async(
{
"email": "test-events-async@example.com",
"first_name": "Test",
"last_name": "User",
}
)
contact_id = contact["id"]
print(f"Contact: {contact_id}")

# --- Create event ---
print("\n--- Create event ---")
created: resend.Events.CreateResponse = await resend.Events.create_async(
{
"name": "user.signed_up",
"schema": {
"plan": "string",
"trial_days": "number",
"is_enterprise": "boolean",
"upgraded_at": "date",
},
}
)
event_id = created["id"]
print(f"Created event: {event_id}")

# --- Get event by ID ---
print("\n--- Get event by ID ---")
event: resend.Event = await resend.Events.get_async(event_id)
print(f"Name: {event['name']}, schema: {event['schema']}")

# --- Get event by name ---
print("\n--- Get event by name ---")
event_by_name: resend.Event = await resend.Events.get_async("user.signed_up")
print(f"Found by name: {event_by_name['name']}")

# --- Update event schema ---
print("\n--- Update event schema ---")
updated: resend.Events.UpdateResponse = await resend.Events.update_async(
{
"identifier": "user.signed_up",
"schema": {"plan": "string", "source": "string"},
}
)
print(f"Updated event: {updated['id']}")

# --- Send event with contact_id ---
print("\n--- Send event with contact_id ---")
sent: resend.Events.SendResponse = await resend.Events.send_async(
{
"event": "user.signed_up",
"contact_id": contact_id,
"payload": {"plan": "pro"},
}
)
print(f"Sent event: {sent['event']}")

# --- Send event with email ---
print("\n--- Send event with email ---")
sent_email: resend.Events.SendResponse = await resend.Events.send_async(
{
"event": "user.signed_up",
"email": "test-events-async@example.com",
}
)
print(f"Sent event: {sent_email['event']}")

# --- List events ---
print("\n--- List events ---")
list_resp: resend.Events.ListResponse = await resend.Events.list_async()
print(f"Total: {len(list_resp['data'])}, has_more: {list_resp['has_more']}")

# --- Cleanup ---
print("\n--- Delete event ---")
deleted: resend.Events.DeleteResponse = await resend.Events.remove_async(event_id)
print(f"Deleted: {deleted['deleted']}")

print("\n--- Delete contact ---")
await resend.Contacts.remove_async(id=contact_id)
print(f"Deleted contact: {contact_id}")


asyncio.run(main())
8 changes: 6 additions & 2 deletions examples/async/receiving_email_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ async def main() -> None:
print("No attachments")

print("\n--- Listing All Received Emails ---")
all_emails: EmailsReceiving.ListResponse = await resend.Emails.Receiving.list_async()
all_emails: EmailsReceiving.ListResponse = (
await resend.Emails.Receiving.list_async()
)

print(f"Total emails in this batch: {len(all_emails['data'])}")
print(f"Has more emails: {all_emails['has_more']}")
Expand Down Expand Up @@ -138,7 +140,9 @@ async def main() -> None:
first_attachment = received_email["attachments"][0]
attachment_id = first_attachment["id"]

print(f"\n--- Retrieving Attachment Details: {first_attachment['filename']} ---")
print(
f"\n--- Retrieving Attachment Details: {first_attachment['filename']} ---"
)

attachment_details: resend.EmailAttachmentDetails = (
await resend.Emails.Receiving.Attachments.get_async(
Expand Down
Loading
Loading