0

Retrieve Survey Responses

by
Published Apr 8, 2025
Script buttondown Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Buttondown = {
3
  token: string;
4
};
5
/**
6
 * Retrieve Survey Responses
7
 *
8
 */
9
export async function main(
10
  auth: Buttondown,
11
  email: string | undefined,
12
  subscriber: string | undefined,
13
  survey: string | undefined,
14
  creation_date__start: string | undefined,
15
  creation_date__end: string | undefined,
16
  expand: string | undefined,
17
) {
18
  const url = new URL(`https://api.buttondown.com/v1/survey_responses`);
19
  for (const [k, v] of [
20
    ["email", email],
21
    ["subscriber", subscriber],
22
    ["survey", survey],
23
    ["creation_date__start", creation_date__start],
24
    ["creation_date__end", creation_date__end],
25
    ["expand", expand],
26
  ]) {
27
    if (v !== undefined && v !== "" && k !== undefined) {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Token " + auth.token,
35
    },
36
    body: undefined,
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44