Shopify API Python

12.7.0 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with the Shopify API using a private app's API key and password, activate a session, and perform basic operations like fetching shop details and making a GraphQL query. Remember to set your `SHOPIFY_API_KEY`, `SHOPIFY_PASSWORD`, `SHOPIFY_SHOP_NAME`, and `SHOPIFY_API_VERSION` environment variables or replace the placeholder values. Shopify recommends using GraphQL for new development.

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}")

view raw JSON →