Automations API
Relevant source files
The following files were used as context for generating this wiki page:
The Automations API manages automated workflows within the Resend ecosystem. It allows you to create, retrieve, update, and monitor sequences of actions triggered by specific events. These workflows integrate with other modules such as Templates and Events to execute tasks like sending emails or managing contact states automatically.
Sources: automations.go:1-10, examples/automations.go:15-50
Architecture and Core Components
The system architecture relies on three primary entities: Automations, Steps, and Connections. An Automation serves as the container for the workflow. Steps represent discrete actions or triggers, and Connections define the logic flow between these steps.
Sources: automations.go:48-75
Automation Steps
Steps are the building blocks of a workflow. Each step requires a unique Key, a Type, and a Config map that stores parameters specific to the step type.
| Step Type | Description | Config Requirements |
|---|---|---|
trigger | Initiates the workflow. | event_name (e.g., "user.created") |
send_email | Sends an email via a template. | template object with id |
delay | Pauses execution. | duration (string) or seconds (int) |
wait_for_event | Pauses until an event occurs. | event_name, timeout (string) |
condition | Branches the workflow logic. | Logic-specific parameters |
contact_update | Modifies contact information. | Attribute mapping |
Sources: automations.go:31-40, examples/automations.go:167-195
Automation Connections
Connections link steps together using From and To keys. Some steps, like wait_for_event, support specific connection types to handle different outcomes.
| Connection Type | Description |
|---|---|
default | Standard sequential flow. |
event_received | Path taken when a waited event occurs. |
timeout | Path taken when a wait step expires. |
condition_met | Path taken when a condition is true. |
condition_not_met | Path taken when a condition is false. |
Sources: automations.go:42-46, examples/automations.go:200-205
Workflow Structure
The following diagram illustrates a multi-step onboarding flow utilizing triggers, delays, and event waiting.
This diagram shows the transition from a trigger through a delay to a branching wait step based on event results.
Sources: examples/automations.go:167-206, automations_test.go:264-282
API Service Interface
The AutomationsSvc interface defines the methods available for interacting with the API. The implementation handles HTTP requests to the /automations endpoints.
Management Methods
Create: Generates a new automation.Update: Modifies existing automation details or status.Remove: Deletes an automation by ID.Duplicate: Creates a copy of an existing automation.Stop: Disables a currently active automation.
Retrieval Methods
Get: Fetches a single automation's structure and configuration.List: Returns a list of automations, support filters forstatus(enabled/disabled).
Sources: automations.go:142-162, automations_test.go:138-150
Automation Runs
An "Automation Run" represents a single execution instance of an automation workflow. The API provides visibility into the execution status and output of individual steps.
This diagram represents the lifecycle of a single automation execution.
Sources: automations.go:121-133, automations_test.go:343-365
Run Statuses
Execution instances can have the following statuses:
running: The workflow is currently active.completed: All steps finished successfully.failed: An error occurred during execution.cancelled: The run was manually aborted.
Sources: automations.go:23-29
Error Handling
The Automations API uses specific error constants to identify failures during request construction or execution. These are defined in the central error module.
| Error Constant | Description |
|---|---|
ErrFailedToCreateAutomationCreateRequest | Request failed during creation phase. |
ErrFailedToCreateAutomationStopRequest | Could not generate the stop command. |
ErrFailedToCreateAutomationListRunsRequest | Error fetching execution history. |
ErrFailedToCreateAutomationGetRunRequest | Error retrieving specific run details. |
Sources: errors.go:111-121
Implementation Example
The following code illustrates creating an automation that links a user creation event to a welcome email.
automation, err := client.Automations.Create(&resend.CreateAutomationRequest{
Name: "Welcome Flow",
Status: resend.AutomationStatusEnabled,
Steps: []resend.AutomationStep{
{
Key: "start",
Type: resend.AutomationStepTypeTrigger,
Config: map[string]any{"event_name": "user.created"},
},
{
Key: "email",
Type: resend.AutomationStepTypeSendEmail,
Config: map[string]any{"template": map[string]any{"id": "tmpl_123"}},
},
},
Connections: []resend.AutomationConnection{
{From: "start", To: "email"},
},
})
Sources: examples/automations.go:41-65, automations_test.go:17-30
The Automations API provides a robust framework for building event-driven communications. By combining triggers, logical steps, and detailed run tracking, developers can automate complex lifecycle workflows directly through the Resend Go SDK.
Sources: automations.go, examples/automations.go