1 | |
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 |
|