Creates an order risk for an order

Creates an order risk for an order

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
 * Creates an order risk for an order
7
 * Creates an order risk for an order
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  order_id: string,
13
  body: {
14
    risk?: {
15
      cause_cancel?: boolean;
16
      display?: boolean;
17
      message?: string;
18
      recommendation?: string;
19
      score?: number;
20
      source?: string;
21
      [k: string]: unknown;
22
    };
23
    [k: string]: unknown;
24
  }
25
) {
26
  const url = new URL(
27
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/${order_id}/risks.json`
28
  );
29

30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      "Content-Type": "application/json",
34
      "X-Shopify-Access-Token": auth.token,
35
    },
36
    body: JSON.stringify(body),
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