# Automations

An automation walks each person through a flow of emails, waits and conditions. Something starts it, such as an event from your app, and from there it runs by itself.

## What an automation is made of

- A trigger: what starts a run. An event you send, a built-in event, a schedule, a date on each contact, or nothing but your own call.
- Steps: what a run does, in order. The first step in the list is where every run starts.
- Rules: the time zone, the sending hours, who may enter again, and what ends a run early.

Each time a person enters, a run is made for them. A run remembers where the person is, the data that started it and what every step did.

You always edit a copy. Publishing makes that copy the one that runs, as a new version, and a run finishes on the version it entered on. So an edit never changes a flow under the people who are in it.

## Create and publish

An automation starts as a draft. A step names the step that follows it with `next`, and a step that goes two ways names both with `yes` and `no`. Without `next`, the step after it in the list follows, and `null` ends the run there.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/automations" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Welcome",
  "trigger": {
    "kind": "event",
    "event": "user.signed_up"
  },
  "steps": [
    {
      "id": "welcome",
      "kind": "send_email",
      "from": "Acme <hello@mail.example.com>",
      "subject": "Welcome to Acme",
      "html": "<p>Glad you are here.</p>"
    }
  ]
}'
```

TypeScript:

```ts
await opensend.automations.create({
  name: 'Welcome',
  trigger: {
    kind: 'event',
    event: 'user.signed_up',
  },
  steps: [
    {
      id: 'welcome',
      kind: 'send_email',
      from: 'Acme <hello@mail.example.com>',
      subject: 'Welcome to Acme',
      html: '<p>Glad you are here.</p>',
    },
  ],
})
```

CLI:

```bash
opensend automations create \
  --name Welcome \
  --trigger '{"kind":"event","event":"user.signed_up"}' \
  --steps '[{"id":"welcome","kind":"send_email","from":"Acme <hello@mail.example.com>","subject":"Welcome to Acme","html":"<p>Glad you are here.</p>"}]'
```

MCP:

```json
{
  "name": "automations_create",
  "arguments": {
    "name": "Welcome",
    "trigger": {
      "kind": "event",
      "event": "user.signed_up"
    },
    "steps": [
      {
        "id": "welcome",
        "kind": "send_email",
        "from": "Acme <hello@mail.example.com>",
        "subject": "Welcome to Acme",
        "html": "<p>Glad you are here.</p>"
      }
    ]
  }
}
```

The answer lists `problems`: what is missing or wrong, with the step and the field each one is about. A draft can be saved with problems. It cannot be published while one of them is `blocking`.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/automations/auto_teams/publish" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.automations.publish('auto_teams')
```

CLI:

```bash
opensend automations publish auto_teams
```

MCP:

```json
{
  "name": "automations_publish",
  "arguments": {
    "id": "auto_teams"
  }
}
```

- Publishing a draft also starts it. Publishing an active or a paused automation keeps its state.
- `automation_incomplete`: something blocks the publish. The message names the step and says what is missing.
- A sender has to be on a verified domain, a template has to be published, and an audience or a webhook a step names has to exist.

## Start it with an event

An event says that something happened to one person. Send it from your app with an address, or the id of a contact, and any data that belongs to it.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/events/send" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "order.paid",
  "email": "ada@example.com",
  "data": {
    "order_id": "1042",
    "total": 49
  }
}'
```

TypeScript:

```ts
await opensend.events.send({
  name: 'order.paid',
  email: 'ada@example.com',
  data: {
    order_id: '1042',
    total: 49,
  },
})
```

CLI:

```bash
opensend events send \
  --name order.paid \
  --email ada@example.com \
  --data '{"order_id":"1042","total":49}'
