Making HTTP requests from Python to Shopify
Introduction
This guide provides a sampling of how to make HTTP requests from Python to Shopify.
Prerequesites
- A Shopify store
- An app with its credentials
Disclaimer
In this tutorial, you will see urls like this: https://apikey:password@storeaddress.myshopify.com/admin/products.json
Keep in mind that, you have to replace apikey and password with the credentials of your app. In addition to that, storeaddress must be replaced by your store address name.
How to do it
- Install requests library
$ pip install requests
- Import requests library (app.py)
import requests
- Make a post request: create a product
def create_product():
payload = {
"product": {
"title": "product for testing",
"body_html": "product for testing body",
}
}
headers = {"Accept": "application/json", "Content-Type": "application/json"}
r = requests.post("https://apikey:password@storeaddress.myshopify.com/admin/products.json", json=payload, headers=headers)
print(r.json())
- Make a get request: retrieve all products
def get_all_products():
r = requests.get(
"https://apikey:password@storeaddress.myshopify.com/admin/products.json")
print(r.json()['products'][0]['vendor'])
- Make a get request: retrieve a product by id
def get_specific_product():
r = requests.get(
"https://apikey:password@storeaddress.myshopify.com/admin/products/934425690169.json")
print(r.json())
- Make a put request: update a product
def update_product():
payload = {
"product": {
"title": "product for testing updated",
"body_html": "product for testing body updated",
}
}
headers = {"Accept": "application/json", "Content-Type": "application/json"}
r = requests.put("https://apikey:password@storeaddress.myshopify.com/admin/products/934425690169.json", json=payload, headers=headers)
print(r.json())
- Make a delete request: delete a product
def delete_product():
r = requests.delete(
"https://apikey:password@storeaddress.myshopify.com/admin/products/934425690169.json")
print(r.status_code)
Done !
Get the full code source on github .
Take a look at the Shopify REST Admin API reference. It is meant to help you customise your json data.
Wow! you saved the day been searching for hours on this!!! Thanks so much
Thank you for this Kayode, I am trying to upload an image file to my Shopify account and get a 200 success code, however, the image does not appear.
Could you please post a sample function for uploading an image for a product?
Thanks Kayode for sharing!
You’re welcome