How to create multiple products using graphQL in python

Topic summary

Issue: A developer wants to create multiple Shopify products via GraphQL in Python by fetching data from an API and passing it to bulkOperationRunQuery mutation, without using JSON file uploads or stagedUploadsCreate.

Code Attempt: The initial code shows a mutation structure with product inputs in a list, but contains syntax errors and reversed text in parts of the implementation.

Solutions Provided:

  • Bulk Operations: One approach involves using bulk operations with multiple productCreate mutations (reference link provided to community example)
  • Multiple Mutations in Single Request: An alternative is sending multiple aliased mutations (p1, p2, etc.) in one GraphQL request, each creating a product with fields like title, descriptionHtml, productType, and vendor
  • Recommendation: For large volumes of products, the bulkOperationRunMutation/upload approach is considered best practice

Status: The original poster acknowledged the suggestions. The discussion confirms it’s possible to create multiple products without stagedUploadsCreate, though the optimal approach depends on product volume.

Summarized with AI on November 15. AI used: claude-sonnet-4-5-20250929.

I am trying to create multiple product using bulk operation but all the docs I see/visited talks about uploading json file or stagedUploadsCreate, for example, I want to fetch products from API and loop over them and pass them once to bulkOperationRunQuery mutation

import requests
import json

# Create a list of product inputs
product_inputs = [
    {
        'title': 'Product 1'
        # Add other fields as needed
    },
    {
        'title': 'Product 2'
        # Add other fields as needed
    },
    # Add more product inputs as necessary
]

# Construct the GraphQL request as a string
query = '''
mutation{
  productCreate(input:
    {
    title:input
  }
  )
  {
    product{
      id
    }
  }
}
'''

# Define the request headers

# Create the request payload
payload = {
    'query': query,
     'variables': {
        'products': product_inputs,
    },
}

# Send the GraphQL request
response = requests.post(endpoint, headers=headers, json=payload)
if response.status_code == 200:
    result = response.json()
    print(result)
    product_create_multiple = result.get('data', {}).get('productCreateMultiple', {})

    if 'userErrors' in product_create_multiple and product_create_multiple['userErrors']:
        print('Errors occurred while creating products:')
        for error in product_create_multiple['userErrors']:
            print(f"Field: {error['field']}, Message: {error['message']}")
    elif 'products' in product_create_multiple and product_create_multiple['products']:
        print('Products created successfully:')
        for product in product_create_multiple['products']:
            print(f"ID: {product['id']}, Title: {product['title']}")
else:
    print('Failed to send the GraphQL request. Status Code:', response.status_code)

Hey @mohanedmashaly

I can’t help you with the Python part but here’s an example of running multiple productCreate mutations with a bulk mutation.

Hello,

Thank you for your reply,

So it is possible to create multiple of products without using stagedUploadsCreate ?

Hey @mohanedmashaly

An alternative approach would to send a mutation for each product, or multiple mutations in a single request like this:

mutation {
  p1: productCreate(input: {
    title: "Product 1",
    descriptionHtml: "Description for product 1",
    productType: "Custom product",
    vendor: "Vendor"
  }) {
    product {
      id
    }
    userErrors {
      field
      message
    }
  }
  p2: productCreate(input: {
    title: "Product 2",
    descriptionHtml: "Description for product 2",
    productType: "Custom product",
    vendor: "Vendor"
  }) {
    product {
      id
    }
    userErrors {
      field
      message
    }
  }
  # continue for each product...
}

For large amount of products, the bulkOperationRunMutation/upload approach would be best.

Thank you :blush: