Magento 2 REST API: Part 2, Adding A Product…

Adding a product through the Magento 2 REST API is actually quite simple. They have all the parameters and attributes and other things that you’d want to set in the JSON request. You can even setup an image or video in media gallery.

	"product": {
		"sku": "ABC_123456",
		"name": "My Product Title Listing",
		"attributeSetId": 10,
		"price": 425.99,
		"status": 1,
		"visibility": 4,
		"typeId": "simple",
		"createdAt": "2016-05-26 14:00:00",
		"updatedAt": "2016-05-26 14:00:00",
		"weight": 15,
		"mediaGalleryEntries": [{
			"mediaType": "image",
			"label": "abc_123456_image",
			"position": 0,
			"disabled": false,
			"file": "",
			"content": {
				"base64EncodedData": "string",
				"type": "string",
				"name": "string"
	"saveOptions": true

Let’s talk about this a bit. Most of what you see is straight forward.

SKU/Name/Price, things like that are whatever you want.

attributeSetId is the ID of your created attribute set, or you can grab the default attribute set ID. If you don’t know where to find this, the easiest way would be to jump in the admin area, edit the attribute set, and the ID is in the URL.

typeId is for your product type. simple, bundled, etc. You can look these up in the admin area as well. Just create a product and get your options from there. You may have to inspect a select element in some cases of these attributes. What you see in the options list is what you put in here.

There are many more attributes you can set on this, including your custom attributes. For the purpose of this tutorial, I’m keeping things simple.

Here’s the PHP code to send that over. You can get the string after ‘bearer’ (HTTPHEADER) from Magento.

$productsJson = "build your json string to match the above examples";

$ch = curl_init( "" );

$curlOptions = array(
    CURLOPT_POSTFIELDS => $productsJson,
    CURLOPT_HTTPHEADER => array( "Content-type: application/json", "Authorization: bearer kalfjkkwjelfkjwelf" )

curl_setopt_array( $ch, $curlOptions );

$response = curl_exec( $ch );

That’s it! You can parse out your $response any old way you need. I hope this helps!


Written by Dan
Welcome to my blog! Here you'll find my collection of bible study lessons, book reviews, and other posts I feel inspired to write. I am a Christian currently serving at First Baptist Church of Northville in Northville, MI.