Retrieves a count of checkouts

Retrieves a count of checkouts from the past 90 days

Script shopify Verified

by hugo697 ยท 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Retrieves a count of checkouts
7
 * Retrieves a count of checkouts from the past 90 days
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  since_id: string | undefined,
13
  created_at_min: string | undefined,
14
  created_at_max: string | undefined,
15
  updated_at_min: string | undefined,
16
  updated_at_max: string | undefined,
17
  status: string | undefined
18
) {
19
  const url = new URL(
20
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/checkouts/count.json`
21
  );
22
  for (const [k, v] of [
23
    ["since_id", since_id],
24
    ["created_at_min", created_at_min],
25
    ["created_at_max", created_at_max],
26
    ["updated_at_min", updated_at_min],
27
    ["updated_at_max", updated_at_max],
28
    ["status", status],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      "X-Shopify-Access-Token": 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