Shopify API Python
The `shopifyapi` library (version 12.7.0) enables Python developers to programmatically access the administrative section of Shopify stores. It provides an ActiveResource-like interface for interacting with both the REST and GraphQL APIs. Shopify releases new API versions quarterly, which often include breaking changes that developers need to anticipate and manage in their integrations.
Warnings
- breaking Shopify APIs undergo quarterly version updates, which frequently introduce breaking changes, deprecations, and structural modifications. These can directly impact existing `shopifyapi` integrations. For instance, the '2024-04' version deprecated specific REST API endpoints and changed how products are published/unpublished.
- breaking The `InventoryAdjustQuantityMutation` in the Admin API has been deprecated and replaced with `InventoryBulkAdjustQuantityAtLocation`. Similarly, direct product publish/unpublish actions are deprecated in favor of publishing/unpublishing to specific sales channels.
- deprecated Shopify generally recommends using the GraphQL API for new development, as the REST API is subject to deprecation in the future.
- breaking The Checkout API is being fully deprecated. This is a significant change impacting how custom checkout experiences are built.
- gotcha When setting up `shopify.Session` or `shopify.ShopifyResource.set_site()`, ensure you provide the correct API version. Using an unsupported or incorrect API version will lead to connection errors.
Install
-
pip install ShopifyAPI
Imports
- shopify
import shopify
- Session
from shopify import Session
- ShopifyResource
from shopify import ShopifyResource
Quickstart
import os
import shopify
# --- Configuration from Environment Variables ---
# For a Private App, you need API Key and Password
SHOPIFY_API_KEY = os.environ.get('SHOPIFY_API_KEY', 'your_api_key')
SHOPIFY_PASSWORD = os.environ.get('SHOPIFY_PASSWORD', 'your_password')
SHOPIFY_SHOP_NAME = os.environ.get('SHOPIFY_SHOP_NAME', 'your-shop-name') # e.g., 'my-awesome-store'
SHOPIFY_API_VERSION = os.environ.get('SHOPIFY_API_VERSION', '2024-07') # Use a stable, supported API version
# Construct the shop URL for a private app
shop_url = f"https://{SHOPIFY_API_KEY}:{SHOPIFY_PASSWORD}@{SHOPIFY_SHOP_NAME}.myshopify.com/admin"
# Set up the API session
shopify.ShopifyResource.set_site(shop_url)
shopify.ShopifyResource.set_api_version(SHOPIFY_API_VERSION)
try:
# Activate a temporary session to make calls
with shopify.Session.temp(shop_url, SHOPIFY_API_VERSION):
# Example: Fetching the current shop's details
shop = shopify.Shop.current()
print(f"Connected to shop: {shop.name} (ID: {shop.id})")
# Example: Fetching a product by ID (replace with a real product ID from your store)
# product_id = 1234567890123 # Replace with an actual product ID
# try:
# product = shopify.Product.find(product_id)
# print(f"Found product: {product.title} (ID: {product.id})")
# except Exception as e:
# print(f"Could not find product {product_id}: {e}")
# Example: Making a GraphQL query (Recommended for new development)
graphql_client = shopify.GraphQL()
query = """
query {
shop {
name
id
}
}
"""
result = graphql_client.execute(query)
print(f"GraphQL Shop Info: {result}")
except shopify.pyactiveresource.connection.ClientError as e:
print(f"API Client Error: {e.response.code} - {e.response.message}")
print("Check your API Key, Password, Shop Name, and API Version.")
except Exception as e:
print(f"An unexpected error occurred: {e}")