Cancels an order

Caution For multi-currency orders, the currency property is required whenever the amount property is provided. For more information, see Migrating to support multiple currencies. Cancels an order. Orders that have a fulfillment object can't be canceled.

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
 * Cancels an order
7
 *  Caution For multi-currency orders, the currency property is required whenever the amount property is provided. For more information, see Migrating to support multiple currencies.  Cancels an order. Orders that have a fulfillment object can't be canceled.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  order_id: string,
13
  body: {
14
    amount?: string;
15
    currency?: string;
16
    note?: string;
17
    [k: string]: unknown;
18
  }
19
) {
20
  const url = new URL(
21
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/${order_id}/cancel.json`
22
  );
23

24
  const response = await fetch(url, {
25
    method: "POST",
26
    headers: {
27
      "Content-Type": "application/json",
28
      "X-Shopify-Access-Token": auth.token,
29
    },
30
    body: JSON.stringify(body),
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38