---
title: "Audiences"
description: "`audiences.list`, `get`, `create`, `update`, `delete`, `empty`, `growth`, `listContacts`, `addContact`, `addContacts`, `importContacts`, `removeContact` and `removeContacts`."
url: "https://nextmail.sh/docs/sdk/audiences"
area: "TypeScript"
category: "Mailbox"
---

# Audiences

`audiences.list`, `get`, `create`, `update`, `delete`, `empty`, `growth`, `listContacts`, `addContact`, `addContacts`, `importContacts`, `removeContact` and `removeContacts`.

## Every method

**audiences.ts**

```
const audiences = await nextmail.audiences.listAll()
const everyone = audiences.find((audience) => audience.builtin === 'default')

const list = await nextmail.audiences.create({
  name: 'Product updates',
  description: 'Customers who asked to hear about releases',
})

await nextmail.contacts.create({ email: 'grace@example.com', name: 'Grace Hopper' })
await nextmail.audiences.addContact(list.id, { email: 'grace@example.com' })

const bulk = await nextmail.audiences.addContacts(list.id, {
  emails: ['ada@example.com', 'alan@example.com'],
})

const imported = await nextmail.audiences.importContacts(list.id, {
  contacts: [{ email: 'katherine@example.com', name: 'Katherine Johnson' }],
})

const members = await nextmail.audiences.listAllContacts(list.id, {
  q: 'grace',
  sort: 'added-newest',
  limit: 200,
})

const growth = await nextmail.audiences.growth({ audienceIds: [list.id], days: 30 })

await nextmail.audiences.update(list.id, { name: 'Release notes' })
await nextmail.audiences.removeContact(list.id, 'grace@example.com')
await nextmail.audiences.removeContacts(list.id, { emails: ['ada@example.com'] })
await nextmail.audiences.empty(list.id)
await nextmail.audiences.delete(list.id)

console.log(everyone?.contactCount, bulk.missing, imported.created, members.length, growth.totals.added)
```

An audience is a named list of contacts in this workspace. Every contact is in the built-in default audience from the moment it exists, and `builtin` is what names that row; the rest are yours to make, fill and delete. Branch on `builtin` rather than on the name, which anybody can change.

Send to one or more audiences with `nextmail.broadcasts.send`, on the Broadcasts page. Putting a contact in an audience is a write to the audience rather than to the contact, so `audiences:write` is the only scope checked. `importContacts` is the exception: it creates contacts, so it needs `contacts:write` as well.

> `addContact` takes an address that is already a contact and refuses one that is not, with 422 `contact_not_found`. Save it with `nextmail.contacts.create` first. Adding somebody twice answers with the membership that is already there, carrying its original `addedAt`, so the call is safe to retry.

> The default audience can be renamed and described like any other, but it cannot be deleted and it cannot be thinned. Both are refused with 409 `audience_immutable`. Delete the contact when you mean the contact to go.

## Response: AudienceResource

`list` resolves to one page of these, `{ items, hasMore, nextCursor }`, the default audience first and the rest newest first, and `listAll` and `iterate` walk every page; `get`, `create` and `update` each resolve to one. `listContacts` resolves to a page of `AudienceContactResource` instead, the contacts themselves with the date each one joined rather than membership records, with `listAllContacts` and `iterateContacts` beside it.

- `id` (string): The durable handle, `aud_` followed by 24 hex characters. Names are not unique, so this is what belongs in stored configuration.
- `name` (string): Trimmed on write, 1 to 120 characters. Two audiences may share a name, because an audience is addressed by its id.
- `description` (string | null): Free text for whoever reads the list later. Null when nobody wrote any, and an explicit null on `update` clears it.
- `builtin` ('default' | (string & {}) | null): `default` on exactly one row per workspace, the audience that holds every contact, and null on every audience somebody created. The union stays open so that a built-in added later narrows in an editor without breaking the published type.
- `contactCount` (number): How many contacts are in the audience, counted at the moment of the read rather than cached. Two reads either side of a `contacts.create` disagree by one.
- `lastContactAt` (string | null): ISO-8601 UTC, when the contact who joined most recently joined this audience. Null while the audience is empty.
- `createdAt` (string): ISO-8601 UTC, when the audience was made. Fixes the list order after the default one.
- `updatedAt` (string): ISO-8601 UTC, bumped by a rename or a description change. Membership changes do not touch it.

## Parameters: audiences.listContacts

- `limit` (number): How many contacts per page: an integer from 1 to 200, defaulting to 50.
- `cursor` (string): The `nextCursor` from the previous page, sent with the same `q`, `source` and `sort`. A cursor naming a contact that is not in this audience is a 400 `invalid_cursor`.
- `q` (string): Searches the name and the address, up to 200 characters. When nothing matches exactly on the first page, close spellings are returned instead, and the pages that follow keep matching the same way.
- `source` (ContactSource): `'manual'` for the contacts somebody saved on purpose, `'auto'` for the ones the app composer recorded. Leave it out for everyone in the audience.
- `sort` (AudienceMemberSort): `'last-heard-newest'` (the default) and `'last-heard-oldest'` go by `lastSeenAt`, and contacts that have never been mailed come last in the first and first in the second. `'added-newest'` and `'added-oldest'` go by when each contact joined this audience, and `'name'` ignores case and sorts a contact with no name by its address.
- `statuses` (AudienceMemberStatus[]): `['subscribed']` keeps the members who have not unsubscribed and `['unsubscribed']` the ones who have. Leave it out, or name both, for everyone in the audience. `AUDIENCE_MEMBER_STATUSES` holds the values.

