Skip to content

Create a configurable product [Web API Tutorial]

Ievgen Shakhsuvarov edited this page Jul 16, 2019 · 53 revisions

Create an attribute set for configurations

Lets keep the Default attribute set from changing and create one new attribute set where we can add anything we will need.

Endpoint
POST http://<host>/rest/all/V1/eav/attribute-sets

HTTP headers

Content-Type: application/json 
Authorization: Bearer <admin_token>

Payload

{
  "entityTypeCode": "catalog_product",
  "attributeSet": {
    "attribute_set_name": "Configurable Hoodies and Sweatshirts",
    "entity_type_id": 4
  },
  "skeletonId": 4
}

Response
Full information about the attribute set that was created with assigned attribute_set_id

Complete cURL request sample

curl -X POST "$endpoint/all/V1/eav/attribute-sets" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" \
 -d '{"entityTypeCode":"catalog_product","attributeSet":{"attribute_set_name":"Configurable Hoodies and Sweatshirts","entity_type_id":4},"skeletonId":4}' | json_pp

Create an attribute

Endpoint
POST http://<host>/rest/all/V1/products/attributes

HTTP headers

Content-Type: application/json 
Authorization: Bearer <admin_token>

Payload

{
	"attribute": {
		"attribute_code": "size",
		"entity_type_id": "4",
                "default_frontend_label": "Size",
		"frontend_labels": [{
			"store_id": 1,
			"label": "Size"
		},{
			"store_id": 2,
			"label": "Size"
		}],
		"is_required": true,
		"default_value": "",
		"frontend_input": "select",
		"is_visible_on_front": true,
		"is_searchable": true,
		"is_visible_in_advanced_search": true,
		"is_filterable": true,
		"is_filterable_in_search": true,
		"options": [{
			"label": "S",
			"value": "S",
			"sort_order": 100,
			"is_default": true,
			"store_labels": [{
				"store_id": 2,
				"label": "Size S"
			}, {
				"store_id": 1,
				"label": "Size S"
			}]
		}, {
			"label": "M",
			"value": "M",
			"sort_order": 0,
			"is_default": false,
			"store_labels": [{
				"store_id": 2,
				"label": "Size M"
			}, {
				"store_id": 1,
				"label": "Size M"
			}]
		}, {
			"label": "L",
			"value": "L",
			"sort_order": 0,
			"is_default": false,
			"store_labels": [{
				"store_id": 2,
				"label": "Size L"
			}, {
				"store_id": 1,
				"label": "Size L"
			}]
		}, {
			"label": "XL",
			"value": "XL",
			"sort_order": 0,
			"is_default": false,
			"store_labels": [{
				"store_id": 2,
				"label": "Size XL"
			}, {
				"store_id": 1,
				"label": "Size XL"
			}]
		}, {
			"label": "XXL",
			"value": "XXL",
			"sort_order": 0,
			"is_default": false,
			"store_labels": [{
				"store_id": 2,
				"label": "Size XXL"
			}, {
				"store_id": 1,
				"label": "Size XXL"
			}]
		}]
	}
}

Complete cUrl request

curl -X POST "$endpoint/all/V1/products/attributes" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" \
 -d '{"attribute":{"attribute_code":"size","entity_type_id":"4","default_frontend_label":"Size","frontend_labels":[{"store_id":1,"label":"Size"},{"store_id":2,"label":"Size"}],"is_required":true,"default_value":"","frontend_input":"select","is_visible_on_front":true,"is_searchable":true,"is_visible_in_advanced_search":true,"is_filterable":true,"is_filterable_in_search":true,"options":[{"label":"S","value":"S","sort_order":100,"is_default":true,"store_labels":[{"store_id":2,"label":"Size S"},{"store_id":1,"label":"Size S"}]},{"label":"M","value":"M","sort_order":0,"is_default":false,"store_labels":[{"store_id":2,"label":"Size M"},{"store_id":1,"label":"Size M"}]},{"label":"L","value":"L","sort_order":0,"is_default":false,"store_labels":[{"store_id":2,"label":"Size L"},{"store_id":1,"label":"Size L"}]},{"label":"XL","value":"XL","sort_order":0,"is_default":false,"store_labels":[{"store_id":2,"label":"Size XL"},{"store_id":1,"label":"Size XL"}]},{"label":"XXL","value":"XXL","sort_order":0,"is_default":false,"store_labels":[{"store_id":2,"label":"Size XXL"},{"store_id":1,"label":"Size XXL"}]}]}}' | json_pp

