Shopify – python api (shopify.SmartCollection.create)

Yep.. I’ve joined the dark side.. from .net to python.. What is more I’m moving away from Ecommerce Sites that i have built from scratch to the more universal Shopify Platform (at least for evaluation reasons)

On first blush this may be better as an ecommerce site than i expected. It has some shocking limitations like only supporting a VERY limited number of payment gateways and having little to no concept of an ACH transaction (ones that take money directly from a persons bank account.. WAY cheeper than running credit card transactions..

One of my tasks was to see if we could write code to generate a menu based on our products taxonomy. Out of the box our company sells around 130 different categorys and if we didn’t script this creation it would be really easy to have unreachable products after the import. The fact that Shopify doesn’t really even understand categories isn’t that much of a problem.. What it does have is “collections”, “collections” come in 2 flavors, dynamic and static.

Because we want automatic incorporation of products as they are uploaded into Shopify the correct choice is dynamic.

So.. how did we manage to script the creation.. Most of the documentation is in PHP.. but our current flavor of language is python and as much as it is probably possible to just convert the code over, shopify was nice enough to create a PYTHON based api that allows you to work with both the ADMIN api and STOREFRONT api. The ADMIN api actually providing all the ability required to work with products or collections

You also need to generate API keys for shopify. To do this login to your store and create a new “app key”

  1. Log into your store and in theme actions click “Edit Code”
  2. On the next screen select “Apps” in the left hand menu.
  3. On the Apps Screen select (at the bottom of the page) “Manage Private Apps”
  4. If you don’t already have one click the button labeled “Create a new private app” (top right)
  5. On the next screen review all the permissions available and what they pertain to (select the ones you need)
  6. You will need the followitn values after going to your app (copy them somewhere)
    API key
    Shared secret
    and example u8rl (as a sanity check)
From the python side you need to install the Shopify Python API (docs:
  1. open a bash prompt
  2. use pip to install the apiĀ  pip install –upgrade ShopifyAPI
In this example I’m just going to create a simple python file that can be executed from a command prompt of some type. (bash or dos)
The first snippet is what is needed to use the API
import shopify


shop_url = "" % (API_KEY, PASSWORD, SHOP_NAME)


This adds a new collection called “rob test” that finds any products where the Product Type is “Stools” and adds them to the collection.

After adding the collection it prints the return from the create hopefully showing more detail about the new collection.

added = shopify.SmartCollection.create({
    "title": "rob test",
    "rules": [
        "column": "type",
        "relation": "equals",
        "condition": "Stools"
    "published": 'true'#
print ('---- added equals ------')
print (added.__dict__)
print ('---- end added  ------')

Listing all collections and their properties

x = shopify.SmartCollection().find()print('-------------------------------------------------------------')
# print x
for y in x:
# if y.attributes["title"] == "rob test":
# print y.__dict__# prints all the properties of the collection
# print y.attributes['title']
print y.__dict__
# the properties are all stored in a dictionary.. you can loop through
# them if needed. print('-------------------------------------------------------------')

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>