Skip to the documentation
Java

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

Scopesaudiences:read
Signature
List<Map<String, Object>> list(RequestOptions options)

Returns every saved segment of the workspace as a plain list, 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 segments().preview to count the matches and pass a segment id as segmentId to broadcasts().send to narrow a broadcast with it.

Parameters

options.apiKeyString

Overrides the client's API key for this call only.

Returns

A list of maps, each with id, name, description, definition, createdAt and updatedAt.

Example

List<Map<String, Object>> segments = client.segments().list(); for (Map<String, Object> segment : segments) {    System.out.println(segment.get("id") + " " + segment.get("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
TypeScript
nextmail.segments.list()
Python
segments.list()
Ruby
segments.list
PHP
segments->list
Go
Segments.List
C#
Segments.ListAsync
CLI
openemail segments list

segments().get

Get one segment

Scopesaudiences:read
Signature
Map<String, Object> get(String id, RequestOptions options)

Returns one saved segment by its id, with match and every condition in definition. The conditions come back as they were saved, so they can be edited and passed to segments().update or segments().preview as they are.

Parameters

idStringRequired

Segment id such as seg_5d2a9c7e1b4f08a36c1e9b42.

options.apiKeyString

Overrides the client's API key for this call only.

Returns

A map for the segment.

Example

Map<String, Object> segment = client.segments().get("seg_5d2a9c7e1b4f08a36c1e9b42"); System.out.println(segment.get("name") + " " + segment.get("definition"));

Notes

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

Also available in

API
GET /segments/{id}
TypeScript
nextmail.segments.get()
Python
segments.get()
Ruby
segments.get
PHP
segments->get
Go
Segments.Get
C#
Segments.GetAsync
CLI
openemail segments get

segments().create

Save a segment

Scopesaudiences:write
Signature
Map<String, Object> create(Map<String, Object> body, RequestOptions options)

Saves a set of conditions under a name. match in definition is all (the default) when a contact has to meet every condition, or any when one is enough, and its conditions hold 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.

Parameters

body.nameStringRequired

What the segment is called, 1 to 120 characters.

body.descriptionString

A longer note, up to 500 characters.

body.definitionMap<String, Object>Required

match (all or any) and conditions, 1 to 20 of them.

options.apiKeyString

Overrides the client's API key for this call only.

Returns

A map for the segment, with its new seg_ id.

Example

Map<String, Object> segment = client.segments().create(Body.of(    "name", "Opened in the last 90 days",    "definition", Body.of(        "match", "all",        "conditions", List.of(Body.of("field", "opened", "operator", "within_days", "days", 90))    ))); client.broadcasts().send(Body.of(    "audienceIds", List.of("aud_4c1b8e2a7d9f05c36b4e8a71"),    "segmentId", segment.get("id"),    "from", "Acme <[email protected]>",    "subject", "Thanks for reading",    "text", "Hi {{firstName|there}}, here is something for our regular readers."));

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
TypeScript
nextmail.segments.create()
Python
segments.create()
Ruby
segments.create
PHP
segments->create
Go
Segments.Create
C#
Segments.CreateAsync
CLI
openemail segments create

segments().update

Change a segment

Scopesaudiences:write
Signature
Map<String, Object> update(String id, Map<String, Object> patch, RequestOptions options)

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 null 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.

Parameters

idStringRequired

The seg_ id of the segment.

patch.nameString

A new name, 1 to 120 characters.

patch.descriptionString or null

A new note. Null takes it away.

patch.definitionMap<String, Object>

New conditions, replacing the old ones.

options.apiKeyString

Overrides the client's API key for this call only.

Returns

A map for the segment as it stands after the change.

Example

Map<String, Object> segment = client.segments().update("seg_5d2a9c7e1b4f08a36c1e9b42", Body.of(    "name", "Opened in the last 30 days",    "definition", Body.of(        "conditions", List.of(Body.of("field", "opened", "operator", "within_days", "days", 30))    ))); System.out.println(segment.get("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}
TypeScript
nextmail.segments.update()
Python
segments.update()
Ruby
segments.update
PHP
segments->update
Go
Segments.Update
C#
Segments.UpdateAsync
CLI
openemail segments update

segments().delete

Delete a segment

Scopesaudiences:write
Signature
Map<String, Object> delete(String id, RequestOptions options)

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.

Parameters

idStringRequired

The seg_ id of the segment.

options.apiKeyString

Overrides the client's API key for this call only.

Returns

A map with object set to segment, id and deleted set to true.

Example

Map<String, Object> removed = client.segments().delete("seg_5d2a9c7e1b4f08a36c1e9b42"); System.out.println(removed.get("id") + " " + removed.get("deleted"));

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}
TypeScript
nextmail.segments.delete()
Python
segments.delete()
Ruby
segments.delete
PHP
segments->delete
Go
Segments.Delete
C#
Segments.DeleteAsync
CLI
openemail segments delete

segments().preview

Count the contacts a segment matches

Scopesaudiences:read
Signature
Map<String, Object> preview(Map<String, Object> body, RequestOptions options)

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.

Parameters

body.definitionMap<String, Object>Required

The conditions to count, as segments().create takes them.

body.audienceIdsList<String>

Up to 10 audiences. Only their subscribed members count. Left out, every contact of the workspace counts.

options.apiKeyString

Overrides the client's API key for this call only.

Returns

A map with contacts, the number that match, and sample, up to 10 of them.

Example

Map<String, Object> preview = client.segments().preview(Body.of(    "definition", Body.of(        "conditions", List.of(Body.of("field", "engaged", "operator", "not_within_days", "days", 180))    ),    "audienceIds", List.of("aud_4c1b8e2a7d9f05c36b4e8a71"))); System.out.println(preview.get("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
TypeScript
nextmail.segments.preview()
Python
segments.preview()
Ruby
segments.preview
PHP
segments->preview
Go
Segments.Preview
C#
Segments.PreviewAsync
CLI
openemail segments preview