Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Auto Update Inventory Program

Solved

Auto Update Inventory Program

arieswen415
Shopify Partner
21 5 3

As the title mentions, I am writing a program to update inventory automatically every weekday. The program is working with no problem; however, it is very inefficient. I would like to ask my fellow developers in Shopify community, how can I make my program more efficient?

 

The program reads through my Excel file that contains 2 columns (SKU and quantity), sends HTTP request to Shopify and finds each item's Inventory ID, and sends another request to update inventory with the following query:

 

String query = "{\"location_id\": \"123123123\", \"inventory_item_id\": "+ id +", \"available\": "+ qty +"}";

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://store-name.myshopify.com/admin/api/2023-07/inventory_levels/set.json")) .header("Content-Type", "application/json") .header("X-Shopify-Access-Token", api_key) .POST(BodyPublishers.ofString(query)) .build();

The program sends two requests, leading to an extended update duration. When I have 500 products, it takes approximately 10 mins. Is it possible to update all SKUs in one request?

 

Any input is appreciated! Thank you for your time reading the question.

 

Accepted Solution (1)
arieswen415
Shopify Partner
21 5 3

This is an accepted solution.

Hello @SBD_ 

 

Thank you very much for your reply. The documentation is a very good starting point for me to make the necessary modifications to the program, and the idea of store inventory ID in a spreadsheet significantly enhances its efficiency.

 

The query I ended up using was

mutation {
  inventorySetOnHandQuantities(
    input: {
      reason: "correction",
      setQuantities: [
        {
          inventoryItemId: "gid://shopify/InventoryItem/123123123123",
          locationId: "gid://shopify/Location/111222333",
          quantity: 23
        },
        {
          inventoryItemId: "gid://shopify/InventoryItem/456456456456",
          locationId: "gid://shopify/Location/111222333",
          quantity: 13
        },
        ...
      ]
    }
  ) {
    userErrors {
     field
     message
    }
  }
}

 

The Java code ( I didn't test the maximum but I can update 200 products in one query )

String query = "mutation { inventorySetOnHandQuantities(input: { reason: \"correction\", setQuantities: [" + quantities + "]}) { userErrors { field message }}}";

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
		.uri(URI.create("https://store-name.myshopify.com/admin/api/2023-04/graphql.json"))
		.header("Content-Type", "application/graphql")
		.header("X-Shopify-Access-Token", api_key)
		.POST(BodyPublishers.ofString(query))
		.build();

 

It takes less than a second to update 500+ products now!

View solution in original post

Replies 2 (2)

SBD_
Shopify Staff
1831 273 423

Hey @arieswen415 

 

Perhaps Bulk Operations could help: https://shopify.dev/docs/api/usage/bulk-operations/imports

 

Although you'll need to store the variant ID in your spreadsheet to eliminate looking up by SKU.

Scott | Developer Advocate @ Shopify 

arieswen415
Shopify Partner
21 5 3

This is an accepted solution.

Hello @SBD_ 

 

Thank you very much for your reply. The documentation is a very good starting point for me to make the necessary modifications to the program, and the idea of store inventory ID in a spreadsheet significantly enhances its efficiency.

 

The query I ended up using was

mutation {
  inventorySetOnHandQuantities(
    input: {
      reason: "correction",
      setQuantities: [
        {
          inventoryItemId: "gid://shopify/InventoryItem/123123123123",
          locationId: "gid://shopify/Location/111222333",
          quantity: 23
        },
        {
          inventoryItemId: "gid://shopify/InventoryItem/456456456456",
          locationId: "gid://shopify/Location/111222333",
          quantity: 13
        },
        ...
      ]
    }
  ) {
    userErrors {
     field
     message
    }
  }
}

 

The Java code ( I didn't test the maximum but I can update 200 products in one query )

String query = "mutation { inventorySetOnHandQuantities(input: { reason: \"correction\", setQuantities: [" + quantities + "]}) { userErrors { field message }}}";

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
		.uri(URI.create("https://store-name.myshopify.com/admin/api/2023-04/graphql.json"))
		.header("Content-Type", "application/graphql")
		.header("X-Shopify-Access-Token", api_key)
		.POST(BodyPublishers.ofString(query))
		.build();

 

It takes less than a second to update 500+ products now!