Update the price of a product using the API in python

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?

Hi DjSchutz,

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

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 = [email removed]
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

I believe this is something related to Python not working correctly with Shopify - I’m investigating something similar here: https://community.shopify.com/post/2436056