0

Connects an inventory item to a location

by
Published Nov 8, 2023

Connects an inventory item to a location by creating an inventory level at that location. When connecting inventory items to locations, it's important to understand the rules around fulfillment service locations.

Script shopify Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 416 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Connects an inventory item to a location
7
 * Connects an inventory item to a location by creating an inventory level at that location. When connecting inventory items to locations, it's important to understand the rules around fulfillment service locations.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  body: {
13
    inventory_item_id?: number;
14
    location_id?: number;
15
    [k: string]: unknown;
16
  }
17
) {
18
  const url = new URL(
19
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/inventory_levels/connect.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