0

Retrieve Sorted Bitlinks for Group

by
Published Apr 8, 2025

Returns a list of Bitlinks sorted by group. The list of custom bitlinks has newest entries first.

Script bitly Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Bitly = {
3
  token: string;
4
};
5
/**
6
 * Retrieve Sorted Bitlinks for Group
7
 * Returns a list of Bitlinks sorted by group. The list of custom bitlinks has newest entries first.
8
 */
9
export async function main(
10
  auth: Bitly,
11
  group_guid: string,
12
  sort: "clicks",
13
  unit: "minute" | "hour" | "day" | "week" | "month" | undefined,
14
  units: string | undefined,
15
  unit_reference: string | undefined,
16
  size: string | undefined,
17
) {
18
  const url = new URL(
19
    `https://api-ssl.bitly.com/v4/groups/${group_guid}/bitlinks/${sort}`,
20
  );
21
  for (const [k, v] of [
22
    ["unit", unit],
23
    ["units", units],
24
    ["unit_reference", unit_reference],
25
    ["size", size],
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: "Bearer " + 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