Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
132 changes: 132 additions & 0 deletions examples/async/automations_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
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())
85 changes: 85 additions & 0 deletions examples/async/events_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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())
135 changes: 135 additions & 0 deletions examples/automations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import os

import resend

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

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

# --- Create a simple automation (trigger → send_email) ---
print("\n--- Create automation ---")
simple: resend.Automations.CreateResponse = resend.Automations.create({
"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 = resend.Automations.get(automation_id)
print(f"Name: {automation['name']}, status: {automation['status']}")
for step in automation["steps"]:
print(f" Step key={step['key']} type={step['type']}")
for conn in automation["connections"]:
print(f" Connection: {conn['from']} -> {conn['to']}")

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

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

list_enabled: resend.Automations.ListResponse = resend.Automations.list(
params={"status": "enabled", "limit": 10}
)
print(f"Enabled: {len(list_enabled['data'])}")

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

# --- List runs ---
print("\n--- List runs ---")
runs: resend.Automations.Runs.ListResponse = resend.Automations.Runs.list(automation_id)
print(f"Total runs: {len(runs['data'])}")
if runs["data"]:
run_id = runs["data"][0]["id"]
run: resend.AutomationRun = resend.Automations.Runs.get(automation_id, run_id)
print(f"Run status: {run['status']}, steps: {len(run['steps'])}")
for step in run["steps"]:
print(f" Step key={step['key']} type={step['type']} status={step['status']}")

# --- Multi-step automation: delay + wait_for_event ---
print("\n--- Create multi-step automation (delay + wait_for_event) ---")
multi: resend.Automations.CreateResponse = resend.Automations.create({
"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 = resend.Automations.get(multi_id)
for step in retrieved["steps"]:
print(f" Step key={step['key']} type={step['type']} config={step['config']}")

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