Documentation of shohpify python API?

1,495 views
Skip to first unread message

rsp

unread,
Sep 13, 2011, 7:27:31 PM9/13/11
to shopi...@googlegroups.com
I have succesfully set up the Django shopify app example and can do some basic interaction with my shop. However, I wonder if there is clearer documentation of just how to use this api? I see the documentation at api.shopify.com, but this only helps me see how the response objects are structured. I don't get how all of that translates into the python api. ie, how would i update a random variable, Mark an order as fulfilled, change the quantity of a product, accept payment on an order, etc?

The pyactiveresouce documentation seems minimal also, making this even more difficult.

Thanks,
Rohit

Dylan Smith

unread,
Sep 14, 2011, 1:50:28 PM9/14/11
to shopi...@googlegroups.com
I'll walk you through some examples, starting with translating the Product resource examples from the API documentation (http://api.shopify.com/product.html).  I'll show you as an example interactive session using the data from the documentation, similarly you could use shopify_api.py script (which works .

# Receive a list of all Products:
  # Get all products
    >>> products = shopify.Product.find()
    >>> print products
    [product(632910392), product(921728736)]
  # You can access attributes directly:
    >>> print products[0].handle
    ipod-nano
  # But for debugging/learning you might want to get a hash of all the attributes:
    >>> print products[0].attributes
    {'template_suffix': None, 'handle': 'ipod-nano', 'product_type': 'Cult Products', 'tags': 'Emotive, Flash Memory, MP3, Music', 'created_at': datetime.datetime(2011, 9, 6, 11, 46, 13, tzinfo=tzoffset(None, -14400)), 'title': 'IPod Nano - 8GB', 'body_html': '<p>It's the small iPod with one very big idea: Video. Now the world's most popular music player, available in 4GB and 8GB models, lets you enjoy TV shows, movies, video podcasts, and more. The larger, brighter display means amazing picture quality. In six eye-catching colors, iPod nano is stunning all around. And with models starting at just $149, little speaks volumes.</p>', 'updated_at': datetime.datetime(2011, 9, 6, 11, 46, 13, tzinfo=tzoffset(None, -14400)), 'published_at': datetime.datetime(2007, 12, 31, 19, 0, tzinfo=tzoffset(None, -18000)), 'id': 632910392, 'images': [image(850703190)], 'vendor': 'Burton', 'variants': [variant(808950810), variant(49148385), variant(39072856), variant(457924702)], 'options': [option(None)]}
  # You can access sub-resources (e.g. options, images and variants for Products) the same way
    >>> print products[0].images[0].src
    >>> print products[0].options[0].attributes
    {'name': 'Title'}
  
  # Fetches all products that belong to a certain collection
    >>> print shopify.Product.find(collection_id= 841564295)
    [product(632910392)]

  # Get all products after the specified ID
    >>> print shopify.Product.find(since_id=632910392)
    [product(921728736)]

  # Get all products, showing only some attributes
    >>> products = shopify.Product.find(fields='id')
    >>> print products
    [product(632910392), product(921728736)]
    >>> print products[0].attributes
    {'images': [image(850703190)], 'id': 632910392, 'title': 'IPod Nano - 8GB'}

# Receive a count of all Products:
  # Count all products
    >>> print shopify.Product.count()
    2
  # Count all products that belong to a certain collection
    >>> print shopify.Product.count(dict(collection_id=850703190))
    1

# Receive a single Product:
  # Get a single product by ID
    >>> print shopify.Product.find(632910392)
    product(632910392)

  # Get only particular fields
    >>> product = shopify.Product.find(632910392)
    >>> print product.attributes.keys()
    ['images', 'id', 'title']

# Create a new Product
  # Create a new product with multiple product variants
    >>> new_product = shopify.Product()
    >>> print new_product.id  # Only exists in memory for now
    None
    >>> new_product.product_type = "Snowboard"
    >>> new_product.body_html = "<strong>Good snowboard!</strong>"
    >>> new_product.title = "Burton Custom Freestlye 151"
    >>> variant1 = shopify.Variant()
    >>> variant2 = shopify.Variant(dict(price="20.00", option1="Second")) # attributes can be set at creation
    >>> new_product.variants = [variant1, variant2]
    >>> new_product.vendor = "Burton"
    >>> print new_product.save()  # Sends request to Shopify
    True
    >>> print new_product.id
    1048875193

  # Create a new product with the default product variant (using create)
    >>> product = shopify.Product.create(dict(product_type="Snowboard", body_html="<strong>Good snowboard!</strong>", title="Burton Custom Freestlye 151", tags="Barnes & Noble, John's Fav, \"Big Air\"", vendor="Burton"))
    >>> print product.id  # The create method already called save
    1048875194

  # Create a new, but unpublished product (using constructor for setting attributes)
    >>> product = shopify.Product(dict(product_type="Snowboard", body_html="<strong>Good snowboard!</strong>", title="Burton Custom Freestlye 151", published=False, vendor="Burton"))
    >>> product.save()

# Modify an existing Product
  # Hide a published product by changing the published attribute to false
    >>> shopify.Product(dict(id=632910392, published=False)).save()

  # Show a hidden product by changing the published attribute to true
    >>> shopify.Product(dict(id=632910392, published=True)).save()

  # Update a product's tags (use existing product object)
    >>> product = shopify.Product.find(632910392)
    >>> product.tags = "Barnes & Noble, John's Fav"
    >>> product.save()

# Remove a Product from the database
  # Using existing object
    >>> product = shopify.Product.find(632910392)
    >>> product.destroy()
  # Avoiding find where id is known
    >>> product = shopify.Product(dict(id=632910392))
    >>> product.destroy()


The other tricky thing that I will walk you through is prefix options, which you can think of as required arguments since they are used as part of the URL (as seen in the API docs).  For example, the resource Article has a prefix option for the blog id, which is used directly in the URL.  So, shopify.Article.find() doesn't, instead call shopify.Article.find(blog_id=241253187).

# Examples for the Article resource
    >>> shopify.Article.find(blog_id=241253187)
    >>> shopify.Article.count(dict(blog_id=241253187))
    >>> shopify.Article.find(134645308 , blog_id=241253187)
    >>> article = shopify.Article(prefix_options=dict(blog_id=241253187))
    >>> article.title = "My new Article title"
    >>> article.save() # create
    >>> article.author = "John Smith"
    >>> article.save() # update
    >>> article.destroy()

When an resource is saved (for creating or updating), validation errors may occur.  The error messages can be retrieved as follows:

    >>> article = shopify.Article(prefix_options=dict(blog_id=241253187))
    >>> article.title = ""
    >>> print article.save()
    False
    >>> print article.errors.full_messages()
    ["title can't be blank"]

I tried to make this extensive, since I plan on creating a page on the wiki to help others with the same problem.  Please let me know if anything is not clear enough.

Dylan Smith

unread,
Sep 16, 2011, 3:13:36 PM9/16/11
to shopi...@googlegroups.com
On Wed, Sep 14, 2011 at 1:50 PM, Dylan Smith <dylan...@jadedpixel.com> wrote:
I tried to make this extensive, since I plan on creating a page on the wiki to help others with the same problem.  Please let me know if anything is not clear enough.

The wiki page is now up here:
Reply all
Reply to author
Forward
0 new messages