Kraiter
SDK Reference

Segments

SDK reference for creating and managing audience segments in Kraiter.

The kraiter.segments namespace provides methods for defining and computing dynamic audience segments. A segment is a rule-based grouping of contacts that is recomputed on demand.

create

Creates a new segment with a set of rules.

const segment = await kraiter.segments.create({
  name: 'Active Pro Users',
  rules: [
    { field: 'properties.plan', operator: 'equals', value: 'pro' },
    { field: 'lastActiveAt', operator: 'after', value: '2025-01-01T00:00:00Z' },
  ],
});

Parameters

ParameterTypeRequiredDescription
namestringYesA human-readable name for the segment.
rulesSegmentRule[]YesAn array of rules that define segment membership. All rules must match for a contact to be included (AND logic).

SegmentRule object

FieldTypeDescription
fieldstringThe contact field or property path to evaluate (e.g. email, properties.plan, lastActiveAt).
operatorstringThe comparison operator. Supported values: equals, not_equals, contains, not_contains, starts_with, ends_with, before, after, gt, lt, gte, lte, exists, not_exists.
valueunknownThe value to compare against. Type depends on the operator and field.

Returns

Promise<Segment> — the newly created segment object.

Errors

CodeWhen
VALIDATION_ERRORThe rules array is empty or contains invalid operators.

get

Retrieves a segment by ID.

const segment = await kraiter.segments.get('seg_abc123');
console.log(segment.name, segment.rules.length);

Parameters

ParameterTypeRequiredDescription
idstringYesThe segment ID.

Returns

Promise<Segment> — the segment object.

Errors

CodeWhen
NOT_FOUNDNo segment exists with this ID.

list

Lists all segments. Returns an async iterator.

for await (const segment of kraiter.segments.list()) {
  console.log(segment.id, segment.name);
}

Parameters

ParameterTypeRequiredDescription
cursorstringNoPagination cursor.
limitnumberNoMaximum items per page.

Returns

AsyncIterable<Segment> — an async iterator of segment objects.


update

Updates an existing segment. You can change the name, the rules, or both.

const updated = await kraiter.segments.update('seg_abc123', {
  name: 'Active Enterprise Users',
  rules: [
    { field: 'properties.plan', operator: 'equals', value: 'enterprise' },
    { field: 'lastActiveAt', operator: 'after', value: '2025-01-01T00:00:00Z' },
  ],
});

Parameters

ParameterTypeRequiredDescription
idstringYesThe segment ID to update.
namestringNoUpdated segment name.
rulesSegmentRule[]NoUpdated rules array.

Returns

Promise<Segment> — the updated segment object.

Errors

CodeWhen
NOT_FOUNDNo segment exists with this ID.
VALIDATION_ERRORThe updated rules are invalid.

delete

Permanently deletes a segment.

await kraiter.segments.delete('seg_abc123');

Parameters

ParameterTypeRequiredDescription
idstringYesThe segment ID to delete.

Returns

Promise<void>

Errors

CodeWhen
NOT_FOUNDNo segment exists with this ID.

compute

Triggers a recomputation of segment membership. This evaluates the segment rules against all contacts and updates the membership list. The method returns once the computation has been queued — membership updates may take a few moments to complete for large contact lists.

await kraiter.segments.compute('seg_abc123');

Parameters

ParameterTypeRequiredDescription
idstringYesThe segment ID to recompute.

Returns

Promise<void>

Errors

CodeWhen
NOT_FOUNDNo segment exists with this ID.

getMembers

Lists the contacts that belong to a segment. Returns an async iterator. Membership is based on the last computation — call compute first if you need fresh results.

// Recompute then iterate
await kraiter.segments.compute('seg_abc123');

for await (const contact of kraiter.segments.getMembers('seg_abc123')) {
  console.log(contact.email);
}

Parameters

ParameterTypeRequiredDescription
idstringYesThe segment ID.
cursorstringNoPagination cursor.
limitnumberNoMaximum items per page.

Returns

AsyncIterable<Contact> — an async iterator of contact objects belonging to the segment.

Errors

CodeWhen
NOT_FOUNDNo segment exists with this ID.