```

MCP:

```json
{
  "name": "events_send",
  "arguments": {
    "name": "order.paid",
    "email": "ada@example.com",
    "data": {
      "order_id": "1042",
      "total": 49
    }
  }
}
```

events.send:

```json
{
  "object": "event_receipt",
  "id": "rcp_04",
  "name": "order.paid",
  "email": "ada@example.com",
  "contactId": null,
  "data": {
    "order_id": "1042",
    "total": 49
  },
  "started": 1,
  "resumed": 0,
  "exited": 0,
  "createdAt": "2026-11-27T09:30:00.000Z"
}
```

- Every active automation whose trigger names the event starts a run. The answer counts them as `started`.
- When a contact has the address, the run is for that contact: its names fill the email and its subscription is respected. Without one, the run is for the bare address.
- The data takes up to 10 KB. Its values fill `{{name}}` slots and can be tested by conditions.
- With `idempotencyKey`, or the `Idempotency-Key` header, a repeat of the request answers with the first result and starts nothing.

These events are sent by the workspace itself. A trigger or a wait can name them.

| Event | Sent when |
| --- | --- |
| `contact.created` | A person becomes a contact of the workspace. It is sent once for an address. |
| `contact.joined` | A contact is added to an audience, when they are new and when they were a contact already. Its data has `audience_id`. |
| `contact.left` | A contact is taken out of an audience. They stay a contact. |
| `contact.updated` | A name or a property of a contact changes. Its data has `changed`, the names that changed, so a condition can ask whether `changed` contains `plan`. |
| `contact.subscribed` | A contact who had unsubscribed is subscribed again. |
| `contact.unsubscribed` | A contact unsubscribes. |
| `email.delivered` | An email to the person is delivered. |
| `email.opened` | The person opens an email. |
| `email.clicked` | The person follows a link in an email. |
| `email.bounced` | An email to the person bounces. |
| `email.complained` | The person marks an email as spam. |
| `email.received` | The person writes to an address on one of your domains. |

A trigger can carry `conditions`, so only some events start a run, and `audienceId`, so only contacts of one audience enter. For `contact.joined` and `contact.left` that is the audience they joined or left. For every other event it is an audience the contact is in.

## Other ways to start

| Kind | Starts a run |
| --- | --- |
| `event` | When the event it names arrives, for the person the event is about. |
| `schedule` | At a time that repeats every day, week or month, for every subscribed contact of an audience. |
| `date` | On a date each contact carries in a property, or a number of days before or after it. With `yearly`, every year. |
| `manual` | Only when you enrol someone. |

Enrolling starts a run for one person in any active automation, whatever its trigger.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/automations/auto_welcome/enroll" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "email": "ada@example.com"
}'
```

TypeScript:

```ts
await opensend.automations.enroll('auto_welcome', {
  email: 'ada@example.com',
})
```

CLI:

```bash
opensend automations enroll auto_welcome --email ada@example.com
```

MCP:

```json
{
  "name": "automations_enroll",
  "arguments": {
    "id": "auto_welcome",
    "email": "ada@example.com"
  }
}
```

## Start a flow from a payment

Your payment provider tells your server what happened with a webhook. Send that on as an event, and an automation can act on it: notices while a payment is open, a reminder before a renewal, a note after a cancellation.

```ts
import { OpenSend } from '@open-send/opensend'

const opensend = new OpenSend()

export async function paymentFailed(noticeId: string, email: string, payUrl: string) {
  const receipt = await opensend.events.send(
    { name: 'payment.failed', email, data: { pay_url: payUrl } },
    { idempotencyKey: noticeId },
  )
  console.log(receipt.started)
}
```

- Check the signature of the request first, the way your provider describes, and call this from its handler.
- The id the provider gives its event is the idempotency key, so a delivery it repeats starts nothing twice.
- With Polar, `subscription.past_due` is the payment that failed and `order.paid` the one that went through.

The dashboard has a starter for each of these flows. A starter listens for the names below, which you can change to the ones your app sends.

| Starter | Starts on | Also listens for | What it does |
| --- | --- | --- | --- |
| Failed payment | `payment.failed` | `payment.succeeded` | Three notices that stop once the payment goes through. |
| Abandoned checkout | `checkout.started` | `order.paid` | A reminder when a checkout stays unpaid for an hour. |
| Renewal reminder | `invoice.upcoming` | Nothing else | A note before a subscription renews. |
| Cancellation | `subscription.canceled` | `subscription.started` | A goodbye, then one invitation to come back. |

## Steps

| Kind | What it does |
| --- | --- |
| `send_email` | Sends an email to the person, or to the address in `to`. Written in the step, or built from a published template. |
| `delay` | Waits for an amount of minutes, hours, days or weeks, up to 365 days. |
| `wait_until` | Waits until a time of day, on the days of the week you allow. With `date` it waits for a date in place of that: one you write, or one a slot holds, such as `{{starts_at}}`, with an `amount` of time `before` or after it. A date that has passed lets the run go on at once, and a run with no date there fails. |
| `wait_for_event` | Waits for an event about the person. `yes` when it arrives, `no` when the time is up. |
| `condition` | Tests the person, the data or what happened so far. `yes` when it holds, `no` otherwise. |
| `branch` | Sends the run down one of up to 8 named paths: the first one in `branches` whose conditions hold, or `no` when none does. |
| `split` | Sends a share of people down `yes` and the rest down `no`. A person always lands on the same side. |
| `update_contact` | Changes the names, the properties or the subscription of the contact. |
| `delete_contact` | Deletes the contact for good. The run ends there. |
| `send_event` | Sends an event of your own about the person, with `data` whose text can hold `{{name}}` slots. It is recorded like one you sent yourself, so it starts the automations that listen for it, lets their waits go on and applies their exit events. A chain of automations that start each other this way ends after three. |
| `add_to_audience` | Adds the person to an audience, and makes them a contact first when they are not one. It sends `contact.joined`, and `contact.created` for a new contact. |
| `remove_from_audience` | Takes the person out of an audience, which sends `contact.left`. They stay a contact. |
| `webhook` | Posts a signed `automation.step` event to one of your webhooks, with the data of the run. |
| `exit` | Ends the run. |

Paths can meet again: two steps may name the same `next`. A flow cannot loop back to a step it has passed.

## More than two ways

A `condition` step has two ways out. A `branch` step has as many as you name: each entry of `branches` has a `name`, its own `conditions` with `match`, and `to`, the step that follows. The paths are tested from the top and the first that holds is taken. `no` is where a run goes when none holds.

```ts
import { OpenSend } from '@open-send/opensend'

const opensend = new OpenSend()

export async function welcomeByPlan(from: string) {
  const flow = await opensend.automations.create({
    name: 'Welcome by plan',
    trigger: { kind: 'event', event: 'plan.chosen' },
    steps: [
      {
        id: 'plan',
        kind: 'branch',
        branches: [
          {
            name: 'Pro',
            conditions: [{ kind: 'data', field: 'plan', operator: 'equals', value: 'pro' }],
            to: 'pro',
          },
          {
            name: 'Team',
            conditions: [{ kind: 'data', field: 'plan', operator: 'equals', value: 'team' }],
            to: 'team',
          },
        ],
        no: 'free',
      },
      { id: 'pro', kind: 'send_email', from, subject: 'Welcome to Pro', text: 'Pro is on.', next: null },
      { id: 'team', kind: 'send_email', from, subject: 'Welcome to Team', text: 'Your team has its space.', next: null },
      { id: 'free', kind: 'send_email', from, subject: 'Welcome', text: 'You are on the free plan.', next: null },
    ],
  })
  await opensend.automations.publish(flow.id)
  console.log(flow.steps.length)
  return flow.id
}
```

- The run says which way it went: the step answers `yes` with the name of the path, or `no` when none matched.
- A path needs a name of its own and at least one condition. `to` and `no` end the run when they are `null` or left out.

## Conditions

A condition step, a trigger and a wait for an event take a list of `conditions`. `match` says whether `all` of them have to hold or `any` one is enough.

| Kind | Tests |
| --- | --- |
| `data` | A value in what started the run, such as `plan` or `order.total`. In a wait for an event it reads the event that arrives. |
| `waited` | A value in an event a wait step received earlier in the run. |
| `contact` | A field of the contact: `email`, `first_name`, `last_name`, `status` or one of its properties. |
| `email` | Whether an email a step of this run sent was delivered, opened, clicked, bounced or complained about. |
| `event` | Whether the person sent an event since the run started, or within a number of days. |
| `audience` | Whether the person is in an audience. |
| `subscribed` | Whether the contact is subscribed. |
| `group` | Several of the above as one, with a `match` of its own. |

