---
title: "client.Segments"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://openemail.uk/docs/go/reference/segments"
area: "Go"
category: "Reference"
---

# client.Segments

Every method in this namespace: its signature, its parameters, what it returns and an example.

## Methods

Saved conditions on contacts, such as opened in the last 90 days or a custom field, that narrow a broadcast to the people who match: save, change and delete them, and count who they match.

### `Segments.List`

List the saved segments

```go
List(ctx context.Context, opts ...nextmail.RequestOption) ([]nextmail.Object, error)
```

Returns every saved segment of the workspace as a plain slice, newest first, each with its name, its description and its conditions. A workspace keeps at most 100, so the list is never paged.

A segment holds no contacts of its own. Who matches is worked out each time it is read, so use `Preview` to count the matches and pass a segment id as `segmentId` to `Broadcasts.Send` to narrow a broadcast with it.

Scopes: `audiences:read`.

**Parameters**

- `nextmail.WithAPIKey` (`string`): Overrides the client's API key for this call only.

**Returns**

A `[]nextmail.Object`, each with `id`, `name`, `description`, `definition`, `createdAt` and `updatedAt`.

**Example**

```go
segments, err := client.Segments.List(ctx)
if err != nil {
	return err
}

for _, segment := range segments {
	fmt.Println(segment.String("id"), segment.String("name"))
}
```

**Notes**

- The SDK retries it after a network failure or a retryable status, since it changes nothing.
- Segments belong to the workspace, not to the key or the person who made them.

