Development discussions around Shopify APIs
This is a post for everyone who went through the same problems I did trying to make the Shopify Python API work.
DON'T!
Instead, I suggest you use GraphQL requests with this python function.
The main advantages are:
Here is a complete example to get you started. It covers rate limiting and error checking as well.
import json import time import requests API_KEY = 'myAPIKEY' PASSWORD = 'myAPIPassword' SHOP_NAME = 'myShopName' API_VERSION = '2020-04' #change to the API version shop_url = "https://%s:%s@%s.myshopify.com/admin/api/%s" % (API_KEY, PASSWORD, SHOP_NAME, API_VERSION) def callShopifyGraphQL(GraphQLString): response = requests.post(shop_url+'/graphql.json', json={'query': GraphQLString}) if response.status_code==400: raise ValueError('GraphQL error:' + response.text) answer = json.loads(response.text) throttleStatus = None if 'errors' in answer: try: throttleStatus = answer['errors'][0]['extensions']['code'] except: pass if throttleStatus != 'THROTTLED': raise ValueError('GraphQL error:' + repr(answer['errors'])) qlRequired = answer['extensions']['cost']['requestedQueryCost'] qlLimit = answer['extensions']['cost']['throttleStatus']['maximumAvailable'] qlAvail = answer['extensions']['cost']['throttleStatus']['currentlyAvailable'] print('GraphQL throttling status: ' + str(qlAvail) + '/' + str(int(qlLimit))) while throttleStatus=='THROTTLED': print('Waiting (GraphQL throttling)... ' + str(qlAvail) + '/' + str(int(qlLimit)) + ' used, requested ' + str(qlRequired)) time.sleep(1) response = requests.post(shop_url+'/graphql.json', json={'query': GraphQLString}) answer = json.loads(response.text) try: throttleStatus = answer['errors'][0]['extensions']['code'] except: throttleStatus = None pass return answer['data'] graphQLquery = """{ products(first: 100) { edges { node { handle id description } } } }""" for i in range(1,125): callShopifyGraphQL(graphQLquery)
Hello DaVinci,
thx much for your post!
@Shopify
The Python documentation at your GitHub repo is crap and will not work. I spend hours tried to get it working. This is not userfriendly. If you have much work and can't keep the docs up-to-date, tell the community.
Keep developing.
Hello DaVinci,
I tried your example, I am getting a UnicodeError: label empty or too long on line: callShopifyGraphQL(graphQLquery)
Do you mind pointing me in the right direction? I too are hitting the API limitation of Shopify. Thanks!
For anyone who have my issue, this stackoverflow thread fixed my problem:
User | RANK |
---|---|
12 | |
6 | |
5 | |
5 | |
5 |
Learn these 5 things I had to learn the hard way with starting and running my own business
By Kitana Jan 27, 2023Would you love to unleash the unbridled power of the Google Shopping Channel into your sho...
By Gabe Jan 6, 2023How can you turn a hobby into a career? That’s what Emmanuel did while working as a wa...
By Skye Dec 30, 2022