Get Segment ( mailchimp)
One script reply has been approved by the moderators Verified

Created by adam186 114 days ago Viewed 55 times 0 Points

Get information about a specific segment.

No comments yet

Login to be able to comment
Points: 0
deno
One script reply has been approved by the moderators
Ap­pro­ved
import { Resource } from 'https://deno.land/x/windmill@v1.70.1/mod.ts'

/**
 * @param fields *(optional)* A list of fields to return in the response.
 * Reference parameters of sub-objects with dot notation.
 * 
 * @param exclude_fields *(optional)* A list of fields to exclude from the response.
 * Reference parameters of sub-objects with dot notation. If both `fields` and `exclude_fields`
 * are present, then only `exclude_fields` will be used.
 */
export async function main(
  auth: Resource<'mailchimp'>,
  list_id: string,
  segment_id: string,
  fields?: string[],
  exclude_fields?: string[],
  include_cleaned?: boolean,
  include_transactional?: boolean,
  include_unsubscribed?: boolean,
) {
  const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments/${segment_id}`)
  const params = {
    fields,
    exclude_fields,
    include_cleaned,
    include_transactional,
    include_unsubscribed
  }
  for(const key in params) {
    const value = params[<keyof typeof params>key]
    if(value) {
      url.searchParams.append(key, Array.isArray(value) ? value.join(',') : '' + value)
    }
  }

  const response = await fetch(url, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${auth.api_key}`
    }
  })
  
  if(!response.ok) {
    throw Error(await response.text())
  }
  return await response.json()
}

Submitted by adam186 114 days ago

Edited 23 days ago

No comments yet

Login to be able to comment