Fix URL Key for Specified Store Already Exists Error – Magento 2

apimagento2

Using the following command twice fails:

curl -X POST -H "Authorization: Bearer XXXXX" -H "Content-Type:     application/json" -H "Cache-Control: no-cache" -d '{
"category": {
    "includeInMenu": true, 
    "name": "AB", 
    "id": 0, 
    "isActive": true, 
    "parentId": 0
}
}' "http://localhost/rest/V1/categories"

Even though I tried changing the "name" field.

Any Idea?

Best Answer

Use body like this.

{
  "parent_id": 2,
  "name": "Category 1",
  "is_active": true,
  "include_in_menu": true,
  "custom_attributes": [
    {
      "attribute_code": "is_anchor",
      "value": "1"
    },
    {
      "attribute_code": "url_key",
      "value": "category-1"
    }  
 ]
}

Custom attributes "url_key" must be unique for all categories (and products)