422 when creating multiple products with the same title - BUG

Patrick_Hughes
Shopify Expert
24 0 9

Hey @Shopify, I think I found a bug.


When I try to create multiple products with the same title asynchronously only one product gets created and the rest throw a 422 Unprocessable Entity.

 

const shopify = new Shopify(....); // official node api -> shopify-api-node
let products = [
    { title: 'Some name', vendor: 'A' },
    { title: 'Some name', vendor: 'B' },
    { title: 'Some name', vendor: 'C' },
    { title: 'Some name', vendor: 'D' }
]

for(let product of products){
    shopify.product.create(product).then().catch( err => console.error(err) )
}

  
I think the problem lies with the handle creation, because if I run the same code except with handles defined like this:

let products = [
    { title: 'Some name', handle: 'some-name', vendor: 'A' },
    { title: 'Some name', handle: 'some-name-1', vendor: 'B' },
    { title: 'Some name', handle: 'some-name-2', vendor: 'C' },
    { title: 'Some name', handle: 'some-name-3', vendor: 'D' }
]

then it works successfully. 

Replies 2 (2)
Busfox
Shopify Staff (Retired)
Shopify Staff (Retired)
628 49 110

Hi @Patrick_Hughes,

 

You're correct, it's the handle creation that is causing this. The error I get when attempting this is:

 

{
  "errors": {
    "handle": [
      "has already been taken"
    ]
  }
}

When no handle is specified, we set the handle as the parameterized title. When two same-titled products are created at the same time, we try to create them with the same handle. One will fail in this case. Otherwise, if there was time between the products being created, we'd append a numerical suffix to the handle if a product with the handle already exists.

 

I'm curious what your use case is that this acts as a blocker? I can create an issue internally, but I'm not sure it will be a priority.

Andrew | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

Patrick_Hughes
Shopify Expert
24 0 9

Hey @Busfox 

Thanks for looking into this and explaining what's going on behind the scenes here. It wasn't a blocker for me but it was unexpected. I got around it by placing all existing handles (which I have in the DB) into a lookup, generating a handle on my side, and then running a while loop until the numerical suffix makes the handle unique in the lookup. Another, easier but slower option is to create the products synchronously. The use case is for an epos integration where it's possible to have two products with the same title created at the same time.