API connection

Magento 2 API Create Products with Postman

Magento 2 supports REST API Integration, which lets you speed up getting, sending, and processing data, also transferring it to the third-party system. In this example, we will use Postman to connect with Magento API. Follow our guides to learn how to create simple products using Magento 2 API, those who are new to development knowledge still take in.

Step 1: Generate Access Token

To allow a web API request from a client, the Magento access token must be given on the line. As a result, the token serves as an electronic key that enables you to enter Magento 2 API. Complete the following steps to authorize access to resources:

Login to your Postman account, in the POST request, enter your store URL along with this endpoint.

POST <host>/rest/V1/integration/admin/token

magento admin api endpoint request

Enter your Magento admin username and password in the Body sections, then click Send.

Once you get the token in the response section, in Authorization:

  • Choose Type: Bearer Token
  • Then paste your token
enter access token postman

Postman will be able to access your Magento 2 API now.

Step 2: Prepare the product

Our example products below are based on Magento 2 default products, you will be able to get their attributes by API here.

  • The Body for creating a simple product and a configurable product is similar, with some exceptions:
    • The simple product SKU attaches the configurable option to the configurable product SKU.
    • The name shows the size.
    • The type_id is set to simple.
    • The visibility is set to 1, meaning that the simple product should not be displayed in the store.
    • The price and size attributes are specified.
  • Although it is not required, the simple product Body also contains stock-item information. By default, out-of-stock items are hidden, so adding stock will make the product visible on the website.

For example, we have a product with these characteristics:

  • Attribute Set Gear
  • Product Name Vintage Backpack
  • SKU VBP_001
  • Price 32
  • Tax Class Taxable Goods
  • Weight 30
  • Categories Gear, Bags
  • Visibility Not display on the store
  • Material Polyester
  • Pattern Graphic Print
  • Color Brown
  • Size Small
  • Description Vintage Backpack is a heavy weight washed cotton canvas backpack with a large main compartment, a front pocket, and two side pockets.

Step 3: Create a Simple Product

Create a new Post Request with this endpoint:

POST <host>/rest/V1/products

In the Body section enter this code

{
  "product": {
    "sku": "VBP_001",
    "name": "Vintage Backpack",
    "attribute_set_id": 9,
    "price": 32,
    "status": 1,
    "visibility": 1,
    "type_id": "simple",
    "weight": "30",
    "extension_attributes": {
    	"category_links": [
    		{
    			"position": 0,
    			"category_id": "11"
    		},
    		{
    			"position": 1,
    			"category_id": "12"
    		},
    		{
    			"position": 2,
    			"category_id": "16"
    		}
    	],
    	"stock_item": {
    		"qty": "100",
    		"is_in_stock": true
    	}
    },
    "custom_attributes": [
    	{
    		"attribute_code": "description",
    		"value": "Vintage Backpack is a heavy weight washed cotton canvas backpack with a large main compartment, a front pocket, and two side pockets."
    	},
    	{
    		"attribute_code": "tax_class_id",
    		"value": "2"
    	},
    	{
    		"attribute_code": "material",
    		"value": "189"
    	},
    	{
    		"attribute_code": "pattern",
    		"value": "166"
    	},
    	{
    		"attribute_code": "color",
    		"value": "42"
    	},
    	{
    		"attribute_code": "size",
    		"value": "20"
    	}
    ]
  }
}
product attribute setting with magento api

The Response section will result as below

{
    "id": 2132,
    "sku": "VBP_001",
    "name": "Vintage Backpack",
    "attribute_set_id": 9,
    "price": 32,
    "status": 1,
    "visibility": 1,
    "type_id": "simple",
    "created_at": "2020-09-17 10:40:03",
    "updated_at": "2020-09-17 10:40:28",
    "weight": 30,
    "extension_attributes": {
        "website_ids": [
            1
        ],
        "category_links": [
            {
                "position": 0,
                "category_id": "11"
            },
            {
                "position": 1,
                "category_id": "12"
            },
            {
                "position": 2,
                "category_id": "16"
            }
        ],
        "stock_item": {
            "item_id": 2132,
            "product_id": 2132,
            "stock_id": 1,
            "qty": 100,
            "is_in_stock": true,
            "is_qty_decimal": false,
            "show_default_notification_message": false,
            "use_config_min_qty": true,
            "min_qty": 0,
            "use_config_min_sale_qty": 1,
            "min_sale_qty": 1,
            "use_config_max_sale_qty": true,
            "max_sale_qty": 10000,
            "use_config_backorders": true,
            "backorders": 2,
            "use_config_notify_stock_qty": true,
            "notify_stock_qty": 1,
            "use_config_qty_increments": true,
            "qty_increments": 0,
            "use_config_enable_qty_inc": true,
            "enable_qty_increments": false,
            "use_config_manage_stock": true,
            "manage_stock": true,
            "low_stock_date": null,
            "is_decimal_divided": false,
            "stock_status_changed_auto": 0
        }
    },
    "product_links": [],
    "options": [],
    "media_gallery_entries": [],
    "tier_prices": [],
    "custom_attributes": [
        {
            "attribute_code": "options_container",
            "value": "container2"
        },
        {
            "attribute_code": "msrp_display_actual_price_type",
            "value": "0"
        },
        {
            "attribute_code": "url_key",
            "value": "vintage-backpack"
        },
        {
            "attribute_code": "required_options",
            "value": "0"
        },
        {
            "attribute_code": "has_options",
            "value": "0"
        },
        {
            "attribute_code": "tax_class_id",
            "value": "2"
        },
        {
            "attribute_code": "category_ids",
            "value": [
                "11",
                "12",
                "16"
            ]
        },
        {
            "attribute_code": "description",
            "value": "Vintage Backpack is a heavy weight washed cotton canvas backpack with a large main compartment, a front pocket, and two side pockets."
        },
        {
            "attribute_code": "material",
            "value": "189"
        },
        {
            "attribute_code": "size",
            "value": "20"
        },
        {
            "attribute_code": "pattern",
            "value": "166"
        },
        {
            "attribute_code": "color",
            "value": "42"
        }
    ]
}

product attribute response with magento api

Step 4: Check your product in Admin Panel and Stores

On the Admin sidebar, go to Catalog then choose Product

Search for your product

product catalog in magento admin

See how it all goes through in this video

Above are detailed examples for creating a simple product with Magento 2 API using Postman. For more Magento 2 REST API documentation you could refer here. If you have any questions feel free to leave a comment below! Visit our blogs for more useful guides.

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email