Also available in: API [`GET /segments`](https://openemail.uk/docs/api/reference/audiences#get-segments); TypeScript [`nextmail.segments.list()`](https://openemail.uk/docs/sdk/reference/segments#list); Python [`segments.list()`](https://openemail.uk/docs/python/reference/segments#list); Ruby [`segments.list`](https://openemail.uk/docs/ruby/reference/segments#list); PHP [`segments->list`](https://openemail.uk/docs/php/reference/segments#list); Java [`segments().list`](https://openemail.uk/docs/java/reference/segments#list); C# [`Segments.ListAsync`](https://openemail.uk/docs/csharp/reference/segments#list); CLI [`openemail segments list`](https://openemail.uk/docs/cli/reference/segments#segments-list).

### `Segments.Get`

Get one segment

```go
Get(ctx context.Context, id string, opts ...nextmail.RequestOption) (nextmail.Object, error)
```

Returns one saved segment by its id, with `match` and every condition in `definition`. The conditions come back as they were saved, so the object can be edited and passed to `Update` or `Preview` as it is.

Scopes: `audiences:read`.

**Parameters**

- `id` (`string`, required): Segment id such as `seg_5d2a9c7e1b4f08a36c1e9b42`.
- `nextmail.WithAPIKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An `nextmail.Object` for the segment.

**Example**

```go
segment, err := client.Segments.Get(ctx, "seg_5d2a9c7e1b4f08a36c1e9b42")
if err != nil {
	return err
}

fmt.Println(segment.String("name"))
```

**Notes**

- A segment from another workspace, or one that was deleted, is a 404 `segment_not_found`.

Also available in: API [`GET /segments/{id}`](https://openemail.uk/docs/api/reference/audiences#get-segments-id); TypeScript [`nextmail.segments.get()`](https://openemail.uk/docs/sdk/reference/segments#get); Python [`segments.get()`](https://openemail.uk/docs/python/reference/segments#get); Ruby [`segments.get`](https://openemail.uk/docs/ruby/reference/segments#get); PHP [`segments->get`](https://openemail.uk/docs/php/reference/segments#get); Java [`segments().get`](https://openemail.uk/docs/java/reference/segments#get); C# [`Segments.GetAsync`](https://openemail.uk/docs/csharp/reference/segments#get); CLI [`openemail segments get`](https://openemail.uk/docs/cli/reference/segments#segments-get).

### `Segments.Create`

Save a segment

```go
Create(ctx context.Context, body nextmail.Body, opts ...nextmail.RequestOption) (nextmail.Object, error)
```

Saves a set of conditions under a name. `definition.match` is `all` (the default) when a contact has to meet every condition, or `any` when one is enough, and `definition.conditions` holds 1 to 20 of them.

Each condition names a `field` and an `operator`. `email` takes `is`, `is_not`, `contains`, `not_contains` and `ends_with`, and `name` takes those but `ends_with`, plus `is_set` and `is_not_set`. `source` takes `is` and `is_not` with how the contact was added. `created`, `opened`, `clicked` and `engaged` take `within_days` and `not_within_days` with `days`: when the contact was added, the last time they opened or clicked a tracked email from the workspace, and the later of those two. `audience` takes `in` and `not_in` with `audienceId`, and counts only subscribed members. `event` takes `within_days` and `not_within_days` with `event`, a name sent with `Events.Send`, and `days`. `attribute` takes `key`, the name of a custom field on the contact, with the operators `name` takes. Text is compared without regard to case.

Nothing is sent and no contact changes. A segment is used by passing its id as `segmentId` to `Broadcasts.Send`.

Scopes: `audiences:write`.

**Parameters**

- `name` (`string`, required): What the segment is called, 1 to 120 characters.
- `description` (`string`): A longer note, up to 500 characters.
- `definition` (`nextmail.Body`, required): `match` (`all` or `any`) and `conditions`, 1 to 20 of them.
- `nextmail.WithAPIKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An `nextmail.Object` for the segment, with its new `seg_` id.

**Example**

```go
segment, err := client.Segments.Create(ctx, nextmail.Body{
	"name": "Opened in the last 90 days",
	"definition": nextmail.Body{
		"match":      "all",
		"conditions": []nextmail.Body{{"field": "opened", "operator": "within_days", "days": 90}},
	},
})
if err != nil {
	return err
}

_, err = client.Broadcasts.Send(ctx, nextmail.Body{
	"audienceIds": []string{"aud_4c1b8e2a7d9f05c36b4e8a71"},
	"segmentId":   segment.String("id"),
	"from":        "Acme <news@acme.com>",
	"subject":     "Thanks for reading",
	"text":        "Hi {{firstName|there}}, here is something for our regular readers.",
})

return err
```

**Notes**

- A workspace keeps at most 100 segments. The 101st is refused with 409 `segment_limit_reached`.
- An audience condition that names an audience the workspace does not have is a 404 `audience_not_found`, and an operator its field does not take is a 422 `invalid_parameter`.
- The SDK does not retry a create after a network failure, so a retry after a lost response can save two. List them and delete the spare.

Also available in: API [`POST /segments`](https://openemail.uk/docs/api/reference/audiences#post-segments); TypeScript [`nextmail.segments.create()`](https://openemail.uk/docs/sdk/reference/segments#create); Python [`segments.create()`](https://openemail.uk/docs/python/reference/segments#create); Ruby [`segments.create`](https://openemail.uk/docs/ruby/reference/segments#create); PHP [`segments->create`](https://openemail.uk/docs/php/reference/segments#create); Java [`segments().create`](https://openemail.uk/docs/java/reference/segments#create); C# [`Segments.CreateAsync`](https://openemail.uk/docs/csharp/reference/segments#create); CLI [`openemail segments create`](https://openemail.uk/docs/cli/reference/segments#segments-create).

### `Segments.Update`

Change a segment

```go
Update(ctx context.Context, id string, patch nextmail.Body, opts ...nextmail.RequestOption) (nextmail.Object, error)
```

A partial update: a field you leave out keeps what it holds. Sending `definition` replaces `match` and every condition at once, and `description` set to `nil` takes the description away.

A broadcast already sent or scheduled keeps the conditions it was given, so a change here only reaches the broadcasts sent after it.

Scopes: `audiences:write`.

**Parameters**

- `id` (`string`, required): The `seg_` id of the segment.
- `name` (`string`): A new name, 1 to 120 characters.
- `description` (`string or nil`): A new note. `nil` takes it away.
- `definition` (`nextmail.Body`): New conditions, replacing the old ones.
- `nextmail.WithAPIKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An `nextmail.Object` for the segment as it stands after the change.

**Example**

```go
segment, err := client.Segments.Update(ctx, "seg_5d2a9c7e1b4f08a36c1e9b42", nextmail.Body{
	"name": "Opened in the last 30 days",
	"definition": nextmail.Body{
		"conditions": []nextmail.Body{{"field": "opened", "operator": "within_days", "days": 30}},
	},
})
if err != nil {
	return err
}

fmt.Println(segment.String("updatedAt"))
```

**Notes**

- The SDK retries it after a network failure, since sending the same change twice leaves the same segment.
- A segment from another workspace is a 404 `segment_not_found`.

Also available in: API [`PATCH /segments/{id}`](https://openemail.uk/docs/api/reference/audiences#patch-segments-id); TypeScript [`nextmail.segments.update()`](https://openemail.uk/docs/sdk/reference/segments#update); Python [`segments.update()`](https://openemail.uk/docs/python/reference/segments#update); Ruby [`segments.update`](https://openemail.uk/docs/ruby/reference/segments#update); PHP [`segments->update`](https://openemail.uk/docs/php/reference/segments#update); Java [`segments().update`](https://openemail.uk/docs/java/reference/segments#update); C# [`Segments.UpdateAsync`](https://openemail.uk/docs/csharp/reference/segments#update); CLI [`openemail segments update`](https://openemail.uk/docs/cli/reference/segments#segments-update).

### `Segments.Delete`

Delete a segment

```go
Delete(ctx context.Context, id string, opts ...nextmail.RequestOption) (nextmail.Object, error)
```

Deletes the saved segment. No contact is touched, and a broadcast already sent or scheduled keeps the conditions it was given.

A draft that still names the segment in `segmentId` is refused with 404 `segment_not_found` when it is sent, until it names another segment or none.

Scopes: `audiences:write`.

**Parameters**

- `id` (`string`, required): The `seg_` id of the segment.
- `nextmail.WithAPIKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An `nextmail.Object` with `object` set to `segment`, `id` and `deleted` set to true.

**Example**

```go
removed, err := client.Segments.Delete(ctx, "seg_5d2a9c7e1b4f08a36c1e9b42")
if err != nil {
	return err
}

fmt.Println(removed.String("id"))
```

**Notes**

- The SDK does not retry a delete. A 404 on your own second attempt after a lost response means the first one worked.

Also available in: API [`DELETE /segments/{id}`](https://openemail.uk/docs/api/reference/audiences#delete-segments-id); TypeScript [`nextmail.segments.delete()`](https://openemail.uk/docs/sdk/reference/segments#delete); Python [`segments.delete()`](https://openemail.uk/docs/python/reference/segments#delete); Ruby [`segments.delete`](https://openemail.uk/docs/ruby/reference/segments#delete); PHP [`segments->delete`](https://openemail.uk/docs/php/reference/segments#delete); Java [`segments().delete`](https://openemail.uk/docs/java/reference/segments#delete); C# [`Segments.DeleteAsync`](https://openemail.uk/docs/csharp/reference/segments#delete); CLI [`openemail segments delete`](https://openemail.uk/docs/cli/reference/segments#segments-delete).

### `Segments.Preview`

Count the contacts a segment matches

```go
Preview(ctx context.Context, body nextmail.Body, opts ...nextmail.RequestOption) (nextmail.Object, error)
```

Counts the contacts a set of conditions matches now and returns up to 10 of them, without saving or sending anything. Pass the `definition` of a saved segment to count it, or new conditions to try them first.

With `audienceIds`, only the subscribed members of those audiences count, which is the number a broadcast to them with this segment would start from before unsubscribes and suppressions are taken out. `Broadcasts.Preview` with `segmentId` gives that final number.

Scopes: `audiences:read`.

**Parameters**

- `definition` (`nextmail.Body`, required): The conditions to count, as `Create` takes them.
- `audienceIds` (`[]string`): Up to 10 audiences. Only their subscribed members count. Left out, every contact of the workspace counts.
- `nextmail.WithAPIKey` (`string`): Overrides the client's API key for this call only.

**Returns**

An `nextmail.Object` with `contacts`, the number that match, and `sample`, up to 10 of them.

**Example**

```go
preview, err := client.Segments.Preview(ctx, nextmail.Body{
	"definition": nextmail.Body{
		"conditions": []nextmail.Body{{"field": "engaged", "operator": "not_within_days", "days": 180}},
	},
	"audienceIds": []string{"aud_4c1b8e2a7d9f05c36b4e8a71"},
})
if err != nil {
	return err
}

fmt.Println(preview.Int("contacts"), "people have not opened or clicked in six months")
```

**Notes**

- A condition on a custom field reads each contact it checks and stops at 20,000 of them, answering 422 `segment_too_large`. Narrow it with `audienceIds` first.
- The SDK retries it after a network failure or a retryable status, since it changes nothing.

Also available in: API [`POST /segments/preview`](https://openemail.uk/docs/api/reference/audiences#post-segments-preview); TypeScript [`nextmail.segments.preview()`](https://openemail.uk/docs/sdk/reference/segments#preview); Python [`segments.preview()`](https://openemail.uk/docs/python/reference/segments#preview); Ruby [`segments.preview`](https://openemail.uk/docs/ruby/reference/segments#preview); PHP [`segments->preview`](https://openemail.uk/docs/php/reference/segments#preview); Java [`segments().preview`](https://openemail.uk/docs/java/reference/segments#preview); C# [`Segments.PreviewAsync`](https://openemail.uk/docs/csharp/reference/segments#preview); CLI [`openemail segments preview`](https://openemail.uk/docs/cli/reference/segments#segments-preview).
