What's your biggest current challenge? Have your say in Community Polls along the right column.
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.

update the price of a product using the API in python

update the price of a product using the API in python

djSchutz
Visitor
2 0 0
Hey guys, I'm trying to update the price of a product using the API, but this error is occurring: Failed to update price for product 9108841398545: {"errors":{"variant":"Required parameter missing or invalid"}}. Does anyone 

know how to solve it?

djSchutz_0-1707252912897.png

 

 
Replies 3 (3)

Liam
Community Manager
3108 344 895

Hi DjSchutz,

 

Are you using a library to make API calls to Shopify with python? 

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

djSchutz
Visitor
2 0 0

Only requests and this class:

 

class ShopifyInterface:

def __init__(self, shop_key):
self.shop_url = #url here
self.access_key = shop_key
self.api_version = '2023-10'
self.base_url = f"{api_ley}:{shop_key}@{self.shop_url}/admin/api/{self.api_version}/"
self.auth_headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": self.access_key
}
self.pagination_wait_time = 0.33 #Prevent rate limiting

##### function to add new products and update the price of existing products ################
@anvil.server.callable
def update_product_prices(product_id, new_price):
shop_key = anvil.secrets.get_secret('shopify_admin_key')
shop = ShopifyInterface(shop_key)

# Assuming you want to update a specific variant
variant_id = 47957229371665

payload = {
"variants": [{
"id": variant_id, # Use the correct field name for variant ID
"price": new_price # Use the new_price parameter
}]
}

headers = {"Content-Type": "application/json", **shop.auth_headers}
endpoint = f"{shop.base_url}products/{product_id}" # Fix the typo in the endpoint URL

response = requests.put(endpoint, json=payload, headers=headers)

if response.ok:
print(f"Price updated successfully for product {product_id}")
print(response.text)
print(response.status_code)
else:
print(f"Failed to update price for product {product_id}: {response.text}")
print(response.status_code)

 

 

 

UPDATE: I'm now getting a 200 status code but it's not actually updating

 
Liam
Community Manager
3108 344 895

I believe this is something related to Python not working correctly with Shopify - I'm investigating something similar here: https://community.shopify.com/c/authentication-and-access/update-product-variant-issue-with-admin-ap...

 

 

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