WooCommerce REST API: Create Product with Images

I am starting to have more and more tutorials about REST API on my blog which is for sure because I am doing some work with my crossposting plugin.

Today is another one where I am going to share with you a simple code example how you can add Product image and Product gallery images when creating a product with REST API.

Just in case I mean these things:

WooCommerce product image and gallery images
You can find this if you go and edit any WooCommerce product.

Either you create a product with /wc/v3/products/ or update and existing one with /wc/v3/products/<id> endpoint, in both cases you can pass an array of images to the body of the request. Let’s look at the example and then I am going to explain it to you in details.

$request = wp_remote_request(
	'https://your-website/wp-json/wc/v3/products',
	array(
		'method' => 'POST',
		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$key:$secret" )
		),
		'body' => array(
			'name' => 'Test Product 123',
			'regular_price' => '2100.99',
			'images' => array(
				// image 1
				array( // the first one is for Product image (Featured image)
					'id' => 5,
				),
				// image 2
				array(
					'id' => 5491,
				),
				// image 3
				array(
					'src' => 'https://rudrastyh.com/wp-content/uploads/2022/12/at-third-place.jpg',
					'name' => 'Misha is working in Athens',
					'alt' => 'Misha Rudrastyh is working in Athens',
				)
			)
		)
	)
);

Some notes related to the code above:

One more thing – it is kind of strict API request, so if you provide something wrong into images array, then the product is not going to be created at all. So always double check that you’re providing correct image IDs or URLs.

Below are the common errors:

CodeError
woocommerce_product_invalid_image_id#123 is an invalid image ID.
woocommerce_product_image_upload_errorError getting remote image <URL>. Error: Not Found
woocommerce_product_image_upload_errorError getting remote image <URL>. Error: A valid URL was not provided.
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X