New Shopify Certification now available: Liquid Storefronts for Theme Developers

How to create multiple products using graphQL in python

Solved
mohanedmashaly
Shopify Partner
3 0 0

 

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)

 

Accepted Solution (1)
SBD_
Shopify Staff
Shopify Staff
1649 231 340

This is an accepted solution.

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.

Scott | Developer Advocate @ Shopify 

View solution in original post

Replies 4 (4)
SBD_
Shopify Staff
Shopify Staff
1649 231 340

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.

Scott | Developer Advocate @ Shopify 

mohanedmashaly
Shopify Partner
3 0 0

 

Hello,

Thank you for your reply,

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

SBD_
Shopify Staff
Shopify Staff
1649 231 340

This is an accepted solution.

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.

Scott | Developer Advocate @ Shopify 

mohanedmashaly
Shopify Partner
3 0 0

Thank you 😊