0

Search Segments

by
Published Nov 29, 2022

Get information about segments of a specific list.

Script mailchimp Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
/**
4
 * @param fields *(optional)* A list of fields to return in the response.
5
 * Reference parameters of sub-objects with dot notation.
6
 *
7
 * @param exclude_fields *(optional)* A list of fields to exclude from the response.
8
 * Reference parameters of sub-objects with dot notation. If both `fields` and `exclude_fields`
9
 * are present, then only `exclude_fields` will be used.
10
 *
11
 * @param since_created_at *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
12
 * @param before_created_at *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
13
 * @param since_updated_at *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
14
 * @param before_updated_at *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
15
 */
16
type Mailchimp = {
17
  api_key: string;
18
  server: string;
19
};
20
export async function main(
21
  auth: Mailchimp,
22
  list_id: string,
23
  fields?: string[],
24
  exclude_fields?: string[],
25
  count?: number,
26
  offset?: number,
27
  type?: string,
28
  since_created_at?: string,
29
  before_created_at?: string,
30
  since_updated_at?: string,
31
  before_updated_at?: string,
32
  include_cleaned?: boolean,
33
  include_transactional?: boolean,
34
  include_unsubscribed?: boolean,
35
) {
36
  const url = new URL(
37
    `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`,
38
  );
39
  const params = {
40
    fields,
41
    exclude_fields,
42
    count,
43
    offset,
44
    type,
45
    since_created_at,
46
    before_created_at,
47
    since_updated_at,
48
    before_updated_at,
49
    include_cleaned,
50
    include_transactional,
51
    include_unsubscribed,
52
  };
53
  for (const key in params) {
54
    const value = params[<keyof typeof params>key];
55
    if (value) {
56
      url.searchParams.append(
57
        key,
58
        Array.isArray(value) ? value.join(",") : "" + value,
59
      );
60
    }
61
  }
62

63
  const response = await fetch(url, {
64
    method: "GET",
65
    headers: {
66
      Authorization: `Bearer ${auth.api_key}`,
67
    },
68
  });
69

70
  if (!response.ok) {
71
    throw Error(await response.text());
72
  }
73
  return await response.json();
74
}
75

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    /**
    2
     * @param fields *(optional)* A list of fields to return in the response.
    3
     * Reference parameters of sub-objects with dot notation.
    4
     *
    5
     * @param exclude_fields *(optional)* A list of fields to exclude from the response.
    6
     * Reference parameters of sub-objects with dot notation. If both `fields` and `exclude_fields`
    7
     * are present, then only `exclude_fields` will be used.
    8
     *
    9
     * @param since_created_at *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
    10
     * @param before_created_at *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
    11
     * @param since_updated_at *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
    12
     * @param before_updated_at *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
    13
     */
    14
    type Mailchimp = {
    15
      api_key: string;
    16
      server: string;
    17
    };
    18
    export async function main(
    19
      auth: Mailchimp,
    20
      list_id: string,
    21
      fields?: string[],
    22
      exclude_fields?: string[],
    23
      count?: number,
    24
      offset?: number,
    25
      type?: string,
    26
      since_created_at?: string,
    27
      before_created_at?: string,
    28
      since_updated_at?: string,
    29
      before_updated_at?: string,
    30
      include_cleaned?: boolean,
    31
      include_transactional?: boolean,
    32
      include_unsubscribed?: boolean,
    33
    ) {
    34
      const url = new URL(
    35
        `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`,
    36
      );
    37
      const params = {
    38
        fields,
    39
        exclude_fields,
    40
        count,
    41
        offset,
    42
        type,
    43
        since_created_at,
    44
        before_created_at,
    45
        since_updated_at,
    46
        before_updated_at,
    47
        include_cleaned,
    48
        include_transactional,
    49
        include_unsubscribed,
    50
      };
    51
      for (const key in params) {
    52
        const value = params[<keyof typeof params>key];
    53
        if (value) {
    54
          url.searchParams.append(
    55
            key,
    56
            Array.isArray(value) ? value.join(",") : "" + value,
    57
          );
    58
        }
    59
      }
    60
    
    
    61
      const response = await fetch(url, {
    62
        method: "GET",
    63
        headers: {
    64
          Authorization: `Bearer ${auth.api_key}`,
    65
        },
    66
      });
    67
    
    
    68
      if (!response.ok) {
    69
        throw Error(await response.text());
    70
      }
    71
      return await response.json();
    72
    }
    73