File size: 1,851 Bytes
b0d7bb5 1b757f2 b0d7bb5 1b757f2 0e1fe30 1b757f2 0e1fe30 b0d7bb5 0e1fe30 b0d7bb5 0e1fe30 b0d7bb5 0e1fe30 1b757f2 0e1fe30 1b757f2 0e1fe30 1b757f2 0e1fe30 1b757f2 0e1fe30 1b757f2 b0d7bb5 0e1fe30 b0d7bb5 0e1fe30 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# 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}"
|