|
|
|
|
|
import os |
|
import shopify |
|
|
|
|
|
API_KEY = os.getenv("SHOPIFY_API_KEY") |
|
ADMIN_TOKEN = os.getenv("SHOPIFY_ADMIN_TOKEN") |
|
STORE_DOMAIN = os.getenv("SHOPIFY_STORE_DOMAIN") |
|
API_VERSION = "2023-10" |
|
|
|
if not (API_KEY and ADMIN_TOKEN and STORE_DOMAIN): |
|
raise RuntimeError("Shopify credentials not set in environment variables") |
|
|
|
|
|
SHOP_URL = f"https://{API_KEY}:{ADMIN_TOKEN}@{STORE_DOMAIN}/admin/api/{API_VERSION}" |
|
shopify.ShopifyResource.set_site(SHOP_URL) |
|
|
|
|
|
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. |
|
""" |
|
|
|
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})] |
|
|
|
|
|
success = product.save() |
|
if not success: |
|
errors = product.errors.full_messages() |
|
raise Exception(f"Shopify error: {errors}") |
|
|
|
|
|
handle = product.handle |
|
return f"https://{STORE_DOMAIN}/products/{handle}" |
|
|