|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../lib/resend" |
| 4 | + |
| 5 | +raise if ENV["RESEND_API_KEY"].nil? |
| 6 | + |
| 7 | +Resend.api_key = ENV["RESEND_API_KEY"] |
| 8 | + |
| 9 | +# 1. Create a template (needed for the send_email step config) |
| 10 | +template = Resend::Templates.create({ |
| 11 | + name: "Welcome Email", |
| 12 | + from: "onboarding@resend.dev", |
| 13 | + subject: "Welcome!", |
| 14 | + html: "<p>Welcome to our platform!</p>" |
| 15 | +}) |
| 16 | +template_id = template[:id] |
| 17 | +puts "created template: #{template_id}" |
| 18 | + |
| 19 | +Resend::Templates.publish(template_id) |
| 20 | +puts "published template: #{template_id}" |
| 21 | + |
| 22 | +# 2. Create a simple automation: trigger → send_email |
| 23 | +simple_automation = Resend::Automations.create({ |
| 24 | + name: "Simple Welcome Automation", |
| 25 | + steps: [ |
| 26 | + { |
| 27 | + key: "trigger", |
| 28 | + type: "trigger", |
| 29 | + config: { event_name: "user.created" } |
| 30 | + }, |
| 31 | + { |
| 32 | + key: "send_welcome", |
| 33 | + type: "send_email", |
| 34 | + config: { template: { id: template_id } } |
| 35 | + } |
| 36 | + ], |
| 37 | + connections: [ |
| 38 | + { from: "trigger", to: "send_welcome", type: "default" } |
| 39 | + ] |
| 40 | +}) |
| 41 | +automation_id = simple_automation[:id] |
| 42 | +puts "created simple automation: #{automation_id}" |
| 43 | + |
| 44 | +# 3. Get automation |
| 45 | +retrieved = Resend::Automations.get(automation_id) |
| 46 | +puts "retrieved automation: #{retrieved[:id]}, status: #{retrieved[:status]}" |
| 47 | + |
| 48 | +# 4. Update automation (change status to enabled) |
| 49 | +updated = Resend::Automations.update({ |
| 50 | + automation_id: automation_id, |
| 51 | + status: "enabled" |
| 52 | +}) |
| 53 | +puts "updated automation: #{updated[:id]}" |
| 54 | + |
| 55 | +# 5. List automations (without filter) |
| 56 | +all_automations = Resend::Automations.list |
| 57 | +puts "automations: #{all_automations[:data].length}, has_more: #{all_automations[:has_more]}" |
| 58 | + |
| 59 | +# List automations with status filter |
| 60 | +enabled_automations = Resend::Automations.list({ status: "enabled" }) |
| 61 | +puts "enabled automations: #{enabled_automations[:data].length}" |
| 62 | + |
| 63 | +# 6. Stop automation |
| 64 | +stopped = Resend::Automations.stop(automation_id) |
| 65 | +puts "stopped automation: #{stopped[:id]}" |
| 66 | + |
| 67 | +# 7. List runs (Resend::Automations::Runs.list) |
| 68 | +runs = Resend::Automations::Runs.list(automation_id) |
| 69 | +puts "runs: #{runs[:data].length}, has_more: #{runs[:has_more]}" |
| 70 | + |
| 71 | +# 8. Get a run if any exist (Resend::Automations::Runs.get) |
| 72 | +if runs[:data].any? |
| 73 | + run_id = runs[:data].first[:id] |
| 74 | + run = Resend::Automations::Runs.get(automation_id, run_id) |
| 75 | + puts "run: #{run[:id]}, status: #{run[:status]}" |
| 76 | +else |
| 77 | + puts "no runs yet for this automation" |
| 78 | +end |
| 79 | + |
| 80 | +# 9. Multi-step automation: trigger → delay → wait_for_event → send_email |
| 81 | +multi_automation = Resend::Automations.create({ |
| 82 | + name: "Multi-step Onboarding Automation", |
| 83 | + steps: [ |
| 84 | + { |
| 85 | + key: "trigger", |
| 86 | + type: "trigger", |
| 87 | + config: { event_name: "user.created" } |
| 88 | + }, |
| 89 | + { |
| 90 | + key: "wait_30_min", |
| 91 | + type: "delay", |
| 92 | + config: { duration: "30 minutes" } |
| 93 | + }, |
| 94 | + { |
| 95 | + key: "wait_verified", |
| 96 | + type: "wait_for_event", |
| 97 | + config: { event_name: "user.verified", timeout: "1 hour" } |
| 98 | + }, |
| 99 | + { |
| 100 | + key: "send_welcome", |
| 101 | + type: "send_email", |
| 102 | + config: { template: { id: template_id } } |
| 103 | + } |
| 104 | + ], |
| 105 | + connections: [ |
| 106 | + { from: "trigger", to: "wait_30_min", type: "default" }, |
| 107 | + { from: "wait_30_min", to: "wait_verified", type: "default" }, |
| 108 | + { from: "wait_verified", to: "send_welcome", type: "event_received" } |
| 109 | + ] |
| 110 | +}) |
| 111 | +multi_automation_id = multi_automation[:id] |
| 112 | +puts "created multi-step automation: #{multi_automation_id}" |
| 113 | + |
| 114 | +# 10. Cleanup: delete both automations and the template |
| 115 | +Resend::Automations.remove(automation_id) |
| 116 | +puts "removed automation: #{automation_id}" |
| 117 | + |
| 118 | +Resend::Automations.remove(multi_automation_id) |
| 119 | +puts "removed multi-step automation: #{multi_automation_id}" |
| 120 | + |
| 121 | +Resend::Templates.remove(template_id) |
| 122 | +puts "removed template: #{template_id}" |
0 commit comments