Using the shopify python api
From Shopify Wiki
Contents |
Setup
Open up your terminal, and install the latest release of the shopify python API package along with its dependencies:
$ easy_install -U ShopifyAPI
Next, we need an API_KEY and API_PASSWORD for a shop to try out the API. Follow the instructions on the Private applications page to obtain these credentials. For a partner account, obtain the credentials for a test shop.
Now open up a Python interactive session:
$ python
Next we need to import the library and tell it to use the shop's credentials for following API calls:
>>> import shopify >>> SHOP_NAME = "<paste your API shop name here>" >>> API_PASSWORD = "<paste your API password here>" >>> session = shopify.Session(SHOP_NAME) >>> session.token = API_PASSWORD >>> shopify.ShopifyResource.activate_session(session)
Using the API Reference
The API reference is a valuable resource for using the API, because it shows what resources are accessible through the API, what API calls can be made, and what data is sent and received for these API calls.
This section will walk you through some examples to how to use the API through the python adaptor. I will start with translating several of the Product resource examples from the Product API documentation. You can follow along with your python interactive session, but the data used is from the API documentation, so will differ from what you see for your shop.
Receive a list of all Products
Get all products:
>>> products = shopify.Product.find() >>> products [product(632910392), product(921728736)]
You can access attributes directly
>>> products[0].handle ipod-nano
But for debugging/learning you might want to get a hash of all the attributes:
>>> 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
>>> products[0].images[0].src
http://static.shopify.com/s/files/1/6909/3384/products/ipod-nano.png?0
>>> products[0].options[0].attributes
{'name': 'Title'}
Fetches all products that belong to a certain collection
>>> shopify.Product.find(collection_id=841564295) [product(632910392)]
Get all products after the specified ID
>>> shopify.Product.find(since_id=632910392) [product(921728736)]
Get all products, showing only some attributes
>>> products = shopify.Product.find(fields='id')
>>> products
[product(632910392), product(921728736)]
>>> products[0].attributes
{'images': [image(850703190)], 'id': 632910392, 'title': 'IPod Nano - 8GB'}
Receive a count of all Products
Count all products
>>> shopify.Product.count() 2
Count all products that belong to a certain collection
>>> shopify.Product.count(collection_id=850703190) 1
Receive a single Product
Get a single product by ID:
>>> shopify.Product.find(632910392) product(632910392)
Get only particular fields:
>>> product = shopify.Product.find(632910392) >>> 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" >>> new_product.save() # Sends request to Shopify True >>> 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")) >>> 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()
Prefix Options
Prefix options can be thought of as required arguments, since they are used as part. This can be seen in the API documentations for Articles resource, which has a prefix option for the blog id, which is used directly in the URL.
Currently there is not a single API call to list all articles, but all articles for a blog can be retrieved as follows:
>>> shopify.Article.find(blog_id=241253187) [article(989034056), article(134645308)]
Here are several more examples for the Article resource:
>>> articles = shopify.Article.find(blog_id=241253187) >>> count = shopify.Article.count(blog_id=241253187) >>> article = shopify.Article.find(134645308, blog_id=241253187) >>> article = shopify.Article(dict(blog_id=241253187)) >>> article.title = "My new Article title" >>> article.save() # create True >>> article.author = "John Smith" >>> article.save() # update True >>> article.destroy()
Handling Errors
When a resource is saved (for creating or updating), validation errors may occur. The error messages can be retrieved as follows:
>>> article = shopify.Article(dict(blog_id=241253187)) >>> article.title = "" >>> article.save() False >>> article.errors.full_messages() ["title can't be blank"]
