Create a new Fulfillment

Create a fulfillment for the specified order and line items.

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
 * Create a new Fulfillment
7
 * Create a fulfillment for the specified order and line items.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  order_id: string,
13
  body: {
14
    fulfillment?: {
15
      line_items?: { id?: number; [k: string]: unknown }[];
16
      location_id?: number;
17
      tracking_company?: string;
18
      tracking_number?: string;
19
      [k: string]: unknown;
20
    };
21
    [k: string]: unknown;
22
  }
23
) {
24
  const url = new URL(
25
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/${order_id}/fulfillments.json`
26
  );
27

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