Cannot enable/disable inventory tracking with REST API

Solved

Cannot enable/disable inventory tracking with REST API

aevumkairos
Shopify Partner
4 1 2

Pretty new to this so apologies if this is an easy to solve problem, but I haven't had any luck searching around.  I'm attempting to toggle an item to "Track quantity" and can't seem to do so.  I've tried a direct PUT request as well as using the python Shopify API.  Here's my PUT request:

headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': private_app_password
}
response = requests.get(url, headers=headers)
 
inventoryItem is the inventory ID which is being worked out by other stuff before this.  Then I create a json format with the update info and send the request:
tracked_data = {
"inventory_item": {
"id": inventoryItem,
"tracked": True
}
}
update_response = requests.put(url, headers=headers, json=tracked_data)
The update response shows the updated information, and I receive code 200, but the changes aren't reflected on the product.  I appreciate any help you guys send my way.
Accepted Solution (1)
aevumkairos
Shopify Partner
4 1 2

This is an accepted solution.

After further experimentation, I figured it out!  I did some testing and confirmed my code works when run standalone, which confused me.  Turns out it's the python Shopify API that was causing this issue.  Since I am creating a session and didn't clear that session, my REST API calls were just getting ignored I guess.  I modified my program to collect a list of all of the products that need to have inventory tracking turned on, then execute that at the end after clearing my session, and it works!  For any future Googlers, clear your session with 

shopify.ShopifyResource.clear_session()

View solution in original post

Replies 3 (3)

Liam
Shopify Staff
2873 312 821

Hi Aevumkairos,

 

It seems like you're doing everything, based on the Shopify API documentation. One thing that I see could be a potential issue is the URL you're using for the PUT request. In your code, you're using the same URL for GET and PUT requests. According to the InventoryItem documentation, the for PUT request should be https://XXXXXX.myshopify.com/admin/api/2023-10/inventory_items/{inventoryItemId}.json where {inventoryItemId} is the ID of the inventory item you want to update.

 

Given that you're getting a 200 response, it suggests that the server successfully processed your request, but the changes aren't saved or applied. It could be that the inventory item is getting updated but the product isn't.

 

Another possible issue could be the permissions set in your Shopify private app. If you haven't already, make sure your app has the write_inventory access scope.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

aevumkairos
Shopify Partner
4 1 2

This is an accepted solution.

After further experimentation, I figured it out!  I did some testing and confirmed my code works when run standalone, which confused me.  Turns out it's the python Shopify API that was causing this issue.  Since I am creating a session and didn't clear that session, my REST API calls were just getting ignored I guess.  I modified my program to collect a list of all of the products that need to have inventory tracking turned on, then execute that at the end after clearing my session, and it works!  For any future Googlers, clear your session with 

shopify.ShopifyResource.clear_session()
Liam
Shopify Staff
2873 312 821

Great to hear you figured this out Aevumkairos! 

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog