Codementor Events

Making HTTP requests from Python to Shopify

Published May 27, 2018Last updated Nov 23, 2018

Introduction

This guide provides a sampling of how to make HTTP requests from Python to Shopify.

Prerequesites

  1. A Shopify store
  2. 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

  1. Install requests library
  $ pip install requests
  1. Import requests library (app.py)
import requests
  1. 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())
  1. 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'])
  1. 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())
  1. 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())
  1. 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.

Discover and read more posts from Kayode Adechinan T. Salami
get started
post comments4Replies
Jay Friedmann
2 years ago

Wow! you saved the day been searching for hours on this!!! Thanks so much

kriptcing
5 years ago

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?

Azadeh Brownlee
6 years ago

Thanks Kayode for sharing!

Kayode Adechinan T. Salami
5 years ago

You’re welcome

Show more replies