Assign attribute to an attribute set

The attributeSetId, attributeGroupId, attributeCode, sortOrder attributes are required. The value for attributeSetId you have already obtained in Create an attribute set for configurations step. What about other attributes? Read about Data preparation before assigning attribute to an attribute set

Endpoint
POST /V1/products/attribute-sets/attributes

HTTP headers

Content-Type: application/json 
Authorization: Bearer <admin_token>

Payload

{
  "attributeSetId": 9,
  "attributeGroupId": 30,
  "attributeCode": "size",
  "sortOrder": 30
}

Response

Complete cURL request

curl -X POST "$endpoint/all/V1/products/attribute-sets/attributes" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" \
 -d '{"attributeSetId":9,"attributeGroupId":30,"attributeCode":"size","sortOrder":30}' | json_pp -json_opt allow_nonref

Step 2. Create a category

During this operation, you will get id of the category. You need to remember this value. Anyway, you always can get the value of the id property from catalogCategoryListV1 interface by addressing it from your local API reference.
In case you are confused with searchCriteria you may try to run this snippet:

curl -X GET "$endpoint/all/V1/categories/list?searchCriteria%5BpageSize%5D=100&searchCriteria%5BcurrentPage%5D=1" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" | grep -i '"id"'

Endpoint
POST http://<host>/rest/all/V1/categories"

HTTP headers

Content-Type: application/json 
Authorization: Bearer <admin_token>

Payload

{
	"category": {
		"parent_id": 2,
		"name": "Hoodies & Sweatshirts",
		"is_active": true,
		"level": 2,
		"include_in_menu": true,
		"custom_attributes": [{
			"attribute_code": "is_anchor",
			"value": "1"
		}, {
			"attribute_code": "url_key",
			"value": "hoodies-sweatshirts"
		}, {
			"attribute_code": "url_path",
			"value": "hoodies-sweatshirts"
		}]
	}
}

Response
Full information about the category that was created and id that was assigned to the category

Complete cURL request sample

curl -X POST "$endpoint/all/V1/categories" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" \
 -d '{"category":{"parent_id":2,"name":"Hoodies & Sweatshirts","is_active":true,"level":2,"include_in_menu":true,"custom_attributes":[{"attribute_code":"is_anchor","value":"1"},{"attribute_code":"url_key","value":"hoodies-sweatshirts"},{"attribute_code":"url_path","value":"hoodies-sweatshirts"}]}}' | json_pp | grep -i '"id"' 

Create the product

Endpoint
POST http://<host>/rest/test/V1/products

HTTP headers

Content-Type: application/json  
Authorization: Bearer <admin_token>

Payload

{
	"product": {
		"sku": "Swim Warm-Up Hoodie",
		"name": "Swim Warm-Up Hoodie",
		"status": 1,
		"type_id": "configurable",
		"attribute_set_id": 9,
		"extension_attributes": {
			"category_links": [{
				"position": 0,
				"category_id": "5"
			}],
			"stock_item": {
				"is_in_stock": true
			}
		}
	}
}

Response Full information about the product that was created

Complete cURL request sample

configurable_product_SKU="Swim Warm-Up Hoodie"; category_id="5" && curl -X POST "$endpoint/test/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$configurable_product_SKU"'","name":"'"$configurable_product_SKU"'","status":1,"type_id":"configurable","attribute_set_id":9,"extension_attributes":{"category_links":[{"position":0,"category_id":"'"$category_id"'"}],"stock_item":{"is_in_stock":true}}}}'

Create configurations

Send each payload as a seperate request on the same endpoint with the same headers.

Endpoint POST http://<host>/rest/test/V1/products

HTTP headers

Content-Type: application/json  
Authorization: Bearer <admin_token>

Payload

