# Segments

A segment picks contacts by rules: make one, count who it matches, and change its rules. This page is generated from the OpenAPI document of release `main-6976024`, the one the API serves, so it lists only what the API has.

How to authenticate, where errors are explained, and the other resources are in the [API reference](https://flyanote.com/docs/api).

## Operations

* [The account's segments](https://flyanote.com/docs/api/segments#list-segments): `GET /v1/segments`
* [Make a segment](https://flyanote.com/docs/api/segments#create-segment): `POST /v1/segments`
* [Read a segment](https://flyanote.com/docs/api/segments#get-segment): `GET /v1/segments/{segment_id}`
* [Rename or archive a segment](https://flyanote.com/docs/api/segments#update-segment): `PATCH /v1/segments/{segment_id}`
* [Delete a segment](https://flyanote.com/docs/api/segments#delete-segment): `DELETE /v1/segments/{segment_id}`
* [Replace the rules](https://flyanote.com/docs/api/segments#replace-segment-rules): `PUT /v1/segments/{segment_id}/rules`
* [Discard the draft](https://flyanote.com/docs/api/segments#discard-segment-draft): `DELETE /v1/segments/{segment_id}/draft`
* [Count rules before saving them](https://flyanote.com/docs/api/segments#count-segment): `POST /v1/segments/count`
* [A segment's counts](https://flyanote.com/docs/api/segments#get-segment-counts): `GET /v1/segments/{segment_id}/counts`

## The account's segments

`GET /v1/segments`

A page of segments, oldest first, without their rules. Archived ones only with `state=all`.

Its API key needs `segments:read`.

### Parameters

| Parameter | What it is                                                     |
| --------- | -------------------------------------------------------------- |
| cursor    | text, in the query. The previous page’s next\_cursor.          |
| limit     | a whole number from 1 to 100, in the query.                    |
| state     | one of active or all, in the query. all includes the archived. |

### Answers

* **200**: A page of segments
* **401**:  
  * API key missing ([api\_key\_missing](https://flyanote.com/docs/errors#api-key-missing))
  * Invalid API key ([api\_key\_invalid](https://flyanote.com/docs/errors#api-key-invalid))
  * API key revoked ([api\_key\_revoked](https://flyanote.com/docs/errors#api-key-revoked))
  * API key expired ([api\_key\_expired](https://flyanote.com/docs/errors#api-key-expired))
* **403**:  
  * Account closed ([account\_closed](https://flyanote.com/docs/errors#account-closed))
  * Missing scope ([api\_key\_scope\_missing](https://flyanote.com/docs/errors#api-key-scope-missing))
  * Test keys can't do this ([test\_key\_not\_allowed](https://flyanote.com/docs/errors#test-key-not-allowed))
* **422**:  
  * Validation failed ([validation\_failed](https://flyanote.com/docs/errors#validation-failed))
* **429**:  
  * Too many requests ([rate\_limited](https://flyanote.com/docs/errors#rate-limited))
* **500**:  
  * Internal error ([internal](https://flyanote.com/docs/errors#internal))
* **503**:  
  * API keys unavailable ([api\_keys\_unavailable](https://flyanote.com/docs/errors#api-keys-unavailable))

### What a 200 answers

| Field                                | What it is                            |
| ------------------------------------ | ------------------------------------- |
| data (required)                      | a list, each an object.               |
| data\[\].id (required)               | text.                                 |
| data\[\].name (required)             | text.                                 |
| data\[\].applied\_version (required) | a whole number.                       |
| data\[\].revision (required)         | a whole number.                       |
| data\[\].has\_draft (required)       | true or false.                        |
| data\[\].archived\_at (required)     | a date and time (RFC 3339), or null.  |
| data\[\].created\_at (required)      | a date and time (RFC 3339).           |
| data\[\].updated\_at (required)      | a date and time (RFC 3339).           |
| next\_cursor (required)              | text, or null. Null on the last page. |

## Make a segment

`POST /v1/segments`

Stores the rules as the segment's applied rules, version 1\. The rules are refused as 422 `segment_invalid`, with `errors[0].pointer` at the first rule refused and `errors[0].code` why, never the value: a shape the tree doesn’t take, a list, tag, or field the account doesn’t have, an operator the field’s type doesn’t take, a bound passed, or an engagement rule where engagement isn’t recorded yet. An account holds at most 200 segments, archived ones included. A segment that isn't archived with this name, these rules, and no draft already answers 200, so a retry is safe, to a caller that may read segments (a key needs `segments:read` for it); one with this name in any case and anything else is 409 `segment_name_taken`.

Its API key needs `segments:write`.

### Request body

A `application/json` body.

| Field            | What it is                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| name (required)  | text, up to 100 characters.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| rules (required) | an object. A rule tree, version 1: { "version": 1, "match": "all" \| "any", "rules": \[ … \] }, where each rule is a group (match and rules again) or a leaf with a kind and an op. The kinds: list (on, not\_on, with list\_id: a tpc\_… id), tag (has, not\_has, with tag\_id), field (with field\_id: is and is\_not with a value of the field's type; less\_than and greater\_than with a number; before and after with a date as YYYY-MM-DD; in\_last with days; is\_set and is\_not\_set), clicked, not\_clicked, and opened (in\_last with days), joined (before, after, or in\_last), and location (is, is\_not, with a country as ISO 3166-1 alpha-2 and an optional region as ISO 3166-2). location reads the place a contact states first: the country set on the contact, then the person's own (a form's country field, a postal code). Only a contact with neither is placed by the country of a request they made, which is an estimate: the connection's country, not the person's. A stated country with no region takes the region from the next of those that names the same country, a request's included, and never takes another country. A language or a time zone places nobody. A negative matches a contact with no value. opened is an estimate: a mail app can open a message by itself, and a click counts as an open, so it has no negative. At most 30 leaves in a tree, 15 rules in a group, 3 levels, and in\_last 3650 days; days count from midnight in the account's time zone. Anything else is 422 segment\_invalid at the rule's JSON pointer. |

### Answers

* **200**: The account already had it, as asked
* **201**: Made
* **400**:  
  * Malformed request ([malformed\_request](https://flyanote.com/docs/errors#malformed-request))
* **401**:  
  * API key missing ([api\_key\_missing](https://flyanote.com/docs/errors#api-key-missing))
  * Invalid API key ([api\_key\_invalid](https://flyanote.com/docs/errors#api-key-invalid))
  * API key revoked ([api\_key\_revoked](https://flyanote.com/docs/errors#api-key-revoked))
  * API key expired ([api\_key\_expired](https://flyanote.com/docs/errors#api-key-expired))
* **403**:  
  * Account closed ([account\_closed](https://flyanote.com/docs/errors#account-closed))
  * Missing scope ([api\_key\_scope\_missing](https://flyanote.com/docs/errors#api-key-scope-missing))
  * Test keys can't do this ([test\_key\_not\_allowed](https://flyanote.com/docs/errors#test-key-not-allowed))
* **409**:  
  * Segment name already in use ([segment\_name\_taken](https://flyanote.com/docs/errors#segment-name-taken))
  * Too many segments ([segment\_limit\_reached](https://flyanote.com/docs/errors#segment-limit-reached))
* **413**:  
  * Request body too large ([payload\_too\_large](https://flyanote.com/docs/errors#payload-too-large))
* **415**:  
  * Unsupported media type ([unsupported\_media\_type](https://flyanote.com/docs/errors#unsupported-media-type))
* **422**:  
  * Segment rules invalid ([segment\_invalid](https://flyanote.com/docs/errors#segment-invalid))
  * Validation failed ([validation\_failed](https://flyanote.com/docs/errors#validation-failed))
* **429**:  
  * Too many requests ([rate\_limited](https://flyanote.com/docs/errors#rate-limited))
* **500**:  
  * Internal error ([internal](https://flyanote.com/docs/errors#internal))
* **503**:  
  * API keys unavailable ([api\_keys\_unavailable](https://flyanote.com/docs/errors#api-keys-unavailable))
  * Segments unavailable ([segments\_unavailable](https://flyanote.com/docs/errors#segments-unavailable))

### What a 200 answers

| Field                        | What it is                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| id (required)                | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| name (required)              | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| rules (required)             | an object. A rule tree, version 1: { "version": 1, "match": "all" \| "any", "rules": \[ … \] }, where each rule is a group (match and rules again) or a leaf with a kind and an op. The kinds: list (on, not\_on, with list\_id: a tpc\_… id), tag (has, not\_has, with tag\_id), field (with field\_id: is and is\_not with a value of the field's type; less\_than and greater\_than with a number; before and after with a date as YYYY-MM-DD; in\_last with days; is\_set and is\_not\_set), clicked, not\_clicked, and opened (in\_last with days), joined (before, after, or in\_last), and location (is, is\_not, with a country as ISO 3166-1 alpha-2 and an optional region as ISO 3166-2). location reads the place a contact states first: the country set on the contact, then the person's own (a form's country field, a postal code). Only a contact with neither is placed by the country of a request they made, which is an estimate: the connection's country, not the person's. A stated country with no region takes the region from the next of those that names the same country, a request's included, and never takes another country. A language or a time zone places nobody. A negative matches a contact with no value. opened is an estimate: a mail app can open a message by itself, and a click counts as an open, so it has no negative. At most 30 leaves in a tree, 15 rules in a group, 3 levels, and in\_last 3650 days; days count from midnight in the account's time zone. Anything else is 422 segment\_invalid at the rule's JSON pointer. |
| applied\_version (required)  | a whole number. Numbered from 1 with each change of the applied rules.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| applied\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| draft (required)             | an object, or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| revision (required)          | a whole number. Moves with every change of the rules, the draft, or the name: the ETag.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| used\_by (required)          | an object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| used\_by.sends (required)    | a whole number. How many scheduled emails (not yet gone) name the segment, as their audience or as an exclusion. While any does, the segment can’t be archived.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| used\_by.unpinned (required) | a whole number. How many of those still resolve the applied rules when they go, rather than a version they’ve already read. While any does, a change to the rules goes to draft and waits for an apply (issue 1581).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| archived\_at (required)      | a date and time (RFC 3339), or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| created\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| updated\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

### What a 201 answers

| Field                        | What it is                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| id (required)                | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| name (required)              | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| rules (required)             | an object. A rule tree, version 1: { "version": 1, "match": "all" \| "any", "rules": \[ … \] }, where each rule is a group (match and rules again) or a leaf with a kind and an op. The kinds: list (on, not\_on, with list\_id: a tpc\_… id), tag (has, not\_has, with tag\_id), field (with field\_id: is and is\_not with a value of the field's type; less\_than and greater\_than with a number; before and after with a date as YYYY-MM-DD; in\_last with days; is\_set and is\_not\_set), clicked, not\_clicked, and opened (in\_last with days), joined (before, after, or in\_last), and location (is, is\_not, with a country as ISO 3166-1 alpha-2 and an optional region as ISO 3166-2). location reads the place a contact states first: the country set on the contact, then the person's own (a form's country field, a postal code). Only a contact with neither is placed by the country of a request they made, which is an estimate: the connection's country, not the person's. A stated country with no region takes the region from the next of those that names the same country, a request's included, and never takes another country. A language or a time zone places nobody. A negative matches a contact with no value. opened is an estimate: a mail app can open a message by itself, and a click counts as an open, so it has no negative. At most 30 leaves in a tree, 15 rules in a group, 3 levels, and in\_last 3650 days; days count from midnight in the account's time zone. Anything else is 422 segment\_invalid at the rule's JSON pointer. |
| applied\_version (required)  | a whole number. Numbered from 1 with each change of the applied rules.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| applied\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| draft (required)             | an object, or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| revision (required)          | a whole number. Moves with every change of the rules, the draft, or the name: the ETag.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| used\_by (required)          | an object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| used\_by.sends (required)    | a whole number. How many scheduled emails (not yet gone) name the segment, as their audience or as an exclusion. While any does, the segment can’t be archived.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| used\_by.unpinned (required) | a whole number. How many of those still resolve the applied rules when they go, rather than a version they’ve already read. While any does, a change to the rules goes to draft and waits for an apply (issue 1581).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| archived\_at (required)      | a date and time (RFC 3339), or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| created\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| updated\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

## Read a segment

`GET /v1/segments/{segment_id}`

Its applied rules, its draft if it has one, and how many scheduled emails name it.

Its API key needs `segments:read`.

### Parameters

| Parameter              | What it is                                                         |
| ---------------------- | ------------------------------------------------------------------ |
| segment\_id (required) | text, up to 64 characters, in the path. The segment’s id (seg\_…). |

### Answers

* **200**: The segment
* **401**:  
  * API key missing ([api\_key\_missing](https://flyanote.com/docs/errors#api-key-missing))
  * Invalid API key ([api\_key\_invalid](https://flyanote.com/docs/errors#api-key-invalid))
  * API key revoked ([api\_key\_revoked](https://flyanote.com/docs/errors#api-key-revoked))
  * API key expired ([api\_key\_expired](https://flyanote.com/docs/errors#api-key-expired))
* **403**:  
  * Account closed ([account\_closed](https://flyanote.com/docs/errors#account-closed))
  * Missing scope ([api\_key\_scope\_missing](https://flyanote.com/docs/errors#api-key-scope-missing))
  * Test keys can't do this ([test\_key\_not\_allowed](https://flyanote.com/docs/errors#test-key-not-allowed))
* **404**:  
  * Segment not found ([segment\_not\_found](https://flyanote.com/docs/errors#segment-not-found))
* **422**:  
  * Validation failed ([validation\_failed](https://flyanote.com/docs/errors#validation-failed))
* **429**:  
  * Too many requests ([rate\_limited](https://flyanote.com/docs/errors#rate-limited))
* **500**:  
  * Internal error ([internal](https://flyanote.com/docs/errors#internal))
* **503**:  
  * API keys unavailable ([api\_keys\_unavailable](https://flyanote.com/docs/errors#api-keys-unavailable))

### What a 200 answers

| Field                        | What it is                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| id (required)                | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| name (required)              | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| rules (required)             | an object. A rule tree, version 1: { "version": 1, "match": "all" \| "any", "rules": \[ … \] }, where each rule is a group (match and rules again) or a leaf with a kind and an op. The kinds: list (on, not\_on, with list\_id: a tpc\_… id), tag (has, not\_has, with tag\_id), field (with field\_id: is and is\_not with a value of the field's type; less\_than and greater\_than with a number; before and after with a date as YYYY-MM-DD; in\_last with days; is\_set and is\_not\_set), clicked, not\_clicked, and opened (in\_last with days), joined (before, after, or in\_last), and location (is, is\_not, with a country as ISO 3166-1 alpha-2 and an optional region as ISO 3166-2). location reads the place a contact states first: the country set on the contact, then the person's own (a form's country field, a postal code). Only a contact with neither is placed by the country of a request they made, which is an estimate: the connection's country, not the person's. A stated country with no region takes the region from the next of those that names the same country, a request's included, and never takes another country. A language or a time zone places nobody. A negative matches a contact with no value. opened is an estimate: a mail app can open a message by itself, and a click counts as an open, so it has no negative. At most 30 leaves in a tree, 15 rules in a group, 3 levels, and in\_last 3650 days; days count from midnight in the account's time zone. Anything else is 422 segment\_invalid at the rule's JSON pointer. |
| applied\_version (required)  | a whole number. Numbered from 1 with each change of the applied rules.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| applied\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| draft (required)             | an object, or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| revision (required)          | a whole number. Moves with every change of the rules, the draft, or the name: the ETag.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| used\_by (required)          | an object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| used\_by.sends (required)    | a whole number. How many scheduled emails (not yet gone) name the segment, as their audience or as an exclusion. While any does, the segment can’t be archived.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| used\_by.unpinned (required) | a whole number. How many of those still resolve the applied rules when they go, rather than a version they’ve already read. While any does, a change to the rules goes to draft and waits for an apply (issue 1581).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| archived\_at (required)      | a date and time (RFC 3339), or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| created\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| updated\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

## Rename or archive a segment

`PATCH /v1/segments/{segment_id}`

Changes only what the body names. Archiving takes the segment off the lists and frees its name; its history stays, and it can’t be undone. A segment a scheduled email names can’t be archived (409 `segment_in_use`). Counts toward the account’s `segment-change` limit when it changes something, and when it renamed before its archive was refused: the rename is undone, but it ran.

Its API key needs `segments:write`.

### Parameters

| Parameter              | What it is                                                         |
| ---------------------- | ------------------------------------------------------------------ |
| segment\_id (required) | text, up to 64 characters, in the path. The segment’s id (seg\_…). |

### Request body

A `application/json` body.

| Field    | What it is                  |
| -------- | --------------------------- |
| name     | text, up to 100 characters. |
| archived | true. Archive it.           |

### Answers

* **200**: The segment as it now is
* **400**:  
  * Malformed request ([malformed\_request](https://flyanote.com/docs/errors#malformed-request))
* **401**:  
  * API key missing ([api\_key\_missing](https://flyanote.com/docs/errors#api-key-missing))
  * Invalid API key ([api\_key\_invalid](https://flyanote.com/docs/errors#api-key-invalid))
  * API key revoked ([api\_key\_revoked](https://flyanote.com/docs/errors#api-key-revoked))
  * API key expired ([api\_key\_expired](https://flyanote.com/docs/errors#api-key-expired))
* **403**:  
  * Account closed ([account\_closed](https://flyanote.com/docs/errors#account-closed))
  * Missing scope ([api\_key\_scope\_missing](https://flyanote.com/docs/errors#api-key-scope-missing))
  * Test keys can't do this ([test\_key\_not\_allowed](https://flyanote.com/docs/errors#test-key-not-allowed))
* **404**:  
  * Segment not found ([segment\_not\_found](https://flyanote.com/docs/errors#segment-not-found))
* **409**:  
  * Segment name already in use ([segment\_name\_taken](https://flyanote.com/docs/errors#segment-name-taken))
  * Segment in use ([segment\_in\_use](https://flyanote.com/docs/errors#segment-in-use))
* **413**:  
  * Request body too large ([payload\_too\_large](https://flyanote.com/docs/errors#payload-too-large))
* **415**:  
  * Unsupported media type ([unsupported\_media\_type](https://flyanote.com/docs/errors#unsupported-media-type))
* **422**:  
  * Validation failed ([validation\_failed](https://flyanote.com/docs/errors#validation-failed))
* **429**:  
  * Too many requests ([rate\_limited](https://flyanote.com/docs/errors#rate-limited))
* **500**:  
  * Internal error ([internal](https://flyanote.com/docs/errors#internal))
* **503**:  
  * API keys unavailable ([api\_keys\_unavailable](https://flyanote.com/docs/errors#api-keys-unavailable))
  * Segments unavailable ([segments\_unavailable](https://flyanote.com/docs/errors#segments-unavailable))

### What a 200 answers

| Field                        | What it is                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| id (required)                | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| name (required)              | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| rules (required)             | an object. A rule tree, version 1: { "version": 1, "match": "all" \| "any", "rules": \[ … \] }, where each rule is a group (match and rules again) or a leaf with a kind and an op. The kinds: list (on, not\_on, with list\_id: a tpc\_… id), tag (has, not\_has, with tag\_id), field (with field\_id: is and is\_not with a value of the field's type; less\_than and greater\_than with a number; before and after with a date as YYYY-MM-DD; in\_last with days; is\_set and is\_not\_set), clicked, not\_clicked, and opened (in\_last with days), joined (before, after, or in\_last), and location (is, is\_not, with a country as ISO 3166-1 alpha-2 and an optional region as ISO 3166-2). location reads the place a contact states first: the country set on the contact, then the person's own (a form's country field, a postal code). Only a contact with neither is placed by the country of a request they made, which is an estimate: the connection's country, not the person's. A stated country with no region takes the region from the next of those that names the same country, a request's included, and never takes another country. A language or a time zone places nobody. A negative matches a contact with no value. opened is an estimate: a mail app can open a message by itself, and a click counts as an open, so it has no negative. At most 30 leaves in a tree, 15 rules in a group, 3 levels, and in\_last 3650 days; days count from midnight in the account's time zone. Anything else is 422 segment\_invalid at the rule's JSON pointer. |
| applied\_version (required)  | a whole number. Numbered from 1 with each change of the applied rules.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| applied\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| draft (required)             | an object, or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| revision (required)          | a whole number. Moves with every change of the rules, the draft, or the name: the ETag.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| used\_by (required)          | an object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| used\_by.sends (required)    | a whole number. How many scheduled emails (not yet gone) name the segment, as their audience or as an exclusion. While any does, the segment can’t be archived.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| used\_by.unpinned (required) | a whole number. How many of those still resolve the applied rules when they go, rather than a version they’ve already read. While any does, a change to the rules goes to draft and waits for an apply (issue 1581).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| archived\_at (required)      | a date and time (RFC 3339), or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| created\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| updated\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

## Delete a segment

`DELETE /v1/segments/{segment_id}`

Deletes an archived segment and its versions, for good: archive it first (409 `segment_not_archived` otherwise). One any send named, sent or cancelled, or a campaign email names (its draft, or a version of it), stays archived (409 `segment_in_use`): the email keeps it. Counts toward the account’s `segment-delete` limit, unless it’s refused before the delete runs (404, or 409 `segment_not_archived`).

Its API key needs `segments:write`.

### Parameters

| Parameter              | What it is                                                         |
| ---------------------- | ------------------------------------------------------------------ |
| segment\_id (required) | text, up to 64 characters, in the path. The segment’s id (seg\_…). |

### Answers

* **204**: Deleted
* **401**:  
  * API key missing ([api\_key\_missing](https://flyanote.com/docs/errors#api-key-missing))
  * Invalid API key ([api\_key\_invalid](https://flyanote.com/docs/errors#api-key-invalid))
  * API key revoked ([api\_key\_revoked](https://flyanote.com/docs/errors#api-key-revoked))
  * API key expired ([api\_key\_expired](https://flyanote.com/docs/errors#api-key-expired))
* **403**:  
  * Account closed ([account\_closed](https://flyanote.com/docs/errors#account-closed))
  * Missing scope ([api\_key\_scope\_missing](https://flyanote.com/docs/errors#api-key-scope-missing))
  * Test keys can't do this ([test\_key\_not\_allowed](https://flyanote.com/docs/errors#test-key-not-allowed))
* **404**:  
  * Segment not found ([segment\_not\_found](https://flyanote.com/docs/errors#segment-not-found))
* **409**:  
  * Archive the segment first ([segment\_not\_archived](https://flyanote.com/docs/errors#segment-not-archived))
  * Segment in use ([segment\_in\_use](https://flyanote.com/docs/errors#segment-in-use))
* **422**:  
  * Validation failed ([validation\_failed](https://flyanote.com/docs/errors#validation-failed))
* **429**:  
  * Too many requests ([rate\_limited](https://flyanote.com/docs/errors#rate-limited))
* **500**:  
  * Internal error ([internal](https://flyanote.com/docs/errors#internal))
* **503**:  
  * API keys unavailable ([api\_keys\_unavailable](https://flyanote.com/docs/errors#api-keys-unavailable))
  * Segments unavailable ([segments\_unavailable](https://flyanote.com/docs/errors#segments-unavailable))

## Replace the rules

`PUT /v1/segments/{segment_id}/rules`

Applies the rules at once, unless a scheduled email that hasn't yet read a version names the segment (`used_by.unpinned`): then they're saved as its `draft`, and the email keeps resolving the applied rules until the draft is applied (issue 1581) or discarded. Rules equal to the applied ones end any draft. The rules are refused as 422 `segment_invalid`, with `errors[0].pointer` at the first rule refused and `errors[0].code` why, never the value: a shape the tree doesn’t take, a list, tag, or field the account doesn’t have, an operator the field’s type doesn’t take, a bound passed, or an engagement rule where engagement isn’t recorded yet. A retry of a change that already applied answers `unchanged`, whatever `If-Match` says. Counts toward the account’s `segment-change` limit, unless it answers `unchanged` or is refused.

Its API key needs `segments:write`.

### Parameters

| Parameter              | What it is                                                                                                                                                                                                                                                                                                                   |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| segment\_id (required) | text, up to 64 characters, in the path. The segment’s id (seg\_…).                                                                                                                                                                                                                                                           |
| if-match               | text, in the header. Required. The ETag you read, so the change applies only if nobody changed it since: 428 if\_match\_missing without it, and 412 revision\_mismatch if it changed. \* replaces it whatever it holds. A retry of a change that already applied succeeds, so after a timeout, retry with the same If-Match. |

### Request body

A `application/json` body.

| Field            | What it is                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| rules (required) | an object. A rule tree, version 1: { "version": 1, "match": "all" \| "any", "rules": \[ … \] }, where each rule is a group (match and rules again) or a leaf with a kind and an op. The kinds: list (on, not\_on, with list\_id: a tpc\_… id), tag (has, not\_has, with tag\_id), field (with field\_id: is and is\_not with a value of the field's type; less\_than and greater\_than with a number; before and after with a date as YYYY-MM-DD; in\_last with days; is\_set and is\_not\_set), clicked, not\_clicked, and opened (in\_last with days), joined (before, after, or in\_last), and location (is, is\_not, with a country as ISO 3166-1 alpha-2 and an optional region as ISO 3166-2). location reads the place a contact states first: the country set on the contact, then the person's own (a form's country field, a postal code). Only a contact with neither is placed by the country of a request they made, which is an estimate: the connection's country, not the person's. A stated country with no region takes the region from the next of those that names the same country, a request's included, and never takes another country. A language or a time zone places nobody. A negative matches a contact with no value. opened is an estimate: a mail app can open a message by itself, and a click counts as an open, so it has no negative. At most 30 leaves in a tree, 15 rules in a group, 3 levels, and in\_last 3650 days; days count from midnight in the account's time zone. Anything else is 422 segment\_invalid at the rule's JSON pointer. |

### Answers

* **200**: Saved
* **400**:  
  * Malformed request ([malformed\_request](https://flyanote.com/docs/errors#malformed-request))
* **401**:  
  * API key missing ([api\_key\_missing](https://flyanote.com/docs/errors#api-key-missing))
  * Invalid API key ([api\_key\_invalid](https://flyanote.com/docs/errors#api-key-invalid))
  * API key revoked ([api\_key\_revoked](https://flyanote.com/docs/errors#api-key-revoked))
  * API key expired ([api\_key\_expired](https://flyanote.com/docs/errors#api-key-expired))
* **403**:  
  * Account closed ([account\_closed](https://flyanote.com/docs/errors#account-closed))
  * Missing scope ([api\_key\_scope\_missing](https://flyanote.com/docs/errors#api-key-scope-missing))
  * Test keys can't do this ([test\_key\_not\_allowed](https://flyanote.com/docs/errors#test-key-not-allowed))
* **404**:  
  * Segment not found ([segment\_not\_found](https://flyanote.com/docs/errors#segment-not-found))
* **412**:  
  * Changed since you read it ([revision\_mismatch](https://flyanote.com/docs/errors#revision-mismatch))
* **413**:  
  * Request body too large ([payload\_too\_large](https://flyanote.com/docs/errors#payload-too-large))
* **415**:  
  * Unsupported media type ([unsupported\_media\_type](https://flyanote.com/docs/errors#unsupported-media-type))
* **422**:  
  * Segment rules invalid ([segment\_invalid](https://flyanote.com/docs/errors#segment-invalid))
  * Validation failed ([validation\_failed](https://flyanote.com/docs/errors#validation-failed))
* **428**:  
  * If-Match header needed ([if\_match\_missing](https://flyanote.com/docs/errors#if-match-missing))
* **429**:  
  * Too many requests ([rate\_limited](https://flyanote.com/docs/errors#rate-limited))
* **500**:  
  * Internal error ([internal](https://flyanote.com/docs/errors#internal))
* **503**:  
  * API keys unavailable ([api\_keys\_unavailable](https://flyanote.com/docs/errors#api-keys-unavailable))
  * Segments unavailable ([segments\_unavailable](https://flyanote.com/docs/errors#segments-unavailable))

### What a 200 answers

| Field                                | What it is                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| outcome (required)                   | one of applied, drafted or unchanged. applied: the rules are the segment’s now (a new applied\_version). drafted: a scheduled email still resolves the applied rules, so they went to draft, to apply or discard later. unchanged: the segment already held them.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| segment (required)                   | an object. A segment: rules are the applied rules, what a scheduled email resolves. draft holds a change saved while a scheduled email still resolves the applied rules (used\_by.unpinned), until it’s applied or discarded; null otherwise. Names are unique in the account among the unarchived, ignoring case.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| segment.id (required)                | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| segment.name (required)              | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| segment.rules (required)             | an object. A rule tree, version 1: { "version": 1, "match": "all" \| "any", "rules": \[ … \] }, where each rule is a group (match and rules again) or a leaf with a kind and an op. The kinds: list (on, not\_on, with list\_id: a tpc\_… id), tag (has, not\_has, with tag\_id), field (with field\_id: is and is\_not with a value of the field's type; less\_than and greater\_than with a number; before and after with a date as YYYY-MM-DD; in\_last with days; is\_set and is\_not\_set), clicked, not\_clicked, and opened (in\_last with days), joined (before, after, or in\_last), and location (is, is\_not, with a country as ISO 3166-1 alpha-2 and an optional region as ISO 3166-2). location reads the place a contact states first: the country set on the contact, then the person's own (a form's country field, a postal code). Only a contact with neither is placed by the country of a request they made, which is an estimate: the connection's country, not the person's. A stated country with no region takes the region from the next of those that names the same country, a request's included, and never takes another country. A language or a time zone places nobody. A negative matches a contact with no value. opened is an estimate: a mail app can open a message by itself, and a click counts as an open, so it has no negative. At most 30 leaves in a tree, 15 rules in a group, 3 levels, and in\_last 3650 days; days count from midnight in the account's time zone. Anything else is 422 segment\_invalid at the rule's JSON pointer. |
| segment.applied\_version (required)  | a whole number. Numbered from 1 with each change of the applied rules.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| segment.applied\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| segment.draft (required)             | an object, or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| segment.revision (required)          | a whole number. Moves with every change of the rules, the draft, or the name: the ETag.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| segment.used\_by (required)          | an object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| segment.used\_by.sends (required)    | a whole number. How many scheduled emails (not yet gone) name the segment, as their audience or as an exclusion. While any does, the segment can’t be archived.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| segment.used\_by.unpinned (required) | a whole number. How many of those still resolve the applied rules when they go, rather than a version they’ve already read. While any does, a change to the rules goes to draft and waits for an apply (issue 1581).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| segment.archived\_at (required)      | a date and time (RFC 3339), or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| segment.created\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| segment.updated\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

## Discard the draft

`DELETE /v1/segments/{segment_id}/draft`

Drops the draft; the applied rules stand. A segment with no draft answers 200 as it is, whatever `If-Match` says: a retry, or nothing to discard. Counts toward the account’s `segment-change` limit when it drops a draft.

Its API key needs `segments:write`.

### Parameters

| Parameter              | What it is                                                                                                                                                                                                                                                                                 |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| segment\_id (required) | text, up to 64 characters, in the path. The segment’s id (seg\_…).                                                                                                                                                                                                                         |
| if-match               | text, in the header. Required. The ETag you read, so the change applies only if nobody changed it since: 428 if\_match\_missing without it, and 412 revision\_mismatch if it changed. A retry of a change that already applied succeeds, so after a timeout, retry with the same If-Match. |

### Answers

* **200**: The segment, with no draft
* **401**:  
  * API key missing ([api\_key\_missing](https://flyanote.com/docs/errors#api-key-missing))
  * Invalid API key ([api\_key\_invalid](https://flyanote.com/docs/errors#api-key-invalid))
  * API key revoked ([api\_key\_revoked](https://flyanote.com/docs/errors#api-key-revoked))
  * API key expired ([api\_key\_expired](https://flyanote.com/docs/errors#api-key-expired))
* **403**:  
  * Account closed ([account\_closed](https://flyanote.com/docs/errors#account-closed))
  * Missing scope ([api\_key\_scope\_missing](https://flyanote.com/docs/errors#api-key-scope-missing))
  * Test keys can't do this ([test\_key\_not\_allowed](https://flyanote.com/docs/errors#test-key-not-allowed))
* **404**:  
  * Segment not found ([segment\_not\_found](https://flyanote.com/docs/errors#segment-not-found))
* **409**:  
  * Segment has no draft ([segment\_draft\_missing](https://flyanote.com/docs/errors#segment-draft-missing))
* **412**:  
  * Changed since you read it ([revision\_mismatch](https://flyanote.com/docs/errors#revision-mismatch))
* **422**:  
  * Validation failed ([validation\_failed](https://flyanote.com/docs/errors#validation-failed))
* **428**:  
  * If-Match header needed ([if\_match\_missing](https://flyanote.com/docs/errors#if-match-missing))
* **429**:  
  * Too many requests ([rate\_limited](https://flyanote.com/docs/errors#rate-limited))
* **500**:  
  * Internal error ([internal](https://flyanote.com/docs/errors#internal))
* **503**:  
  * API keys unavailable ([api\_keys\_unavailable](https://flyanote.com/docs/errors#api-keys-unavailable))
  * Segments unavailable ([segments\_unavailable](https://flyanote.com/docs/errors#segments-unavailable))

### What a 200 answers

| Field                        | What it is                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| id (required)                | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| name (required)              | text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| rules (required)             | an object. A rule tree, version 1: { "version": 1, "match": "all" \| "any", "rules": \[ … \] }, where each rule is a group (match and rules again) or a leaf with a kind and an op. The kinds: list (on, not\_on, with list\_id: a tpc\_… id), tag (has, not\_has, with tag\_id), field (with field\_id: is and is\_not with a value of the field's type; less\_than and greater\_than with a number; before and after with a date as YYYY-MM-DD; in\_last with days; is\_set and is\_not\_set), clicked, not\_clicked, and opened (in\_last with days), joined (before, after, or in\_last), and location (is, is\_not, with a country as ISO 3166-1 alpha-2 and an optional region as ISO 3166-2). location reads the place a contact states first: the country set on the contact, then the person's own (a form's country field, a postal code). Only a contact with neither is placed by the country of a request they made, which is an estimate: the connection's country, not the person's. A stated country with no region takes the region from the next of those that names the same country, a request's included, and never takes another country. A language or a time zone places nobody. A negative matches a contact with no value. opened is an estimate: a mail app can open a message by itself, and a click counts as an open, so it has no negative. At most 30 leaves in a tree, 15 rules in a group, 3 levels, and in\_last 3650 days; days count from midnight in the account's time zone. Anything else is 422 segment\_invalid at the rule's JSON pointer. |
| applied\_version (required)  | a whole number. Numbered from 1 with each change of the applied rules.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| applied\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| draft (required)             | an object, or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| revision (required)          | a whole number. Moves with every change of the rules, the draft, or the name: the ETag.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| used\_by (required)          | an object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| used\_by.sends (required)    | a whole number. How many scheduled emails (not yet gone) name the segment, as their audience or as an exclusion. While any does, the segment can’t be archived.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| used\_by.unpinned (required) | a whole number. How many of those still resolve the applied rules when they go, rather than a version they’ve already read. While any does, a change to the rules goes to draft and waits for an apply (issue 1581).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| archived\_at (required)      | a date and time (RFC 3339), or null.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| created\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| updated\_at (required)       | a date and time (RFC 3339).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

## Count rules before saving them

`POST /v1/segments/count`

How many contacts the rules match, for a builder as rules are written. Counted now, over the eligible audience: active contacts with an address to send to, not on the account’s suppression list as far as the list can be matched (about: the send itself decides, so a count may include someone a send would leave out), and under a list only those whose consent to it is in force. Bounded: once it’s the count’s turn, its work shares 5 s, or what’s left of the request’s time if that’s less; past that it’s 422 `segment_too_costly`, and a request with no time left for a count is 503 `segments_unavailable` before it starts. One count at a time per account: a count waits at most 2 s for its turn, outside those 5 s, then it’s 503 `segments_unavailable`. A count answers from the account’s contacts, so a key needs `contacts:read` as well as `segments:read`. The rules are refused as 422 `segment_invalid`, with `errors[0].pointer` at the first rule refused and `errors[0].code` why, never the value: a shape the tree doesn’t take, a list, tag, or field the account doesn’t have, an operator the field’s type doesn’t take, a bound passed, or an engagement rule where engagement isn’t recorded yet.

Its API key needs `segments:read` and `contacts:read`.

### Request body

A `application/json` body.

| Field                | What it is                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| rules (required)     | an object. A rule tree, version 1: { "version": 1, "match": "all" \| "any", "rules": \[ … \] }, where each rule is a group (match and rules again) or a leaf with a kind and an op. The kinds: list (on, not\_on, with list\_id: a tpc\_… id), tag (has, not\_has, with tag\_id), field (with field\_id: is and is\_not with a value of the field's type; less\_than and greater\_than with a number; before and after with a date as YYYY-MM-DD; in\_last with days; is\_set and is\_not\_set), clicked, not\_clicked, and opened (in\_last with days), joined (before, after, or in\_last), and location (is, is\_not, with a country as ISO 3166-1 alpha-2 and an optional region as ISO 3166-2). location reads the place a contact states first: the country set on the contact, then the person's own (a form's country field, a postal code). Only a contact with neither is placed by the country of a request they made, which is an estimate: the connection's country, not the person's. A stated country with no region takes the region from the next of those that names the same country, a request's included, and never takes another country. A language or a time zone places nobody. A negative matches a contact with no value. opened is an estimate: a mail app can open a message by itself, and a click counts as an open, so it has no negative. At most 30 leaves in a tree, 15 rules in a group, 3 levels, and in\_last 3650 days; days count from midnight in the account's time zone. Anything else is 422 segment\_invalid at the rule's JSON pointer. |
| list\_id             | text. Count who would get an email to this list: consent to it in force, and nobody else.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| exclude\_segment\_id | text. Leave out everyone this segment’s applied rules match, as an email’s exclusion does.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |

### Answers

* **200**: The count
* **400**:  
  * Malformed request ([malformed\_request](https://flyanote.com/docs/errors#malformed-request))
* **401**:  
  * API key missing ([api\_key\_missing](https://flyanote.com/docs/errors#api-key-missing))
  * Invalid API key ([api\_key\_invalid](https://flyanote.com/docs/errors#api-key-invalid))
  * API key revoked ([api\_key\_revoked](https://flyanote.com/docs/errors#api-key-revoked))
  * API key expired ([api\_key\_expired](https://flyanote.com/docs/errors#api-key-expired))
* **403**:  
  * Account closed ([account\_closed](https://flyanote.com/docs/errors#account-closed))
  * Missing scope ([api\_key\_scope\_missing](https://flyanote.com/docs/errors#api-key-scope-missing))
  * Test keys can't do this ([test\_key\_not\_allowed](https://flyanote.com/docs/errors#test-key-not-allowed))
* **404**:  
  * Topic not found ([topic\_not\_found](https://flyanote.com/docs/errors#topic-not-found))
  * Segment not found ([segment\_not\_found](https://flyanote.com/docs/errors#segment-not-found))
* **413**:  
  * Request body too large ([payload\_too\_large](https://flyanote.com/docs/errors#payload-too-large))
* **415**:  
  * Unsupported media type ([unsupported\_media\_type](https://flyanote.com/docs/errors#unsupported-media-type))
* **422**:  
  * Segment rules invalid ([segment\_invalid](https://flyanote.com/docs/errors#segment-invalid))
  * Segment too costly to count ([segment\_too\_costly](https://flyanote.com/docs/errors#segment-too-costly))
  * Validation failed ([validation\_failed](https://flyanote.com/docs/errors#validation-failed))
* **429**:  
  * Too many requests ([rate\_limited](https://flyanote.com/docs/errors#rate-limited))
* **500**:  
  * Internal error ([internal](https://flyanote.com/docs/errors#internal))
* **503**:  
  * API keys unavailable ([api\_keys\_unavailable](https://flyanote.com/docs/errors#api-keys-unavailable))
  * Segments unavailable ([segments\_unavailable](https://flyanote.com/docs/errors#segments-unavailable))

### What a 200 answers

| Field                            | What it is                                                                                                                                                                                                                                    |
| -------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| matching (required)              | a whole number. How many the rules match now, less any excluded.                                                                                                                                                                              |
| basis (required)                 | one of contacts or recipients. recipients under a list (list\_id: who would get an email to it, their consent in force), contacts over the whole audience. Either way, a contact who isn’t active or has no address to send to isn’t counted. |
| untracked (required)             | a whole number, or null. How many of the eligible contacts an engagement rule can’t see (not tracked in full), who read as “didn’t click” or “didn’t open”; null when no rule reads engagement.                                               |
| first (required)                 | a list, each an object. The first five matches, newest first: names only, never an address. Every caller of a count may read contacts (a member, or a key with contacts:read, which a count needs).                                           |
| first\[\].id (required)          | text.                                                                                                                                                                                                                                         |
| first\[\].name (required)        | text, or null.                                                                                                                                                                                                                                |
| first\[\].first\_name (required) | text, or null.                                                                                                                                                                                                                                |
| first\[\].last\_name (required)  | text, or null.                                                                                                                                                                                                                                |
| definitions\_version (required)  | a whole number. Which engagement definitions the count read.                                                                                                                                                                                  |
| counted\_at (required)           | a date and time (RFC 3339).                                                                                                                                                                                                                   |

## A segment's counts

`GET /v1/segments/{segment_id}/counts`

How many contacts the segment's applied rules match, and its draft's, if it has one. Counted now, over the eligible audience: active contacts with an address to send to, not on the account’s suppression list as far as the list can be matched (about: the send itself decides, so a count may include someone a send would leave out), and under a list only those whose consent to it is in force. Bounded: once it’s the count’s turn, its work shares 5 s, or what’s left of the request’s time if that’s less; past that it’s 422 `segment_too_costly`, and a request with no time left for a count is 503 `segments_unavailable` before it starts. One count at a time per account: a count waits at most 2 s for its turn, outside those 5 s, then it’s 503 `segments_unavailable`. A count answers from the account’s contacts, so a key needs `contacts:read` as well as `segments:read`. A stored tree the account can no longer run (a tag or field deleted since, or an engagement rule where engagement isn't recorded) is 422 `segment_invalid`, with `errors[0].pointer` into the segment as `GET` shows it (`/rules/…` or `/draft/…`, with `in: path`).

Its API key needs `segments:read` and `contacts:read`.

### Parameters

| Parameter              | What it is                                                                                               |
| ---------------------- | -------------------------------------------------------------------------------------------------------- |
| segment\_id (required) | text, up to 64 characters, in the path. The segment’s id (seg\_…).                                       |
| list\_id               | text, in the query. Count who would get an email to this list: consent to it in force, and nobody else.  |
| exclude\_segment\_id   | text, in the query. Leave out everyone this segment’s applied rules match, as an email’s exclusion does. |

### Answers

* **200**: The counts
* **401**:  
  * API key missing ([api\_key\_missing](https://flyanote.com/docs/errors#api-key-missing))
  * Invalid API key ([api\_key\_invalid](https://flyanote.com/docs/errors#api-key-invalid))
  * API key revoked ([api\_key\_revoked](https://flyanote.com/docs/errors#api-key-revoked))
  * API key expired ([api\_key\_expired](https://flyanote.com/docs/errors#api-key-expired))
* **403**:  
  * Account closed ([account\_closed](https://flyanote.com/docs/errors#account-closed))
  * Missing scope ([api\_key\_scope\_missing](https://flyanote.com/docs/errors#api-key-scope-missing))
  * Test keys can't do this ([test\_key\_not\_allowed](https://flyanote.com/docs/errors#test-key-not-allowed))
* **404**:  
  * Segment not found ([segment\_not\_found](https://flyanote.com/docs/errors#segment-not-found))
  * Topic not found ([topic\_not\_found](https://flyanote.com/docs/errors#topic-not-found))
* **422**:  
  * Segment rules invalid ([segment\_invalid](https://flyanote.com/docs/errors#segment-invalid))
  * Segment too costly to count ([segment\_too\_costly](https://flyanote.com/docs/errors#segment-too-costly))
  * Validation failed ([validation\_failed](https://flyanote.com/docs/errors#validation-failed))
* **429**:  
  * Too many requests ([rate\_limited](https://flyanote.com/docs/errors#rate-limited))
* **500**:  
  * Internal error ([internal](https://flyanote.com/docs/errors#internal))
* **503**:  
  * API keys unavailable ([api\_keys\_unavailable](https://flyanote.com/docs/errors#api-keys-unavailable))
  * Segments unavailable ([segments\_unavailable](https://flyanote.com/docs/errors#segments-unavailable))

### What a 200 answers

| Field                                    | What it is                                                                                                                                                                                                                                    |
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| revision (required)                      | a whole number. The segment’s revision these counts read: its ETag. A step that acts on what the counts showed (issue 1581’s apply) names it, so a change since then is refused rather than applied unseen.                                   |
| applied\_version (required)              | a whole number. The version applied read.                                                                                                                                                                                                     |
| applied (required)                       | an object.                                                                                                                                                                                                                                    |
| applied.matching (required)              | a whole number. How many the rules match now, less any excluded.                                                                                                                                                                              |
| applied.basis (required)                 | one of contacts or recipients. recipients under a list (list\_id: who would get an email to it, their consent in force), contacts over the whole audience. Either way, a contact who isn’t active or has no address to send to isn’t counted. |
| applied.untracked (required)             | a whole number, or null. How many of the eligible contacts an engagement rule can’t see (not tracked in full), who read as “didn’t click” or “didn’t open”; null when no rule reads engagement.                                               |
| applied.first (required)                 | a list, each an object. The first five matches, newest first: names only, never an address. Every caller of a count may read contacts (a member, or a key with contacts:read, which a count needs).                                           |
| applied.first\[\].id (required)          | text.                                                                                                                                                                                                                                         |
| applied.first\[\].name (required)        | text, or null.                                                                                                                                                                                                                                |
| applied.first\[\].first\_name (required) | text, or null.                                                                                                                                                                                                                                |
| applied.first\[\].last\_name (required)  | text, or null.                                                                                                                                                                                                                                |
| applied.definitions\_version (required)  | a whole number. Which engagement definitions the count read.                                                                                                                                                                                  |
| applied.counted\_at (required)           | a date and time (RFC 3339).                                                                                                                                                                                                                   |
| draft (required)                         | an object, or null. The draft’s count; null while the segment has no draft.                                                                                                                                                                   |
| draft.matching (required)                | a whole number. How many the rules match now, less any excluded.                                                                                                                                                                              |
| draft.basis (required)                   | one of contacts or recipients. recipients under a list (list\_id: who would get an email to it, their consent in force), contacts over the whole audience. Either way, a contact who isn’t active or has no address to send to isn’t counted. |
| draft.untracked (required)               | a whole number, or null. How many of the eligible contacts an engagement rule can’t see (not tracked in full), who read as “didn’t click” or “didn’t open”; null when no rule reads engagement.                                               |
| draft.first (required)                   | a list, each an object. The first five matches, newest first: names only, never an address. Every caller of a count may read contacts (a member, or a key with contacts:read, which a count needs).                                           |
| draft.first\[\].id (required)            | text.                                                                                                                                                                                                                                         |
| draft.first\[\].name (required)          | text, or null.                                                                                                                                                                                                                                |
| draft.first\[\].first\_name (required)   | text, or null.                                                                                                                                                                                                                                |
| draft.first\[\].last\_name (required)    | text, or null.                                                                                                                                                                                                                                |
| draft.definitions\_version (required)    | a whole number. Which engagement definitions the count read.                                                                                                                                                                                  |
| draft.counted\_at (required)             | a date and time (RFC 3339).                                                                                                                                                                                                                   |
