0

Count users

by
Published Apr 8, 2025

Returns a total count of all users that match the given filtering criteria.

Script clerk Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Clerk = {
3
  apiKey: string;
4
};
5
/**
6
 * Count users
7
 * Returns a total count of all users that match the given filtering criteria.
8
 */
9
export async function main(
10
  auth: Clerk,
11
  email_address: string | undefined,
12
  phone_number: string | undefined,
13
  external_id: string | undefined,
14
  username: string | undefined,
15
  web3_wallet: string | undefined,
16
  user_id: string | undefined,
17
  query: string | undefined,
18
  email_address_query: string | undefined,
19
  phone_number_query: string | undefined,
20
  username_query: string | undefined,
21
) {
22
  const url = new URL(`https://api.clerk.com/v1/users/count`);
23
  for (const [k, v] of [
24
    ["email_address", email_address],
25
    ["phone_number", phone_number],
26
    ["external_id", external_id],
27
    ["username", username],
28
    ["web3_wallet", web3_wallet],
29
    ["user_id", user_id],
30
    ["query", query],
31
    ["email_address_query", email_address_query],
32
    ["phone_number_query", phone_number_query],
33
    ["username_query", username_query],
34
  ]) {
35
    if (v !== undefined && v !== "" && k !== undefined) {
36
      url.searchParams.append(k, v);
37
    }
38
  }
39
  const response = await fetch(url, {
40
    method: "GET",
41
    headers: {
42
      Authorization: "Bearer " + auth.apiKey,
43
    },
44
    body: undefined,
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.json();
51
}
52