AICEO / shopify_client.py
mgbam's picture
Update shopify_client.py
0e1fe30 verified
raw
history blame
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}"