Update shopify store inventory using shopify API and python

AI-generated summary

Request to validate a Python script using the Shopify Python SDK to update a product’s inventory quantity.

Core steps in the code (code snippet is central):

  • Connects to a Shopify store via ShopifyResource.set_site using shop URL, API key, and password.
  • Retrieves a product by a specific product ID (placeholder 123456789).
  • Assumes a single variant and selects the first variant (product.variants[0]).
  • Sets variant.inventory_quantity to 100 and calls variant.save() to persist the change.

Assumptions/notes reflected in the snippet:

  • Works on the first (and only) variant of the product.
  • Uses hardcoded credentials and product ID placeholders to be replaced with real values.

Outcome/status: The poster is asking if this approach is correct; no confirmations, corrections, or resolutions yet. Discussion remains open.

can you please check this code if it correct or not :

import shopify

Connect to the Shopify store

shop_url = ‘your-shop-name.myshopify.com’
api_key = ‘your-api-key’
password = ‘your-api-password’
[email removed] % (api_key, password, shop_url))

Find the product you want to update

product = shopify.Product.find(123456789) # Replace 123456789 with the ID of the product

Update the inventory quantity

variant = product.variants[0] # Assuming there is only one variant
variant.inventory_quantity = 100 # Replace 100 with the new inventory quantity
variant.save()

1 Like