Skip to content

Create a virtual product [Web API Tutorial]

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

Create a category

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

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

Payload

{
	"category": {
		"parent_id": 2,
		"name": "Promo Codes & Discounts",
		"is_active": true,
		"level": 2,
		"include_in_menu": true,
		"custom_attributes": [{
			"attribute_code": "is_anchor",
			"value": "1"
		}, {
			"attribute_code": "url_key",
			"value": "promo-codes-discounts"
		}, {
			"attribute_code": "url_path",
			"value": "promo-codes-discounts"
		}]
	}
}

Response
Full information about the category that was created

Complete cURL request sample

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

Create a Virtual product

After this step the virtual product with SKU '20% Discount' will be added to the products inventory.
Endpoint
POST http://<host>/rest/test/V1/products

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

Payload

{
	"product": {
		"sku": "20% Discount",
		"name": "20% Discount",
		"attribute_set_id": 4,
		"price": 100,
		"status": 1,
		"visibility": 4,
		"type_id": "virtual",
		"weight": 0.8,
		"extension_attributes": {
			"website_ids": ["2"],
			"category_links": [{
				"position": 0,
				"category_id": "8"
			}],
			"stock_item": {
				"qty": 250,
				"is_in_stock": true,
				"is_qty_decimal": false
			}
		}
	}
}

Response
Magento returns new entity.

Complete cURL request sample

discounts_category_id="3"; productSKU="20% Discount"; test_website_id="2" && curl -X POST "$endpoint/test/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"20% Discount","name":"20% Discount","attribute_set_id":4,"price":100,"status":1,"visibility":4,"type_id":"virtual","weight":0.8,"extension_attributes":{"website_ids":["2"],"category_links":[{"position":0,"category_id":"3"}],"stock_item":{"qty":250,"is_in_stock":true,"is_qty_decimal":false}}}}' | json_pp

◀️ 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