0

Get logs RayIDs

by
Published Nov 16, 2023

The `/rayids` api route allows lookups by specific rayid. The rayids route will return zero, one, or more records (ray ids are not unique).

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 logs RayIDs
8
 * The `/rayids` api route allows lookups by specific rayid. The rayids route will return zero, one, or more records (ray ids are not unique).
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  ray_identifier: string,
13
  zone_identifier: string,
14
  timestamps: "unix" | "unixnano" | "rfc3339" | undefined,
15
  fields: string | undefined
16
) {
17
  const url = new URL(
18
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/logs/rayids/${ray_identifier}`
19
  );
20
  for (const [k, v] of [
21
    ["timestamps", timestamps],
22
    ["fields", fields],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      "X-AUTH-EMAIL": auth.email,
32
      "X-AUTH-KEY": auth.key,
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43