Download shopify_client.py from mgbam/AICEO: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/spaces/mgbam/AICEO/resolve/309404e1401bf41ba6104877bc7bfaeb2bec3196/shopify_client.py
- Command line
-
hf download hf://spaces/mgbam/AICEO@309404e1401bf41ba6104877bc7bfaeb2bec3196/shopify_client.py
-
curl -L -o shopify_client.py https://huggingface.co/spaces/mgbam/AICEO/resolve/309404e1401bf41ba6104877bc7bfaeb2bec3196/shopify_client.py
1.85 kB
| # shopify_client.py | |
| import os | |
| import shopify | |
| # βββ Configuration βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| API_KEY = os.getenv("SHOPIFY_API_KEY") | |
| ADMIN_TOKEN = os.getenv("SHOPIFY_ADMIN_TOKEN") | |
| STORE_DOMAIN = os.getenv("SHOPIFY_STORE_DOMAIN") # e.g. "jc23eb-c7.myshopify.com" | |
| API_VERSION = "2023-10" | |
| if not (API_KEY and ADMIN_TOKEN and STORE_DOMAIN): | |
| raise RuntimeError("Shopify credentials not set in environment variables") | |
| # Build the base URL for the Admin API | |
| SHOP_URL = f"https://{API_KEY}:{ADMIN_TOKEN}@{STORE_DOMAIN}/admin/api/{API_VERSION}" | |
| shopify.ShopifyResource.set_site(SHOP_URL) | |
| # βββ Public Interface βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def create_shopify_product(title: str, description: str, price: str, image_url: str = None) -> str: | |
| """ | |
| Creates a new product in Shopify with the given title, description, and price. | |
| Optionally uploads a single image via URL. | |
| Returns the public product URL on your store. | |
| """ | |
| # 1) Build the new product object | |
| product = shopify.Product() | |
| product.title = title | |
| product.body_html = description | |
| product.variants = [shopify.Variant({"price": price})] | |
| if image_url: | |
| product.images = [shopify.Image({"src": image_url})] | |
| # 2) Save to Shopify | |
| success = product.save() | |
| if not success: | |
| errors = product.errors.full_messages() | |
| raise Exception(f"Shopify error: {errors}") | |
| # 3) Construct the storefront URL from the product handle | |
| handle = product.handle | |
| return f"https://{STORE_DOMAIN}/products/{handle}" | |