I’m new to Shopify GraphQL and trying to register products through GraphQL and Python.
I wrote a code as below but it returns an error. Products with only text can be registered but products with HTML are not registered.
Using GCP, Theme: Debut
import requests
import json
# Shopify API credentials
shop_url = "ShopURL"
access_token = "ACCESS TOKEN"
# GraphQL mutation
mutation = '''
mutation {
productCreate(input: {
title: "ProductCreateTest"
descriptionHtml: '<img data-src=\"https://cdn.shopify.com/s/files/test.jpg?v=123456\" alt=\"\">
variants: [
{
price: 29.99
}
]
images: [
{
src: "https://example.com/image.jpg"
}
]
}) {
product {
id
title
}
}
}
'''
# GraphQL API endpoint
api_url = f"{shop_url}/admin/api/2023-07/graphql.json"
# Headers with authentication
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": access_token
}
# Send the GraphQL request
response = requests.post(api_url, headers=headers, data=json.dumps({"query": mutation}))
# Check for success or handle errors
if response.status_code == 200:
result = response.json()
created_product = result.get("data", {}).get("productCreate", {}).get("product", {})
if created_product:
product_id = created_product.get("id")
print(f"Product created with ID: {product_id}")
else:
print("Error creating product")
else:
print(f"Error: {response.status_code}, {response.text}")
When putting plain text on the descriptionHTML field, it works perfectly.
What I have tried on descriptionHTML field :
descriptionHtml: “<img data-src=“https://cdn.shopify.com/s/files/test.jpg?v=123456" alt=””>
“doublequotes”
"descriptionHtml: “<img data-src="[https://cdn.shopify.com/s/files/test.jpg?v=123456\](https://cdn.shopify.com/s/files/test.jpg?v=123456\)” alt="">
"doublequotes"
"