# Condition

> Branch a contact down one of two paths based on event or contact data.

A **condition** step evaluates a rule against event or contact data and routes the automation down one of two paths: **condition met** or **condition not met**. See [Using automations](https://www.mailblastr.com/docs/automations/overview) for the surrounding workflow.

Common use cases:

- **Plan-based emails** — send different content to free vs. paid users.
- **Engagement splits** — take different actions based on user activity.
- **Personalization** — tailor follow-ups based on event payload values.

## How it works

The condition step accepts a `type` and the corresponding rule fields, plus two outgoing connections — one for each outcome.

**Node.js**

```js
import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr('mb_xxxxxxxxx');

const { data, error } = await mb.automations.create({
  "name": "Plan-based welcome",
  "domain": "yourdomain.com",
  "steps": [
    { "key": "start", "type": "trigger", "config": { "event_name": "user.created" } },
    {
      "key": "check_plan",
      "type": "condition",
      "config": { "type": "rule", "field": "event.plan", "operator": "eq", "value": "pro" }
    },
    { "key": "send_pro_email", "type": "send_email", "config": { "template": { "id": "pro-welcome-template-id" } } },
    { "key": "send_free_email", "type": "send_email", "config": { "template": { "id": "free-welcome-template-id" } } }
  ],
  "connections": [
    { "from": "start", "to": "check_plan", "type": "default" },
    { "from": "check_plan", "to": "send_pro_email", "type": "condition_met" },
    { "from": "check_plan", "to": "send_free_email", "type": "condition_not_met" }
  ]
});
console.log({ data, error });
```

**Ruby**

```ruby
require "mailblastr"

Mailblastr.api_key = "mb_xxxxxxxxx"

Mailblastr::Automations.create({
  "name": "Plan-based welcome",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": {
        "event_name": "user.created"
      }
    },
    {
      "key": "check_plan",
      "type": "condition",
      "config": {
        "type": "rule",
        "field": "event.plan",
        "operator": "eq",
        "value": "pro"
      }
    },
    {
      "key": "send_pro_email",
      "type": "send_email",
      "config": {
        "template": {
          "id": "pro-welcome-template-id"
        }
      }
    },
    {
      "key": "send_free_email",
      "type": "send_email",
      "config": {
        "template": {
          "id": "free-welcome-template-id"
        }
      }
    }
  ],
  "connections": [
    {
      "from": "start",
      "to": "check_plan",
      "type": "default"
    },
    {
      "from": "check_plan",
      "to": "send_pro_email",
      "type": "condition_met"
    },
    {
      "from": "check_plan",
      "to": "send_free_email",
      "type": "condition_not_met"
    }
  ]
})
```

**PHP**

```php
$mailblastr = Mailblastr::client('mb_xxxxxxxxx');

$mailblastr->automations->create([
  'name' => "Plan-based welcome",
  'domain' => "yourdomain.com",
  'steps' => [
    [
      'key' => "start",
      'type' => "trigger",
      'config' => [
        'event_name' => "user.created"
      ]
    ],
    [
      'key' => "check_plan",
      'type' => "condition",
      'config' => [
        'type' => "rule",
        'field' => "event.plan",
        'operator' => "eq",
        'value' => "pro"
      ]
    ],
    [
      'key' => "send_pro_email",
      'type' => "send_email",
      'config' => [
        'template' => [
          'id' => "pro-welcome-template-id"
        ]
      ]
    ],
    [
      'key' => "send_free_email",
      'type' => "send_email",
      'config' => [
        'template' => [
          'id' => "free-welcome-template-id"
        ]
      ]
    ]
  ],
  'connections' => [
    [
      'from' => "start",
      'to' => "check_plan",
      'type' => "default"
    ],
    [
      'from' => "check_plan",
      'to' => "send_pro_email",
      'type' => "condition_met"
    ],
    [
      'from' => "check_plan",
      'to' => "send_free_email",
      'type' => "condition_not_met"
    ]
  ]
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Automations.create({
  "name": "Plan-based welcome",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": {
        "event_name": "user.created"
      }
    },
    {
      "key": "check_plan",
      "type": "condition",
      "config": {
        "type": "rule",
        "field": "event.plan",
        "operator": "eq",
        "value": "pro"
      }
    },
    {
      "key": "send_pro_email",
      "type": "send_email",
      "config": {
        "template": {
          "id": "pro-welcome-template-id"
        }
      }
    },
    {
      "key": "send_free_email",
      "type": "send_email",
      "config": {
        "template": {
          "id": "free-welcome-template-id"
        }
      }
    }
  ],
  "connections": [
    {
      "from": "start",
      "to": "check_plan",
      "type": "default"
    },
    {
      "from": "check_plan",
      "to": "send_pro_email",
      "type": "condition_met"
    },
    {
      "from": "check_plan",
      "to": "send_free_email",
      "type": "condition_not_met"
    }
  ]
})
```

**Go**

```go
package main

import (
    "fmt"
    "io"
    "net/http"
    "strings"
)

func main() {
    req, _ := http.NewRequest("POST", "https://api.mailblastr.com/automations", strings.NewReader(`{
  "name": "Plan-based welcome",
  "domain": "yourdomain.com",
  "steps": [
    { "key": "start", "type": "trigger", "config": { "event_name": "user.created" } },
    {
      "key": "check_plan",
      "type": "condition",
      "config": { "type": "rule", "field": "event.plan", "operator": "eq", "value": "pro" }
    },
    { "key": "send_pro_email", "type": "send_email", "config": { "template": { "id": "pro-welcome-template-id" } } },
    { "key": "send_free_email", "type": "send_email", "config": { "template": { "id": "free-welcome-template-id" } } }
  ],
  "connections": [
    { "from": "start", "to": "check_plan", "type": "default" },
    { "from": "check_plan", "to": "send_pro_email", "type": "condition_met" },
    { "from": "check_plan", "to": "send_free_email", "type": "condition_not_met" }
  ]
}`))
    req.Header.Set("Authorization", "Bearer mb_xxxxxxxxx")
    req.Header.Set("Content-Type", "application/json")
    res, _ := http.DefaultClient.Do(req)
    defer res.Body.Close()
    out, _ := io.ReadAll(res.Body)
    fmt.Println(string(out))
}
```

**Rust**

```rust
use reqwest::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let res = Client::new()
        .post("https://api.mailblastr.com/automations")
        .header("Authorization", "Bearer mb_xxxxxxxxx")
        .header("Content-Type", "application/json")
        .body(r#"{
  "name": "Plan-based welcome",
  "domain": "yourdomain.com",
  "steps": [
    { "key": "start", "type": "trigger", "config": { "event_name": "user.created" } },
    {
      "key": "check_plan",
      "type": "condition",
      "config": { "type": "rule", "field": "event.plan", "operator": "eq", "value": "pro" }
    },
    { "key": "send_pro_email", "type": "send_email", "config": { "template": { "id": "pro-welcome-template-id" } } },
    { "key": "send_free_email", "type": "send_email", "config": { "template": { "id": "free-welcome-template-id" } } }
  ],
  "connections": [
    { "from": "start", "to": "check_plan", "type": "default" },
    { "from": "check_plan", "to": "send_pro_email", "type": "condition_met" },
    { "from": "check_plan", "to": "send_free_email", "type": "condition_not_met" }
  ]
}"#)
        .send()
        .await?;
    println!("{}", res.text().await?);
    Ok(())
}
```

**Java**

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.mailblastr.com/automations"))
    .header("Authorization", "Bearer mb_xxxxxxxxx")
    .header("Content-Type", "application/json")
    .method("POST", HttpRequest.BodyPublishers.ofString("""
{
  "name": "Plan-based welcome",
  "domain": "yourdomain.com",
  "steps": [
    { "key": "start", "type": "trigger", "config": { "event_name": "user.created" } },
    {
      "key": "check_plan",
      "type": "condition",
      "config": { "type": "rule", "field": "event.plan", "operator": "eq", "value": "pro" }
    },
    { "key": "send_pro_email", "type": "send_email", "config": { "template": { "id": "pro-welcome-template-id" } } },
    { "key": "send_free_email", "type": "send_email", "config": { "template": { "id": "free-welcome-template-id" } } }
  ],
  "connections": [
    { "from": "start", "to": "check_plan", "type": "default" },
    { "from": "check_plan", "to": "send_pro_email", "type": "condition_met" },
    { "from": "check_plan", "to": "send_free_email", "type": "condition_not_met" }
  ]
}
"""))
    .build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
```

**.NET**

```csharp
using System.Net.Http;
using System.Text;

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.mailblastr.com/automations");
request.Headers.Add("Authorization", "Bearer mb_xxxxxxxxx");
request.Content = new StringContent(@"{
  ""name"": ""Plan-based welcome"",
  ""domain"": ""yourdomain.com"",
  ""steps"": [
    { ""key"": ""start"", ""type"": ""trigger"", ""config"": { ""event_name"": ""user.created"" } },
    {
      ""key"": ""check_plan"",
      ""type"": ""condition"",
      ""config"": { ""type"": ""rule"", ""field"": ""event.plan"", ""operator"": ""eq"", ""value"": ""pro"" }
    },
    { ""key"": ""send_pro_email"", ""type"": ""send_email"", ""config"": { ""template"": { ""id"": ""pro-welcome-template-id"" } } },
    { ""key"": ""send_free_email"", ""type"": ""send_email"", ""config"": { ""template"": { ""id"": ""free-welcome-template-id"" } } }
  ],
  ""connections"": [
    { ""from"": ""start"", ""to"": ""check_plan"", ""type"": ""default"" },
    { ""from"": ""check_plan"", ""to"": ""send_pro_email"", ""type"": ""condition_met"" },
    { ""from"": ""check_plan"", ""to"": ""send_free_email"", ""type"": ""condition_not_met"" }
  ]
}", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
Console.WriteLine(await response.Content.ReadAsStringAsync());
```

**cURL**

```bash
curl -X POST 'https://api.mailblastr.com/automations' \
  -H 'Authorization: Bearer mb_xxxxxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "Plan-based welcome",
  "domain": "yourdomain.com",
  "steps": [
    { "key": "start", "type": "trigger", "config": { "event_name": "user.created" } },
    {
      "key": "check_plan",
      "type": "condition",
      "config": { "type": "rule", "field": "event.plan", "operator": "eq", "value": "pro" }
    },
    { "key": "send_pro_email", "type": "send_email", "config": { "template": { "id": "pro-welcome-template-id" } } },
    { "key": "send_free_email", "type": "send_email", "config": { "template": { "id": "free-welcome-template-id" } } }
  ],
  "connections": [
    { "from": "start", "to": "check_plan", "type": "default" },
    { "from": "check_plan", "to": "send_pro_email", "type": "condition_met" },
    { "from": "check_plan", "to": "send_free_email", "type": "condition_not_met" }
  ]
}'
```

## Connection types

A condition step always produces two outgoing connections:

| Connection type | Description |
| --- | --- |
| `condition_met` | Taken when the condition evaluates to true. |
| `condition_not_met` | Taken when the condition evaluates to false. |

## Operators

A `rule` condition compares a `field` against a `value` using one of the following operators:

| Operator | Meaning |
| --- | --- |
| `eq` | Equals. |
| `neq` | Not equals. |
| `gt` | Greater than. |
| `gte` | Greater than or equal to. |
| `lt` | Less than. |
| `lte` | Less than or equal to. |
| `contains` | Contains a given value. |
| `starts_with` | Starts with a given value. |
| `ends_with` | Ends with a given value. |
| `exists` | Field exists (no `value` required). |
| `is_empty` | Field is empty (no `value` required). |

## Combining rules

Use `and` or `or` to combine multiple rules into a single branch. Each entry in `rules` is itself a condition config — including nested `and`/`or` groups.

```json
{
  "key": "check_plan_and_amount",
  "type": "condition",
  "config": {
    "type": "and",
    "rules": [
      { "type": "rule", "field": "event.plan", "operator": "eq", "value": "pro" },
      { "type": "rule", "field": "event.amount", "operator": "gte", "value": 100 }
    ]
  }
}
```

## Configuration

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `config.type` | string | Yes | The type of condition node: `rule`, `and`, or `or`. |
| `config.field` | string | Yes | For `rule` type — the field to evaluate. Must use the `event.` or `contact.` namespace prefix (e.g. `event.amount`, `contact.email`). |
| `config.operator` | string | Yes | For `rule` type — the comparison operator. See the operators table above. |
| `config.value` | string | number | boolean | null | No | For `rule` type — the value to compare against. Not required for `exists` and `is_empty`. |
| `config.rules` | object[] | No | For `and` / `or` types — an array of nested condition config objects. Must contain at least one item. |
