# Add to Segment

> Add the current contact to a segment as a step in your automation.

The **add to segment** step adds the current contact to a specified [segment](https://www.sendping.co/docs/segments/overview) when the automation reaches it. See [Using automations](https://www.sendping.co/docs/automations/overview) for the surrounding workflow.

Common use cases:

- **Cohorts** — add contacts to a cohort segment after a specific event.
- **Lifecycle** — segment contacts as they progress through onboarding.
- **Engagement** — group contacts who completed a specific action.

## How it works

In the dashboard, add an **Add to segment** step and select the segment from the dropdown. Over the API, the `add_to_segment` step accepts a single `segment_id` field.

**Node.js**

```js
import { SendPing } from 'sendping';

const mb = new SendPing('mb_xxxxxxxxx');

const { data, error } = await mb.automations.create({
  "name": "Tag VIP users",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": { "event_name": "purchase.completed" }
    },
    {
      "key": "vip_users",
      "type": "add_to_segment",
      "config": { "segment_id": "83a1e324-26dc-47eb-9b28-ba8b6d1fe808" }
    }
  ],
  "connections": [
    { "from": "start", "to": "vip_users", "type": "default" }
  ]
});
console.log({ data, error });
```

**Ruby**

```ruby
require "sendping"

SendPing.api_key = "mb_xxxxxxxxx"

SendPing::Automations.create({
  "name": "Tag VIP users",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": {
        "event_name": "purchase.completed"
      }
    },
    {
      "key": "vip_users",
      "type": "add_to_segment",
      "config": {
        "segment_id": "83a1e324-26dc-47eb-9b28-ba8b6d1fe808"
      }
    }
  ],
  "connections": [
    {
      "from": "start",
      "to": "vip_users",
      "type": "default"
    }
  ]
})
```

**PHP**

```php
<?php
require 'vendor/autoload.php';

use SendPing\SendPing;

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

$sendping->automations->create([
  'name' => "Tag VIP users",
  'domain' => "yourdomain.com",
  'steps' => [
    [
      'key' => "start",
      'type' => "trigger",
      'config' => [
        'event_name' => "purchase.completed"
      ]
    ],
    [
      'key' => "vip_users",
      'type' => "add_to_segment",
      'config' => [
        'segment_id' => "83a1e324-26dc-47eb-9b28-ba8b6d1fe808"
      ]
    ]
  ],
  'connections' => [
    [
      'from' => "start",
      'to' => "vip_users",
      'type' => "default"
    ]
  ]
]);
```

**Python**

```python
import sendping

sendping.api_key = "mb_xxxxxxxxx"

sendping.Automations.create({
  "name": "Tag VIP users",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": {
        "event_name": "purchase.completed"
      }
    },
    {
      "key": "vip_users",
      "type": "add_to_segment",
      "config": {
        "segment_id": "83a1e324-26dc-47eb-9b28-ba8b6d1fe808"
      }
    }
  ],
  "connections": [
    {
      "from": "start",
      "to": "vip_users",
      "type": "default"
    }
  ]
})
```

**Go**

```go
import "github.com/shekhu10/sendping-sdks/sendping-go"

client := sendping.NewClient("mb_xxxxxxxxx")

automation, err := client.Automations.Create(&sendping.CreateAutomationRequest{
    Name:   "Tag VIP users",
    Domain: "yourdomain.com",
    Steps: []sendping.AutomationStepInput{
        {
            Key:    "start",
            Type:   "trigger",
            Config: map[string]any{"event_name": "purchase.completed"},
        },
        {
            Key:  "vip_users",
            Type: "add_to_segment",
            Config: map[string]any{
                "segment_id": "83a1e324-26dc-47eb-9b28-ba8b6d1fe808",
            },
        },
    },
    Connections: []sendping.AutomationConnectionInput{
        {From: "start", To: "vip_users", Type: "default"},
    },
})
```

**Rust**

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

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

let params = CreateAutomationOptions::new("Tag VIP users", "yourdomain.com")
    .with_step(AutomationStepInput::new("trigger")
        .with_key("start")
        .with_config(serde_json::json!({"event_name":"purchase.completed"})))
    .with_step(AutomationStepInput::new("add_to_segment")
        .with_key("vip_users")
        .with_config(serde_json::json!({"segment_id":"83a1e324-26dc-47eb-9b28-ba8b6d1fe808"})))
    .with_connection(ConnectionInput::new("start", "vip_users").with_type("default"));
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;

SendPing sendping = new SendPing("mb_xxxxxxxxx");

CreateAutomationRequest request = CreateAutomationRequest.builder()
        .name("Tag VIP users")
        .domain("yourdomain.com")
        .step(AutomationStep.builder()
                .key("start")
                .type("trigger")
                .config("event_name", "purchase.completed")
                .build())
        .step(AutomationStep.builder()
                .key("vip_users")
                .type("add_to_segment")
                .config("segment_id", "83a1e324-26dc-47eb-9b28-ba8b6d1fe808")
                .build())
        .connection(AutomationConnection.of("start", "vip_users", "default"))
        .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 = "Tag VIP users",
    Domain = "yourdomain.com",
    Steps = new List<AutomationStepInput>
    {
        new()
        {
            Key = "start",
            Type = "trigger",
            Config = new Dictionary<string, object?>
            {
                ["event_name"] = "purchase.completed",
            },
        },
        new()
        {
            Key = "vip_users",
            Type = "add_to_segment",
            Config = new Dictionary<string, object?>
            {
                ["segment_id"] = "83a1e324-26dc-47eb-9b28-ba8b6d1fe808",
            },
        },
    },
    Connections = new List<AutomationConnection>
    {
        new()
        {
            From = "start",
            To = "vip_users",
            Type = "default",
        },
    },
});
```

**cURL**

```bash
curl -X POST 'https://www.sendping.co/api/automations' \
  -H 'Authorization: Bearer mb_xxxxxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "Tag VIP users",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": { "event_name": "purchase.completed" }
    },
    {
      "key": "vip_users",
      "type": "add_to_segment",
      "config": { "segment_id": "83a1e324-26dc-47eb-9b28-ba8b6d1fe808" }
    }
  ],
  "connections": [
    { "from": "start", "to": "vip_users", "type": "default" }
  ]
}'
```

## Configuration

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `config.segment_id` | string | Yes | The ID of the segment to add the contact to. |

```json
{
  "key": "add_to_vip",
  "type": "add_to_segment",
  "config": {
    "segment_id": "83a1e324-26dc-47eb-9b28-ba8b6d1fe808"
  }
}
```
