Skip to content

Create a downloadable product [Web API Tutorial]

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

Create an attribute set for downloadable products

Let us keep the Default attribute set from changing and create the new one.

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": "Downloadable",
    "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":"Downloadable","entity_type_id":4},"skeletonId":4}' | json_pp

Create attributes

Create attribute format

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

HTTP headers

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

Payload

{
	"attribute": {
		"attribute_code": "format",
		"entity_type_id": "4",
		"default_frontend_label": "Format",
		"is_required": false,
		"default_value": "",
		"frontend_input": "select",
		"is_visible_on_front": "1",
		"is_searchable": "0",
		"is_visible_in_advanced_search": "0",
		"is_filterable": true,
		"is_filterable_in_search": false,
		"options": [
                        {
				"label": "Download",
				"value": "option-download"
			},
			{
				"label": "DVD",
				"value": "option-dvd"
			}
		]
	}
}

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":"format","entity_type_id":"4","default_frontend_label":"Format","is_required":false,"default_value":"","frontend_input":"select","is_visible_on_front":"1","is_searchable":"0","is_visible_in_advanced_search":"0","is_filterable":true,"is_filterable_in_search":false,"options":[{"label":"Download","value":"option-download"},{"label":"DVD","value":"option-dvd"}]}}' | json_pp

Create attribute activity

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

HTTP headers

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

Payload

{
    "attribute": {
        "attribute_code": "activity",
        "default_frontend_label": "Activity",
        "default_value": "",
        "entity_type_id": "4",
        "frontend_input": "multiselect",
        "is_filterable": true,
        "is_filterable_in_grid": false,
        "is_filterable_in_search": false,
        "is_required": false,
        "is_searchable": "0",
        "is_visible_in_advanced_search": "0",
        "is_visible_on_front": "1",
        "options": [
            {
                "label": "Running",
                "value": "Running"
            },
            {
                "label": "Warmup",
                "value": "Warmup"
            },
            {
                "label": "Yoga",
                "value": "Yoga"
            },
            {
                "label": "Gym",
                "value": "Gym"
            },
            {
                "label": "Cycling",
                "value": "Cycling"
            },
            {
                "label": "Athletic",
                "value": "Athletic"
            },
            {
                "label": "Sports",
                "value": "Sports"
            },
            {
                "label": "Hiking",
                "value": "Hiking"
            }
        ],
        "scope": "global",
        "used_in_product_listing": "0"
    }
}

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":"activity","default_frontend_label":"Activity","default_value":"","entity_type_id":"4","frontend_input":"multiselect","is_filterable":true,"is_filterable_in_grid":false,"is_filterable_in_search":false,"is_required":false,"is_searchable":"0","is_visible_in_advanced_search":"0","is_visible_on_front":"1","options":[{"label":"Running","value":"Running"},{"label":"Warmup","value":"Warmup"},{"label":"Yoga","value":"Yoga"},{"label":"Gym","value":"Gym"},{"label":"Cycling","value":"Cycling"},{"label":"Athletic","value":"Athletic"},{"label":"Sports","value":"Sports"},{"label":"Hiking","value":"Hiking"}],"scope":"global","used_in_product_listing":"0"}}' | 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": 10,
  "attributeGroupId": 40,
  "attributeCode": "format",
  "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":10,"attributeGroupId":40,"attributeCode":"format","sortOrder":30}' | json_pp -json_opt allow_nonref

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":"activity","default_frontend_label":"Activity","default_value":"","entity_type_id":"4","frontend_input":"multiselect","is_filterable":true,"is_filterable_in_grid":false,"is_filterable_in_search":false,"is_required":false,"is_searchable":"0","is_visible_in_advanced_search":"0","is_visible_on_front":"1","options":[{"label":"Running","value":"Running"},{"label":"Warmup","value":"Warmup"},{"label":"Yoga","value":"Yoga"},{"label":"Gym","value":"Gym"},{"label":"Cycling","value":"Cycling"},{"label":"Athletic","value":"Athletic"},{"label":"Sports","value":"Sports"},{"label":"Hiking","value":"Hiking"}],"scope":"global","used_in_product_listing":"0"}}' | 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": 10,
  "attributeGroupId": 40,
  "attributeCode": "activity",
  "sortOrder": 40
}

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":10,"attributeGroupId":40,"attributeCode":"activity","sortOrder":40}' | json_pp -json_opt allow_nonref

Create categories

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

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

Payload

{
	"category": {
		"parent_id": 2,
		"name": "Training",
		"is_active": true,
		"level": 2,
		"include_in_menu": true,
		"custom_attributes": [{
			"attribute_code": "is_anchor",
			"value": "1"
		}, {
			"attribute_code": "url_key",
			"value": "training"
		}, {
			"attribute_code": "url_path",
			"value": "train"
		}]
	}
}

Response
Full information about the category that was created

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":"Training","is_active":true,"level":2,"include_in_menu":true,"custom_attributes":[{"attribute_code":"is_anchor","value":"1"},{"attribute_code":"url_key","value":"training"},{"attribute_code":"url_path","value":"training"}]}}' | json_pp | grep -i '"id"'

Create category Video Download

Payload

{
	"category": {
		"parent_id": 6,
		"name": "Video Download",
		"is_active": true,
		"level": 3,
		"include_in_menu": true,
		"custom_attributes": [{
			"attribute_code": "is_anchor",
			"value": "1"
		}, {
			"attribute_code": "url_key",
			"value": "video-download"
		}, {
			"attribute_code": "url_path",
			"value": "video-download"
		}]
	}
}

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":6,"name":"Video Download","is_active":true,"level":3,"include_in_menu":true,"custom_attributes":[{"attribute_code":"is_anchor","value":"1"},{"attribute_code":"url_key","value":"video-download"},{"attribute_code":"url_path","value":"video-download"}]}}' | 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

Response

Complete cURL request sample

wget https://magento.com/sites/all/themes/mag_redesign/images/magento-adobe-logo.svg
base64_encode() {   php -r 'print(base64_encode(file_get_contents($argv[1])));' $1; }
file_data=$(base64_encode $PWD/magento-adobe-logo.svg)
file_data=${file_data////\\/}
echo $file_data

curl -X POST "$endpoint/test/V1/products" -H "Content-Type: application/json" -H "Authorization: Bearer $admin_token" -d '{"product":{"sku":"sku-test-product-downloadable3","name":"sku-test-product-downloadable3","type_id":"downloadable","price":10,"attribute_set_id":10,"extension_attributes":{"category_links":[{"position":0,"category_id":"7"}],"downloadable_product_links":[{"title":"link1","sort_order":10,"is_shareable":1,"price":2,"number_of_downloads":0,"link_type":"file","link_file_content":{"name":"link1_content.svg","file_data":"'"$file_data"'"},"sample_type":"file","sample_file_content":{"name":"link1_sample.svg","file_data":"'"$file_data"'"}},{"title":"link2","sort_order":20,"is_shareable":0,"price":3,"number_of_downloads":100,"link_type":"url","link_url":"http://www.example.com/link2.jpg","sample_type":"url","sample_url":"http://www.example.com/link2.jpg"}],"downloadable_product_samples":[{"title":"sample1","sort_order":10,"sample_type":"url","sample_url":"http://www.example.com/sample1.jpg"},{"title":"sample2","sort_order":20,"sample_type":"file","sample_file_content":{"name":"sample2.svg","file_data":"'"$file_data"'"}}]},"custom_attributes": [{"attribute_code":"format","value":"9"},{"attribute_code":"activity","value":"14,16,17"}]}}'

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