{
	"product": {
		"sku": "Swim Warm-Up Hoodie - S",
		"name": "Swim Warm-Up Hoodie - S",
		"attribute_set_id": 9,
		"price": 90,
		"status": 1,
		"visibility": 1,
		"type_id": "simple",
		"weight": "0.6",
		"extension_attributes": {
			"stock_item": {
				"qty": "70",
				"is_in_stock": true
			}
		},
		"custom_attributes": [{
			"attribute_code": "size",
			"value": "4"
		}]
	}
}

Send this payload 4 more times, but modify all of these values each time:

sku & name qty attribute_code value
Swim Warm-Up Hoodie - M 80 size 5
Swim Warm-Up Hoodie - L 100 size 6
Swim Warm-Up Hoodie - XL 75 size 7
Swim Warm-Up Hoodie - XXL 40 size 8

Complete cURL request

configurable_product_SKU_V1="Swim Warm-Up Hoodie - S" && curl -X POST "$endpoint/test/V1/products" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" \
 -d '{"product":{"sku":"'"$configurable_product_SKU_V1"'","name":"'"$configurable_product_SKU_V1"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"0.6","extension_attributes":{"stock_item":{"qty":"70","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"4"}]}}' | json_pp

configurable_product_SKU_V2="Swim Warm-Up Hoodie - M" && curl -X POST "$endpoint/test/V1/products" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" \
 -d '{"product":{"sku":"'"$configurable_product_SKU_V2"'","name":"'"$configurable_product_SKU_V2"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"0.72","extension_attributes":{"stock_item":{"qty":"80","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"5"}]}}' | json_pp

configurable_product_SKU_V3="Swim Warm-Up Hoodie - L" && curl -X POST "$endpoint/test/V1/products" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" \
 -d '{"product":{"sku":"'"$configurable_product_SKU_V3"'","name":"'"$configurable_product_SKU_V3"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"0.8","extension_attributes":{"stock_item":{"qty":"100","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"6"}]}}' | json_pp

configurable_product_SKU_V4="Swim Warm-Up Hoodie - XL" && curl -X POST "$endpoint/test/V1/products" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" \
 -d '{"product":{"sku":"'"$configurable_product_SKU_V4"'","name":"'"$configurable_product_SKU_V4"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"1","extension_attributes":{"stock_item":{"qty":"75","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"7"}]}}' | json_pp

configurable_product_SKU_V5="Swim Warm-Up Hoodie - XXL" && curl -X POST "$endpoint/test/V1/products" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $admin_token" \
 -d '{"product":{"sku":"'"$configurable_product_SKU_V5"'","name":"'"$configurable_product_SKU_V5"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"1.12","extension_attributes":{"stock_item":{"qty":"40","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"8"}]}}' | json_pp

Specify the attribute which is configurable

Endpoint

Payload

{
	"option": {
		"attribute_id": "135",
		"label": "Size",
		"position": 0,
		"is_use_default": false,
		"values": [{
			"value_index": 4
		}, {
			"value_index": 5
		}, {
			"value_index": 6
		}, {
			"value_index": 7
		}, {
			"value_index": 8
		}]
	}
}

Complete cURL request

productSKUURLencoded=$(echo "$configurable_product_SKU" | sed 's/ /%20/g') && curl -X POST "$endpoint/test/V1/configurable-products/$productSKUURLencoded/options" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"option":{"attribute_id":"135","label":"Size","position":0,"is_use_default":false,"values":[{"value_index":4},{"value_index":5},{"value_index":6},{"value_index":7},{"value_index":8}]}}'

Products Catalog

◀️ Previous Step 📖 Table of Contents Next Step ▶️

MSI Documentation:

  1. Technical Vision. Catalog Inventory
  2. Installation Guide
  3. List of Inventory APIs and their legacy analogs
  4. MSI Roadmap
  5. Known Issues in Order Lifecycle
  6. MSI User Guide
  7. DevDocs Documentation
  8. User Stories
  9. User Scenarios:
  10. Technical Designs:
  11. Admin UI
  12. MFTF Extension Tests
  13. Weekly MSI Demos
  14. Tutorials
Clone this wiki locally