0

Update Fulfillment Tracking Info

by
Published Nov 8, 2023

Update the tracking number, URL and carrier of a fulfillment.

Script shopify Verified

The script

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

3
/**
4
 * Update Fulfillment Tracking Info
5
 * Set the tracking number, URL and carrier of a fulfillment. Shopify replaces the tracking info with what you send, so pass every value you want to keep. notify_customer emails the customer the updated tracking.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  fulfillment_id: string,
10
  tracking_number: string | undefined,
11
  tracking_url: string | undefined,
12
  tracking_company: string | undefined,
13
  notify_customer: boolean | undefined
14
) {
15
  if (!tracking_number && !tracking_url && !tracking_company) {
16
    throw new Error(
17
      "Pass at least one of tracking_number, tracking_url, tracking_company"
18
    )
19
  }
20

21
  const response = await fetch(
22
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
23
    {
24
      method: "POST",
25
      headers: {
26
        "X-Shopify-Access-Token": auth.token,
27
        "Content-Type": "application/json",
28
        Accept: "application/json",
29
      },
30
      body: JSON.stringify({
31
        query: `mutation ($fulfillmentId: ID!, $trackingInfoInput: FulfillmentTrackingInput!, $notifyCustomer: Boolean) { fulfillmentTrackingInfoUpdate(fulfillmentId: $fulfillmentId, trackingInfoInput: $trackingInfoInput, notifyCustomer: $notifyCustomer) { fulfillment { id name status displayStatus updatedAt trackingInfo { company number url } } userErrors { field message } } }`,
32
        variables: {
33
          fulfillmentId: fulfillment_id.startsWith("gid://")
34
            ? fulfillment_id
35
            : `gid://shopify/Fulfillment/${fulfillment_id}`,
36
          trackingInfoInput: {
37
            number: tracking_number || undefined,
38
            url: tracking_url || undefined,
39
            company: tracking_company || undefined,
40
          },
41
          notifyCustomer: notify_customer || undefined,
42
        },
43
      }),
44
    }
45
  )
46

47
  if (!response.ok) {
48
    throw new Error(`${response.status} ${await response.text()}`)
49
  }
50

51
  const { data, errors } = await response.json()
52
  if (errors) {
53
    throw new Error(JSON.stringify(errors))
54
  }
55

56
  if (data.fulfillmentTrackingInfoUpdate.userErrors.length) {
57
    throw new Error(
58
      JSON.stringify(data.fulfillmentTrackingInfoUpdate.userErrors)
59
    )
60
  }
61

62
  return data.fulfillmentTrackingInfoUpdate
63
}
64

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
     * Updates the tracking information for a fulfillment
    7
     * Updates the tracking information for a fulfillment.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      fulfillment_id: string,
    13
      body: {
    14
        fulfillment?: {
    15
          notify_customer?: boolean;
    16
          tracking_info?: {
    17
            company?: string;
    18
            number?: string;
    19
            url?: string;
    20
            [k: string]: unknown;
    21
          };
    22
          [k: string]: unknown;
    23
        };
    24
        [k: string]: unknown;
    25
      }
    26
    ) {
    27
      const url = new URL(
    28
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/fulfillments/${fulfillment_id}/update_tracking.json`
    29
      );
    30
    
    
    31
      const response = await fetch(url, {
    32
        method: "POST",
    33
        headers: {
    34
          "Content-Type": "application/json",
    35
          "X-Shopify-Access-Token": auth.token,
    36
        },
    37
        body: JSON.stringify(body),
    38
      });
    39
      if (!response.ok) {
    40
        const text = await response.text();
    41
        throw new Error(`${response.status} ${text}`);
    42
      }
    43
      return await response.json();
    44
    }
    45