> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mailglyph.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Segments

> Create and manage segments with the MailGlyph Node.js SDK.

Use a **secret key** client for segments methods.

```ts theme={null}
import MailGlyph from 'mailglyph';

const client = new MailGlyph(process.env.MAILGLYPH_SECRET_KEY as string);
```

## List segments (`segments.list`)

```ts theme={null}
const segments = await client.segments.list();

console.log(segments.length);
```

## Create a segment (`segments.create`)

```ts theme={null}
const segment = await client.segments.create({
  name: 'Premium users',
  description: 'Users on paid plans',
  condition: {
    logic: 'AND',
    groups: [
      {
        filters: [
          { field: 'data.plan', operator: 'equals', value: 'premium' }
        ]
      }
    ]
  },
  trackMembership: true
});

console.log(segment.id, segment.memberCount);
```

## Get one segment (`segments.get`)

```ts theme={null}
const existing = await client.segments.get(segment.id);

console.log(existing.name);
```

## Update a segment (`segments.update`)

```ts theme={null}
const updated = await client.segments.update(segment.id, {
  name: 'VIP users',
  condition: {
    logic: 'AND',
    groups: [
      {
        filters: [
          { field: 'subscribed', operator: 'equals', value: true }
        ]
      }
    ]
  }
});

console.log(updated.name);
```

## List segment contacts (`segments.listContacts`)

```ts theme={null}
const members = await client.segments.listContacts(segment.id, {
  page: 1,
  pageSize: 20
});

console.log(members.total, members.page, members.data.length);
```

## Delete a segment (`segments.delete`)

```ts theme={null}
await client.segments.delete(segment.id);
```

See full details in the [Segments API reference](https://docs.mailglyph.com/api-reference/segments/listSegments.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.