Skip to content

3 Simple Steps to Manage An eCommerce Store with Python Using the Shopify API – Even If You’re A Beginner

Today, I’m sharing three simple tips to manage a Shopify store with Python.

As a data analyst, one of the best ways to stand out from the crowd is to become a specialist in one specific technology. If you can work with Shopify using Python you have a leg up on other data analysts that don’t have this expertise. Put yourself in a business owner’s shoes: would you rather hire someone with a specific skillset or a more general skill set?

Unfortunately, most data analysts never pick a specialty and get passed up for performance marketing analytics jobs all the time.

eCommerce is BOOMING and store owners need data experts like you.

Many data analysts won’t make an effort to learn this specialized skill, and the list of excuses is endless:

  • Python is too hard
  • eCommerce is boring
  • I don’t know where to start
  • I’m not an expert and nobody will hire me

But Python is the ultimate tool for solving problems for eCommerce store owners.

By the end of this guide you will:

  1. Be able to connect to a Shopify store
  2. Add new products
  3. Update existing products

And I’ll provide a Python notebook that you can copy to experiment on your own.

Step 1: Connecting to your store via the Shopify API using Python

If you don’t already have a Shopify dev store, I put together a full write-up:

https://twitter.com/NewPrediction/status/1597515730282373121

The entire process takes less than an hour from start to finish.

Once you have your store, here’s how you connect using Python.

  1. Install and import the Python Shopify API library
  2. Configure your store connection
    1. URL
    2. Admin API key
    3. API version
  3. Create and activate a new session

Here’s the code to connect to your Shopify store using Python

# Install and import on Google Colab
!pip install ShopifyAPI
import shopify

# Configure store details
shop_url = 'yourstorename.myshopify.com'
admin_api_key = 'your_secret_admin_api_key'
api_version = '2022-07'

# Create and activate a new session
session = shopify.Session(shop_url, api_version, admin_api_key)
shopify.ShopifyResource.activate_session(session)

If you’ve done everything correctly, you should be able to search for product details using find:

# Get product title for a specific product ID
# Your product ID will be different
product = shopify.Product.find(8017472717077)
print(f'The title of this product is "{product.title}".')

Once you’re connected you can add new products!

Step 2: Adding a new product

Adding new products to Shopify via the Admin API with Python can be done in just 3 lines of code:

# Create a new product Object, set the title, save to Shopify
new_product = shopify.Product()
new_product.title = 'Hiking Club Crewneck'
new_product.save() 

If you’ve created a new product, you’ll be able to see it in your Shopify store!

Now we can update products using the Shopify API

Step 3: Updating existing products

Updating products in Shopify with the Python API also takes a few lines of code:

crewneck = shopify.Product.find(8024192516373)
crewneck.body_html = "A very special crewneck covered in patches."
crewneck.save()

If you want to see all of the attributes for a product just use Product.attributes to get a JSON object with all of the product details.

# Command
crewneck.attributes

# Returns a JSON object with all details
{'id': 8024192516373,
 'title': 'Hiking Club Crewneck',
 'body_html': 'A very special crewneck covered in patches.',
 'vendor': 'NewPredictionDev',
 'product_type': '',
 'created_at': '2022-12-03T09:14:06-05:00',
 'handle': 'hiking-club-crewneck',
 'updated_at': '2022-12-03T09:33:03-05:00',
 'published_at': '2022-12-03T09:14:06-05:00',
 'template_suffix': None,
 'status': 'active',
 'published_scope': 'web',
 'tags': '',
 'admin_graphql_api_id': 'gid://shopify/Product/8024192516373',
 'variants': [variant(43961148440853)],
 'options': [option(10194979291413)],
 'images': [],
 'image': None,
 'price': 55.0}

That’s it!

Here’s a copy of my notebook with all of my code for this guide.

Shopify is an amazing eCommerce platform and the API gives you the power to do anything you want using Python.

That’s it for today – I hope you enjoyed it.

See you again next week!


Whenever you’re ready, here are 3 ways I can help you:

  1. View all past issues of my newsletter here.
  2. If you’re ready to build your online data analytics portfolio, my 14-day Data Analytics Portfolio Playbook is for you.
  3. If you want actionable data analytics advice on your specific situation, book a 1:1 coaching session with me today.

Leave a Reply

Your email address will not be published. Required fields are marked *