# 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.sendping.co/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 { SendPing } from 'sendping';

const mb = new SendPing('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 "sendping"

SendPing.api_key = "mb_xxxxxxxxx"

SendPing::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
<?php
require 'vendor/autoload.php';

use SendPing\SendPing;

$sendping = SendPing::client('mb_xxxxxxxxx');

$sendping->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 sendping

sendping.api_key = "mb_xxxxxxxxx"

sendping.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
import "github.com/shekhu10/sendping-sdks/sendping-go"

client := sendping.NewClient("mb_xxxxxxxxx")

automation, err := client.Automations.Create(&sendping.CreateAutomationRequest{
    Name:   "Plan-based welcome",
    Domain: "yourdomain.com",
    Steps: []sendping.AutomationStepInput{
        {
            Key:    "start",
            Type:   "trigger",
            Config: map[string]any{"event_name": "user.created"},
        },
        {
            Key:  "check_plan",
            Type: "condition",
            Config: map[string]any{
                "type":     "rule",
                "field":    "event.plan",
                "operator": "eq",
                "value":    "pro",
            },
        },
        {
            Key:  "send_pro_email",
            Type: "send_email",
            Config: map[string]any{
                "template": map[string]any{"id": "pro-welcome-template-id"},
            },
        },
        {
            Key:  "send_free_email",
            Type: "send_email",
            Config: map[string]any{
                "template": map[string]any{"id": "free-welcome-template-id"},
            },
        },
    },
    Connections: []sendping.AutomationConnectionInput{
        {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",
        },
    },
})
```

**Rust**

```rust
use sendping::{AutomationStepInput, ConnectionInput, CreateAutomationOptions, SendPing};

let mb = SendPing::new("mb_xxxxxxxxx");

let params = CreateAutomationOptions::new("Plan-based welcome", "yourdomain.com")
    .with_step(AutomationStepInput::new("trigger")
        .with_key("start")
        .with_config(serde_json::json!({"event_name":"user.created"})))
    .with_step(AutomationStepInput::new("condition")
        .with_key("check_plan")
        .with_config(serde_json::json!({
            "type": "rule",
            "field": "event.plan",
            "operator": "eq",
            "value": "pro"
        })))
    .with_step(AutomationStepInput::new("send_email")
        .with_key("send_pro_email")
        .with_config(serde_json::json!({"template":{"id":"pro-welcome-template-id"}})))
    .with_step(AutomationStepInput::new("send_email")
        .with_key("send_free_email")
        .with_config(serde_json::json!({"template":{"id":"free-welcome-template-id"}})))
    .with_connection(ConnectionInput::new("start", "check_plan").with_type("default"))
    .with_connection(ConnectionInput::new("check_plan", "send_pro_email")
        .with_type("condition_met"))
    .with_connection(ConnectionInput::new("check_plan", "send_free_email")
        .with_type("condition_not_met"));
let _automation = mb.automations.create(params).await?;
```

**Java**

```java
import co.sendping.SendPing;
import co.sendping.SendPingResponse;
import co.sendping.requests.AutomationConnection;
import co.sendping.requests.AutomationStep;
import co.sendping.requests.CreateAutomationRequest;
import java.util.Map;

SendPing sendping = new SendPing("mb_xxxxxxxxx");

CreateAutomationRequest request = CreateAutomationRequest.builder()
        .name("Plan-based welcome")
        .domain("yourdomain.com")
        .step(AutomationStep.builder()
                .key("start")
                .type("trigger")
                .config("event_name", "user.created")
                .build())
        .step(AutomationStep.builder()
                .key("check_plan")
                .type("condition")
                .config("type", "rule")
                .config("field", "event.plan")
                .config("operator", "eq")
                .config("value", "pro")
                .build())
        .step(AutomationStep.builder()
                .key("send_pro_email")
                .type("send_email")
                .config("template", Map.of("id", "pro-welcome-template-id"))
                .build())
        .step(AutomationStep.builder()
                .key("send_free_email")
                .type("send_email")
                .config("template", Map.of("id", "free-welcome-template-id"))
                .build())
        .connection(AutomationConnection.of("start", "check_plan", "default"))
        .connection(AutomationConnection.of("check_plan", "send_pro_email", "condition_met"))
        .connection(AutomationConnection.of("check_plan", "send_free_email", "condition_not_met"))
        .build();

SendPingResponse response = sendping.automations().create(request);
```

**.NET**

```csharp
using SendPing;

ISendPing sendping = SendPingClient.Create("mb_xxxxxxxxx");

var resp = await sendping.AutomationCreateAsync(new AutomationCreateOptions
{
    Name = "Plan-based welcome",
    Domain = "yourdomain.com",
    Steps = new List<AutomationStepInput>
    {
        new()
        {
            Key = "start",
            Type = "trigger",
            Config = new Dictionary<string, object?>
            {
                ["event_name"] = "user.created",
            },
        },
        new()
        {
            Key = "check_plan",
            Type = "condition",
            Config = new Dictionary<string, object?>
            {
                ["type"] = "rule",
                ["field"] = "event.plan",
                ["operator"] = "eq",
                ["value"] = "pro",
            },
        },
        new()
        {
            Key = "send_pro_email",
            Type = "send_email",
            Config = new Dictionary<string, object?>
            {
                ["template"] = new Dictionary<string, object?>
                {
                    ["id"] = "pro-welcome-template-id",
                },
            },
        },
        new()
        {
            Key = "send_free_email",
            Type = "send_email",
            Config = new Dictionary<string, object?>
            {
                ["template"] = new Dictionary<string, object?>
                {
                    ["id"] = "free-welcome-template-id",
                },
            },
        },
    },
    Connections = new List<AutomationConnection>
    {
        new()
        {
            From = "start",
            To = "check_plan",
            Type = "default",
        },
        new()
        {
            From = "check_plan",
            To = "send_pro_email",
            Type = "condition_met",
        },
        new()
        {
            From = "check_plan",
            To = "send_free_email",
            Type = "condition_not_met",
        },
    },
});
```

**cURL**

```bash
curl -X POST 'https://www.sendping.co/api/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 a single `{ field, operator, value }` rule — groups do not nest; chain condition steps to build one.

```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 — a flat array of `{ field, operator, value }` rules (not nested groups). Must contain at least one item. |
