0

Cancel Fulfillment

by
Published Nov 8, 2023

Cancel a fulfillment so its items can be fulfilled again.

Script shopify Verified

The script

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

3
/**
4
 * Cancel Fulfillment
5
 * Cancel a fulfillment. Its items go back to being unfulfilled, on new or reopened fulfillment orders.
6
 */
7
export async function main(auth: RT.Shopify, fulfillment_id: string) {
8
  const response = await fetch(
9
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
10
    {
11
      method: "POST",
12
      headers: {
13
        "X-Shopify-Access-Token": auth.token,
14
        "Content-Type": "application/json",
15
        Accept: "application/json",
16
      },
17
      body: JSON.stringify({
18
        query: `mutation ($id: ID!) { fulfillmentCancel(id: $id) { fulfillment { id name status displayStatus updatedAt } userErrors { field message } } }`,
19
        variables: {
20
          id: fulfillment_id.startsWith("gid://")
21
            ? fulfillment_id
22
            : `gid://shopify/Fulfillment/${fulfillment_id}`,
23
        },
24
      }),
25
    }
26
  )
27

28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${await response.text()}`)
30
  }
31

32
  const { data, errors } = await response.json()
33
  if (errors) {
34
    throw new Error(JSON.stringify(errors))
35
  }
36

37
  if (data.fulfillmentCancel.userErrors.length) {
38
    throw new Error(JSON.stringify(data.fulfillmentCancel.userErrors))
39
  }
40

41
  return data.fulfillmentCancel
42
}
43

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
     * Cancels a fulfillment
    7
     * Cancels a fulfillment.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      fulfillment_id: string,
    13
      body: { [k: string]: unknown }
    14
    ) {
    15
      const url = new URL(
    16
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/fulfillments/${fulfillment_id}/cancel.json`
    17
      );
    18
    
    
    19
      const response = await fetch(url, {
    20
        method: "POST",
    21
        headers: {
    22
          "Content-Type": "application/json",
    23
          "X-Shopify-Access-Token": auth.token,
    24
        },
    25
        body: JSON.stringify(body),
    26
      });
    27
      if (!response.ok) {
    28
        const text = await response.text();
    29
        throw new Error(`${response.status} ${text}`);
    30
      }
    31
      return await response.json();
    32
    }
    33