0

Get Top or Trending Domains

by
Published Nov 16, 2023

Get top or trending domains based on their rank. Popular domains are domains of broad appeal based on how people use the Internet. Trending domains are domains that are generating a surge in interest. For more information on top domains, see https://blog.cloudflare.com/radar-domain-rankings/.

Script cloudflare Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get Top or Trending Domains
8
 * Get top or trending domains based on their rank. Popular domains are domains of broad appeal based on how people use the Internet. Trending domains are domains that are generating a surge in interest. For more information on top domains, see https://blog.cloudflare.com/radar-domain-rankings/.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  limit: string | undefined,
13
  name: string | undefined,
14
  location: string | undefined,
15
  date: string | undefined,
16
  rankingType: "POPULAR" | "TRENDING_RISE" | "TRENDING_STEADY" | undefined,
17
  format: "JSON" | "CSV" | undefined
18
) {
19
  const url = new URL(`https://api.cloudflare.com/client/v4/radar/ranking/top`);
20
  for (const [k, v] of [
21
    ["limit", limit],
22
    ["name", name],
23
    ["location", location],
24
    ["date", date],
25
    ["rankingType", rankingType],
26
    ["format", format],
27
  ]) {
28
    if (v !== undefined && v !== "") {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      "X-AUTH-EMAIL": auth.email,
36
      "X-AUTH-KEY": auth.key,
37
      Authorization: "Bearer " + auth.token,
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47