Libraries (Official) to Work with WooCommerce REST API and Examples

In the examples across my blog I almost always use the HTTP API when create REST API requests to WooCommerce. I do that because my plugins and especially the crossposting plugin uses HTTP API. You would probably agree with me that it is not always the point to include the whole PHP library inside your custom plugin.

But guys, WooCommerce has the amazingly convenient libraries to work with its REST API which you can use on your projects! In this guide we’re going to talk about PHP and JavaScript libraries (but there are also Python and Ruby ones).

How to Use a PHP wrapper for the WooCommerce REST API

First of all, here is the link to the official GitHub repository.

Then you can either install it using composer composer require automattic/woocommerce or just download the files from GitHub and copy it to your includes or whatever folder.

Example 1. Get products

// Include the PHP library
// In case you're not using Composer
require __DIR__ . '/includes/WooCommerce/Client.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/BasicAuth.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/HttpClient.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/HttpClientException.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/OAuth.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/Options.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/Request.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/Response.php';
// In case you're using Composer
// require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

// Authentication
$woo_api = new Client( $store_url, $username, $pwd, array( 'version' => 'wc/v3' ) );

$products = $woo_api->get(
	'products', 
	array( 
		'per_page' => 100 
	) 
);

// print_r( $products );

The example should be pretty straightforward but just in case:

  • If you decided not to use PHP Composer on your project you need to manually include all the files from the WooCommerce library, otherwise be ready to get a 500 error, something like: “Fatal error: Uncaught Error: Class “Automattic\WooCommerce\HttpClient\HttpClient” not found”.
  • Do not forget to provide variables $store_url, $username and $pwd, feel free to use application passwords for this purpose.
  • You can also use $woo_api->post(), $woo_api->put(), $woo_api->delete() and $woo_api->options() depending on a request type.

Example 2. Create a product

You can also check the example how I am doing it using WordPress HTTP API.

// Include the PHP library
// In case you're not using Composer
require __DIR__ . '/includes/WooCommerce/Client.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/BasicAuth.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/HttpClient.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/HttpClientException.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/OAuth.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/Options.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/Request.php';
require __DIR__ . '/includes/WooCommerce/HttpClient/Response.php';
// In case you're using Composer
// require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

// Authentication
$woo_api = new Client( $store_url, $username, $pwd, array( 'version' => 'wc/v3' ) );

// Creating a product with WooCommerce REST API
try {

	$new_product = $woo_api->post(
		'products',
		array(
			'name' => 'Misha Test Product',
    		'type' => 'simple',
			'sku' => 'misha-test-product',
    		'regular_price' => '23.00',
    		'description' => 'Some test product description.',
		)
	);

	// print_r( $new_product );

} catch( Exception $error ) {
	echo $error->getMessage();
}

In this code I am processing errors using try / catch, because if for example you try to create a product with the SKU which already exists on the store, the PHP library will throw a fatal error back at you.

As a result we will have our product on the “Store 2”:

Create a product with WooCommerce REST API PHP library

Of course all the other possible examples you can find in the WooCommerce API docs.

Working with WooCommerce REST API in JavaScript

First of all you need to install npm install --save @woocommerce/woocommerce-rest-api.

Sometimes you may run into an error in the terminal: “Error: Can’t resolve ‘stream’ in…” and “BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.” Can be fixed quite easily by adding the following lines into webpack.config.js:

module.exports = {
	
	// ...
		
	resolve: { fallback: { stream: false } }
}

Example 3. Get WooCommerce products in JavaScript

import WooRestApi from "@woocommerce/woocommerce-rest-api"

// Authentication
const Woo = new WooRestApi( {
	url: url,
	consumerKey: username,
	consumerSecret: pwd,
	version: "wc/v3"
} )

// Get products and print them in the browser console
Woo.get( 'products', { per_page: 100 } )
	.then( ( response ) => {
		console.log( response.data )
	} ).catch( ( error ) => {
		console.log( error.response.data )
	} )

Do not forget to provide variables url, username and pwd, you don’t need to worry about consumer key and consumer secret, just feel free to use application passwords for this purpose.

After that if you take a look at your browser console, there is going to be something like:

How to get WooCommerce products using JavaScript API.

Example 4. Create a WooCommerce product in JavaScript

import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api"

// Authentication
const Woo = new WooCommerceRestApi( {
	url: url,
	consumerKey: username,
	consumerSecret: pwd,
	version: "wc/v3"
} )

const productData = {
	name: 'Misha Test Product',
	type: 'simple',
	sku: 'misha-test-product',
	regular_price: '23.00',
	description: 'Some test product description.',
}

Woo.post( 'products', productData )
	.then( ( response ) => {
		console.log( response.data )
	} )
	.catch( ( error ) => {
		console.log( error.response.data )
	} )

If product is created successfully, you can find the product object in the console:

Create a WooCommerce product using JavaScript API

Otherwise an error is going to look like this:

WooCommerce API error when creating a product via JavaScript
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