How to specify "product category" with the REST API (python shopify module)

I’m trying to create a product using Python shopify module. ( https://github.com/Shopify/shopify_python_api )

Here is a simplified minimal working example that saves a new product:

product.title = MY_PRODUCT_TITLE
product.body_html = MY_PRODUCT_DESCRIPTION
product.status = "draft"

# https://help.shopify.com/en/manual/products/details#product-organization
product.vendor = MY_VENDOR
product.product_type = MY_PRODUCT_TYPE
# Product category - A label that describes the group or class that a product belongs to.
# The product category is selected from the Shopify Product Taxonomy, a predefined,
# standardized list https://help.shopify.com/txt/product_taxonomy/en.txt
product.product_category = "4371"

product.save()

The above code creates a new product indeed, but the product category is not saved.

This page https://help.shopify.com/en/manual/products/details#product-organization tells that product organization includes: category, type, vendor, collections, tags. I could set all of them, except the product category.

Of course, the “product.product_category” attribute does not exist, I just made that up. In fact, the “product category” attribute is nowhere to be found in the REST API documentation. The “Product resource” is documented herre https://shopify.dev/docs/api/admin-rest/2023-07/resources/product#resource-object and it does not have a product_category attribute. (But it does have product_type and vendor attributes.)

I have been trying to set the product category with this API all day yesterday, and I failed. It is very strange that the product admin page shows the product category, it is also described in the help pages, and shopify even has a URL where you can download the whole category tree ( https://help.shopify.com/txt/product_taxonomy/en.txt ) but it cannot be accessed from the REST API (or at least I could not find it).

I also tried to create a product on the web interface, then fetch product data using the REST API. Guess what happened: the REST API did not return the product category.

Please help me find out how to do this.

1 Like

BTW a productCategory field exists in GraphQL, it is documented here https://shopify.dev/docs/api/admin-graphql/2023-07/objects/Product#field-product-productcategory it has https://shopify.dev/docs/api/admin-graphql/2023-07/objects/ProductCategory type. But GraphQL cannot be used to create or modify products. It can only be used to query data.

So in GraphQL, there is productType and also productCategory. But in the REST API, product_category is missing.

I just realized that it is possible to create a product with ShopifySQL https://shopify.dev/docs/api/admin-graphql/2023-07/mutations/productCreate#field-productinput-productcategory but unfortunately, this is unavailable from the python library.

I really need to do this from Python, because all of the backend is written in Python.

2 Likes

I’m having the exact same problem, thank you for raising this issue ! Will be watching, hopefully there is a solution. Thank you !

In the meantime, Product.productCategory attribute (GraphQL) became deprecated too.

https://shopify.dev/docs/api/admin-graphql/2024-04/objects/Product#field-product-productcategory

The documentation says “The product category specified by the merchant. Deprecated in API version 2024-04. Use category instead.”

The category is documented here:

https://shopify.dev/docs/api/admin-graphql/2024-04/objects/Product#field-product-category

Its value is a TaxonomyCategory, and that is described here: https://shopify.dev/docs/api/admin-graphql/2024-04/objects/TaxonomyCategory

Finally, the list of available categories was also changed, it is no longer available at https://help.shopify.com/txt/product_taxonomy/en.txt but there is a link under TaxonomyCategory docs:

It currently points to this link: https://shopify.github.io/product-taxonomy/releases/unstable/?categoryId=sg-4-17-2-17&shpxid=67d91158-4B1B-4C2D-12DC-C4DB34C3476E

But that is a generated link, it says UNSTABLE in big unfriendly letters. I guess it means that the category tree is continuously changing. There is no way to get notified about changes in the category tree (or at least I don’t see it). It is not clear what happens with the products that are assigned a category that is being removed from the tree.

Looks like these things are changing frequently, and it is not easy to follow.

2 Likes

What we have know is this:

  1. we create the product with REST api, in “draft” status

  2. then we change category, and upload product translations via graphql

  3. finally, we change it to “active”

If something fails, then the product will remain in “draft” state and nobody will see it.

There is no graphql library for python, so I wrote my own. It is far from complete, I’m only adding support for new API calls when I need them.

In the end, probably I’ll have to migrate all calls from REST to GraphQL, because REST is not deprecated completely. (It also means that the “official” python library is deprecated as a whole.)