Join us for an upcoming Shopify Partner webinar on February 27, 2024. Discover the latest Checkout Extensibility features, and deep dive on improvements to Shopify Functions and Web Pixels. Register now for either the 10am EST or 2pm EST sessions.
Solved

Update Product Variant Issue with Admin API

autonomi
Shopify Partner
6 1 0

I am attempting to use the PUT Admin API for product variants located here:

https://shopify.dev/docs/api/admin-rest/2024-01/resources/product-variant#put-variants-variant-id

I am using a python client with the requests library (using requests directly is much smoother than trying to navigate the python client imo). This is not an import detail but one that informs the code:

To test the endpoint, I did a get request on a given variant, which returned appropriately (I have my headers and base url in a predefined object):

 

variant_id = 47772111503633
headers = {"Content-Type": "application/json", **shop.auth_headers}
endpoint = f"{shop.base_url}variants/{variant_id}.json"
response = requests.get(endpoint, headers=headers)
response.json()

 

 

 

{'variant': {'id': 47772111503633,
  'product_id': 9061036785937,
  'title': 'Default Title',
  'price': '6.99',
  'sku': '',
  'position': 2,
  'inventory_policy': 'deny',
  'compare_at_price': None,
  'fulfillment_service': 'manual',
  'inventory_management': 'shopify',
  'option1': 'Default Title',
  'option2': None,
  'option3': None,
  'created_at': '2023-12-06T11:39:04-06:00',
  'updated_at': '2023-12-06T11:39:04-06:00',
  'taxable': True,
  'barcode': '',
  'grams': 113,
  'image_id': None,
  'weight': 0.25,
  'weight_unit': 'lb',
  'inventory_item_id': 49754018152721,
  'inventory_quantity': 1,
  'old_inventory_quantity': 1,
  'requires_shipping': True,
  'admin_graphql_api_id': 'gid://shopify/ProductVariant/47772111503633'}}

 

 

Now, when I go to place a PUT request to the same place, I am getting an issue:

 

variant_id = 47772111503633
payload = {
    "variant": {
        "id": variant_id,
        "price": "7.99"
    }
}

headers = {"Content-Type": "application/json", **shop.auth_headers}
endpoint = f"{shop.base_url}variants/{variant_id}.json"
response = requests.put(endpoint, json=payload, headers=headers)

 

 

autonomi_0-1707340792313.png


I have tried every version of "id", "variant_id", dumping the json to a string, etc, and this continues to give me the same error. It seems that there is either be something I'm missing, or this specific end point has a bug. Can anyone please assist?

Accepted Solution (1)
autonomi
Shopify Partner
6 1 0

This is an accepted solution.

I figured it out myself. For anyone who needs this thread: The trick is that the aliased version of the API DOES work for GET requests, but DOES NOT work for POST and PUT requests. You have to use the original https://{your_store}.myshopify.com as your base URL. You cannot use the base url you set in your DNS. This appears to be new behavior as of the change to inventory_level operations in the REST Admin docs.

View solution in original post

Replies 8 (8)

Liam
Shopify Staff
2646 285 724

Hi Autonomi - is this only happening with variants? IE can you successfully make a put request to update a product title

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

autonomi
Shopify Partner
6 1 0

Hi Liam,

No, it appears that put requests in general are not working for me:

autonomi_0-1707405143812.png

 

Liam
Shopify Staff
2646 285 724

Can you try a put request in curl in your terminal? I'm thinking there could be something related to Python which could be causing this?

 

eg:

 

curl -d '{"variant":{"id":808950810,"price":"new_price_here"}}' \
-X PUT "https://your-development-store.myshopify.com/admin/api/2024-01/variants/808950810.json" \
-H "X-Shopify-Access-Token: {access_token}" \
-H "Content-Type: application/json"

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

autonomi
Shopify Partner
6 1 0

Hi Liam,

 

The command went through (I think) but the price did not update

autonomi_0-1707495957786.png



It doesn't make sense that this would be a python issue though, as I am using the requests library and hitting the API directly, not using a python client. 

autonomi
Shopify Partner
6 1 0

Hi Liam,

 

The issue seems to be pervasive across all POST and PUT requests. Get requests work fine, but the same exact request with the same exact parameters as either of these gives me a "Not Found". I have a critical application that is dependent on this. Any ideas?

 

autonomi_0-1707945426293.png

 

Liam
Shopify Staff
2646 285 724

Does your app have the write_products scope?

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

autonomi
Shopify Partner
6 1 0

Hi Liam,

 

Yes, I have all scopes, including write_products.

 

Thank you,

Issac

autonomi
Shopify Partner
6 1 0

This is an accepted solution.

I figured it out myself. For anyone who needs this thread: The trick is that the aliased version of the API DOES work for GET requests, but DOES NOT work for POST and PUT requests. You have to use the original https://{your_store}.myshopify.com as your base URL. You cannot use the base url you set in your DNS. This appears to be new behavior as of the change to inventory_level operations in the REST Admin docs.