Using the Shopify API in Python

Shopify provides an excellent RESTful API for accessing and modifying your Shopify store’s data. I’m using Python and the requests library is a simple but powerful HTTP toolkit to do the mundane business of requests and responses.

Setup a Private App

The first step is to set up a private app in your Shopify store. This is covered here in the Shopify docs. Make a note of your shop’s URL (it must be the HTTPS form), and the new private app’s API key and password.

Test from the Interpreter

Before you start work in earnest it’s worthwhile using the Python interpreter to check you’ve got the right API details and the Python libraries are loaded correctly.

python
>>> import requests
>>> response = requests.get( "https://YOURSHOP.myshopify.com/admin/products/count.json",
                               auth=("YOURKEY","YOURPASSWORD"))
>>> response.text

should give you the JSON response for the number of products in your shop. Of course, you can use XML for the transfer by referencing count.xml, but JSON is Shopify’s stated preference and mine as well.
So in the case of my test shop

u'{"count":28}'

Of course you can now add your favourite JSON library, if you haven’t got one built in, and access the product count that way

>>> import json
>>> response.json()['count']
28

It really is as simple as that. The Shopify API documentation gives all the calls you can make, and the responses you can expect. It’s wise to check that response.status_code is 200, as expected, to make sure nothing has gone wrong.
There is a limiter on the API to stop it returning too many values, which is currently set at 250 records. Next time I’ll cover paging you requests to deal with this limit.

Leave a Comment