## Response: AudienceContactResource

`listContacts` resolves to a `Page<AudienceContactResource>`, and `listAllContacts` and `iterateContacts` walk every page with the same options. Each row is a `ContactResource`, whose fields are on the Contacts page, with one more. Walking every page is how to export an audience.

- `addedAt` (string): ISO-8601 UTC, when the contact joined this audience. Taking a contact out and adding it again starts it afresh.
- `unsubscribedAt` (string | null): ISO-8601 UTC, when the contact unsubscribed from a broadcast sent to this audience, or null while it is subscribed. An unsubscribed contact stays in the audience, and broadcasts to it skip it. Taking it out and adding it again makes it subscribed afresh.

## Adding and removing in bulk

`addContacts` and `removeContacts` take `{ emails }`, 1 to 200 addresses, and change one audience in one request. `addContacts` never creates a contact: an address that is not one comes back in `missing`, and `importContacts` is the call that creates them. Both are safe to repeat, so a retry after a timeout reports the same people as already done rather than failing.

> Adding to the default audience answers `added: 0`, because every contact is in it already, and `removeContacts` on it is refused with 409 `audience_immutable`. Taking somebody out of an audience leaves them in the address book, in the default audience and in their other audiences.

- `audienceId` (string): The audience the call changed, on both results.
- `added` (number): On `AudienceBatchAddResource`: the new memberships this call made.
- `unchanged` (number): On `AudienceBatchAddResource`: contacts that were in the audience already. Nothing was written for them.
- `removed` (number): On `AudienceBatchRemoveResource`: the memberships this call took away.
- `notInAudience` (Array<string>): On `AudienceBatchRemoveResource`: contacts that were not in the audience, so nothing happened to them.
- `missing` (Array<string>): On both: the addresses that are not contacts in this workspace, lowercased and without repeats.

## Importing

`importContacts` is the CSV import on the audience page. It takes `{ contacts }`, 1 to 500 rows of `{ email, name? }`: each well-formed address becomes a contact if it is not one yet, and every one lands in the audience. Send a longer list in several calls. It needs `audiences:write` and `contacts:write`.

> An address that is already a contact is reused and keeps its name, and a `name` here only fills one that was empty. A new contact is saved as `manual` and joins the default audience too, and an address that was deleted from the book comes back. Replaying the same rows creates nothing twice.

> A row can also carry `attributes`, the custom fields of the contact, such as `plan`. Each value replaces the field of that name, an empty one is ignored, and the fields a row leaves out keep their values.

- `audienceId` (string): The audience the rows went into.
- `created` (number): New contacts this call saved.
- `added` (number): New memberships in this audience, counting contacts that existed already and were not in it yet.
- `skipped` (number): Rows that were not imported because the address was malformed.
- `invalid` (Array<string>): The malformed addresses, exactly as they were sent.

## Emptying

`empty(id)` takes every contact out of one audience in one request and resolves to an `EmptiedAudienceResource`: the audience as it now stands, with `contactCount` at 0, plus `removed`, the number of memberships taken away. The audience keeps its id, name and description, and every contact stays in the address book and in its other audiences.

> It cannot be undone and nothing records who was in the list, so walk `listAllContacts` first if you may want it back. The default audience cannot be emptied, and the call is refused with 409 `audience_immutable`.

## Growth

`growth(options)` reads how many contacts joined each audience over a window that ends now, by day, hour or minute, which is the chart on the audiences page. It needs `audiences:read` and resolves to an `AudienceGrowthResource`.

> An audience records when somebody joined and never when they left, so every figure counts the people still in the list today by the date they joined, and a line never falls. A contact who joined and later left is in none of the numbers.

**Parameters**

- `audienceIds` (Array<string>): Up to 50 audience ids, sent joined with commas. Leave it out for every audience. An id that is not an audience in this workspace is a 404 `audience_not_found`.
- `days` (number): How far back the window reaches, 1 to 1095. It is 30 when neither `days` nor `minutes` is given.
- `minutes` (number): The window in minutes, 1 to 1576800, for a window shorter than a day. It wins over `days` when both are given.
- `grain` (TrackingGrain): The size of each bucket: `day` (the default), `hour` or `minute`.
- `offsetMinutes` (number): The viewer's offset from UTC in minutes, -840 to 840, so day and hour buckets start at their local boundary. 0 by default.

**Response**

- `since` (string): ISO-8601 UTC, the start of the first bucket.
- `until` (string): ISO-8601 UTC, the moment of the read.
- `totals` (AudienceGrowthTotals): `contacts` counts each person once however many lists they are in, and `memberships` adds the lists up, so a person counts once for every list read that holds them. `added` sums the joins in the window, `lists` is how many audiences were read, and `busiest` is the bucket with the most joins, or null.
- `series` (Array<AudienceGrowthSeries>): One entry per audience, largest first: `id`, `name`, `builtin` (true on the default audience), `total` members now, `before` (those who joined before `since`), `added` (those who joined inside the window) and `buckets`, each `{ bucket, added }`. Only buckets with a join are listed, keyed `YYYY-MM-DD`, `YYYY-MM-DDTHH` or `YYYY-MM-DDTHH:MM` in the offset's local time.
