0

Submit Cancellation Request

by
Published Nov 8, 2023

Ask a fulfillment service to cancel a fulfillment order.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Submit Cancellation Request
5
 * Ask the fulfillment service assigned to a fulfillment order to cancel it. Only valid for fulfillment orders assigned to an API fulfillment service whose request was accepted; the service then accepts or rejects it, after which Cancel Fulfillment Order closes it.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  fulfillment_order_id: string,
10
  message: string | undefined
11
) {
12
  const response = await fetch(
13
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
14
    {
15
      method: "POST",
16
      headers: {
17
        "X-Shopify-Access-Token": auth.token,
18
        "Content-Type": "application/json",
19
        Accept: "application/json",
20
      },
21
      body: JSON.stringify({
22
        query: `mutation ($id: ID!, $message: String) { fulfillmentOrderSubmitCancellationRequest(id: $id, message: $message) { fulfillmentOrder { id status requestStatus } userErrors { field message } } }`,
23
        variables: {
24
          id: fulfillment_order_id.startsWith("gid://")
25
            ? fulfillment_order_id
26
            : `gid://shopify/FulfillmentOrder/${fulfillment_order_id}`,
27
          message: message || undefined,
28
        },
29
      }),
30
    }
31
  )
32

33
  if (!response.ok) {
34
    throw new Error(`${response.status} ${await response.text()}`)
35
  }
36

37
  const { data, errors } = await response.json()
38
  if (errors) {
39
    throw new Error(JSON.stringify(errors))
40
  }
41

42
  if (data.fulfillmentOrderSubmitCancellationRequest.userErrors.length) {
43
    throw new Error(
44
      JSON.stringify(data.fulfillmentOrderSubmitCancellationRequest.userErrors)
45
    )
46
  }
47

48
  return data.fulfillmentOrderSubmitCancellationRequest
49
}
50

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Sends a cancellation request
    7
     * Sends a cancellation request to the fulfillment service of a fulfillment order.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      fulfillment_order_id: string,
    13
      body: {
    14
        cancellation_request?: { message?: string; [k: string]: unknown };
    15
        [k: string]: unknown;
    16
      }
    17
    ) {
    18
      const url = new URL(
    19
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/fulfillment_orders/${fulfillment_order_id}/cancellation_request.json`
    20
      );
    21
    
    
    22
      const response = await fetch(url, {
    23
        method: "POST",
    24
        headers: {
    25
          "Content-Type": "application/json",
    26
          "X-Shopify-Access-Token": auth.token,
    27
        },
    28
        body: JSON.stringify(body),
    29
      });
    30
      if (!response.ok) {
    31
        const text = await response.text();
    32
        throw new Error(`${response.status} ${text}`);
    33
      }
    34
      return await response.json();
    35
    }
    36