- The operators are `equals`, `not_equals`, `greater_than`, `greater_or_equal`, `less_than`, `less_or_equal`, `contains`, `not_contains`, `starts_with`, `ends_with`, `exists`, `is_empty`.
- Text is compared without regard to capitals, and numbers and dates by their value.
- A value that is not there never matches, apart from `is_empty`.
- `not: true` turns a test around.
- A `value` can hold `{{name}}` slots. In a wait for an event that is how the event is tied to the run: `{ "kind": "data", "field": "order_id", "operator": "equals", "value": "{{order_id}}" }`.

## The emails of a run

A send step fills `{{name}}` slots in its subject, HTML and text. What goes into HTML is escaped.

| Slot | Filled with |
| --- | --- |
| `{{first_name}}`, `{{last_name}}` | The names of the contact, or the same names in the data. |
| `{{email}}` | The address of the person. |
| `{{unsubscribe_url}}` | The unsubscribe link of the contact. |
| A property of the contact | Its value, by its name. |
| A value in the data of the event | Its value, by its name. It wins over a property. |
| A name in `variables` of the step | What you wrote there, which can hold slots itself. |

- A slot with no value fails that one run with `missing_variable`, so nobody gets an email with a hole in it.
- With `template`, the step sends the published copy of a [template](https://opensend.sh/docs/templates). `variables` gives its values.
- An email that is due outside the sending hours waits until they open.
- Every email a run sends is in [the emails](https://opensend.sh/docs/api/emails), with its status and events.

> With `respectUnsubscribes` on, which is how an automation starts, an unsubscribed contact does not enter, a run ends when its contact unsubscribes, and emails to contacts carry the unsubscribe link and count as marketing email. Switch it off for mail that always has to arrive, such as a receipt.

## Rules

| Setting | What it decides |
| --- | --- |
| `timezone` | The time zone its times of day are read in. `UTC` to begin with. |
| `reentry` | `after_exit`: a person can enter again once their run has ended. `once`: never again. `always`: every time, even while a run is going. |
| `reentryAfterDays` | With `after_exit`, how many days have to pass after a run ends. |
| `window` | The sending hours: days of the week, and a time they open and close. |
| `exitOn` | Events that take a person out of the automation the moment they arrive. |
| `respectUnsubscribes` | Whether unsubscribed contacts are left alone. |

## Pause, resume and test

Pausing stops new people from entering. `runs` says what happens to the people already inside: `hold` keeps them where they are until you resume, `finish` lets them go on to the end, `cancel` ends their runs.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/automations/auto_welcome/pause" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.automations.pause('auto_welcome')
```

CLI:

```bash
opensend automations pause auto_welcome
```

MCP:

```json
{
  "name": "automations_pause",
  "arguments": {
    "id": "auto_welcome"
  }
}
```

An automation also pauses itself, and holds its runs, when something it depends on stops working: a sender whose domain is no longer verified, a template that is gone, an audience or a webhook that was deleted. It does the same on a plan that stops at its limits, once the emails of the month are used up. `pausedReason` and `pausedDetail` say which. Fix it and resume, and everybody goes on from where they stopped.

A test walks the copy you are editing, now, for one address. Its emails are really sent, with "[Test]" before the subject. Waits are skipped, a wait for an event takes its `no` path, and steps that change contacts or call webhooks are skipped. Nothing is counted.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/automations/auto_teams/test" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "email": "ada@example.com"
}'
```

TypeScript:

```ts
await opensend.automations.test('auto_teams', {
  email: 'ada@example.com',
})
```

CLI:

```bash
opensend automations test auto_teams --email ada@example.com
```

MCP:

```json
{
  "name": "automations_test",
  "arguments": {
    "id": "auto_teams",
    "email": "ada@example.com"
  }
}
```

## Runs

cURL:

```bash
curl "$OPENSEND_API_URL/v1/automations/auto_welcome/runs?status=waiting" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.runs.list({
  automationId: 'auto_welcome',
  status: 'waiting',
})
```

CLI:

```bash
opensend runs list --automation-id auto_welcome --status waiting
```

MCP:

```json
{
  "name": "runs_list",
  "arguments": {
    "automationId": "auto_welcome",
    "status": "waiting"
  }
}
```

| Status | Meaning |
| --- | --- |
| `running` | It is due to go on. |
| `waiting` | It is at a wait. `wakeAt` says until when. |
| `completed` | It reached the end of its path. |
| `exited` | It left early. `reason` says why: `exit_step`, `goal`, `unsubscribed`, `contact_deleted`, `automation_deleted`. |
| `failed` | A step failed: `missing_variable`, `send_refused`, `step_failed`. `detail` has the sentence. |
| `canceled` | You cancelled it, or stopped the automation. |

One run has the data that started it and what each step did.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/automations/auto_welcome/runs/run_01" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.runs.get('run_01', {
  automationId: 'auto_welcome',
})
```

CLI:

```bash
opensend runs get run_01 --automation-id auto_welcome
```

MCP:

```json
{
  "name": "runs_get",
  "arguments": {
    "automationId": "auto_welcome",
    "id": "run_01"
  }
}
```

Cancelling takes one person out. Emails already sent are not recalled.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/automations/auto_welcome/runs/run_02/cancel" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.runs.cancel('run_02', {
  automationId: 'auto_welcome',
})
```

CLI:

```bash
opensend runs cancel run_02 --automation-id auto_welcome
```

MCP:

```json
{
  "name": "runs_cancel",
  "arguments": {
    "automationId": "auto_welcome",
    "id": "run_02"
  }
}
```

A run that failed can be tried again once what broke it is fixed: a value that was missing is on the contact now, or the provider answers again. It goes on from the step that failed, on the version it entered with, and an email it failed to send is really sent this time. A run that did not fail is refused with `run_not_failed`.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/automations/auto_receipt/runs/run_03/retry" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.runs.retry('run_03', {
  automationId: 'auto_receipt',
})
```

CLI:

```bash
opensend runs retry run_03 --automation-id auto_receipt
```

MCP:

```json
{
  "name": "runs_retry",
  "arguments": {
    "automationId": "auto_receipt",
    "id": "run_03"
  }
}
```

[An automation](https://opensend.sh/docs/api/automations) answers with the same numbers added up: how many entered, how many are inside now, and for each step how many it reached, sent, and saw opened and clicked.

## Define an event

An event needs no definition: a new name defines itself the first time it is sent. Defining it lets you fix the type of each value in its data.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/events" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "trial.started",
  "schema": {
    "plan": "string",
    "ends_at": "date"
  }
}'
```

TypeScript:

```ts
await opensend.events.create({
  name: 'trial.started',
  schema: {
    plan: 'string',
    ends_at: 'date',
  },
})
```

CLI:

```bash
opensend events create \
  --name trial.started \
  --schema '{"plan":"string","ends_at":"date"}'
```

MCP:

```json
{
  "name": "events_create",
  "arguments": {
    "name": "trial.started",
    "schema": {
      "plan": "string",
      "ends_at": "date"
    }
  }
}
```

- The types are `string`, `number`, `boolean` and `date`.
- A value that can be read as its type is converted: `"49"` becomes `49`, and a date becomes an ISO 8601 time.
- `event_data`: a value does not fit. The message names it.

An event answers with the latest times it was sent and what each one set off.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/events/order.paid" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.events.get('order.paid')
```

CLI:

```bash
opensend events get order.paid
```

MCP:

```json
{
  "name": "events_get",
  "arguments": {
    "id": "order.paid"
  }
}
```

## Hear about runs

A [webhook](https://opensend.sh/docs/webhooks) can ask for `automation.entered` and `automation.finished`, sent when a person enters and when their run ends, and for `automation.step`, sent by a webhook step.

## Limits

- An automation takes up to 200 steps, and 1 MB in all. Keep long emails in templates.
- A wait is at most 365 days.
- The first step of a run is done while the event is answered. After that a schedule moves runs along once a minute, 50 at a time, so a step can start up to a minute after it is due.
- Automations can start each other through the mail they send. Such a chain stops after 3 links.
