Open In App

How to Connect Node.js to Woocommerce API ?

Last Updated : 26 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

WooCommerce is one of the most popular e-commerce platforms available, powering over 30% of all online stores. It is built on top of WordPress and provides a powerful API for developers to interact with their store data programmatically. If you are building a Node.js application that interacts with a WooCommerce store, you can use the WooCommerce API to easily retrieve, create, update, and delete data. In this article, we will discuss how to connect Node.js to the WooCommerce API and perform CRUD operations.

Connecting Node.js to WooCommerce is a common task for developers who are building e-commerce applications. The WooCommerce API provides a powerful and flexible way to interact with your store data programmatically. In this article, we will walk through the steps to connect Node.js to the WooCommerce API and perform CRUD operations, such as retrieving products, creating orders, updating customer information, and more. We will also add comments to the code to help explain what each line does.

This tutorial will show how to connect your node js application to the woocommerce API.  You must be familiar with the fundamentals of javascript and node js to follow this course. 

Step 1:  Get your API credentials by logging in to your dashboard and going to WooCommerce > Settings > Advanced > REST API.

 

Note: Before starting this you should have your API credentials to access the woocommerce API.

Step 2: Install the WooCommerce API package.

To connect to the WooCommerce API, we need to install the @woocommerce/woocommerce-rest-api package.

npm install @woocommerce/woocommerce-rest-api

Step 3: Create a woocommerce object and pass the API key into it. 

Javascript




// Require the WooCommerce API package
  
const WooCommerceRestApi = 
    require("@woocommerce/woocommerce-rest-api").default;
  
// Create a new instance of the WooCommerce API
const api = new WooCommerceRestApi({
    url: "https://yourstore.com",
  
     // Enter your api key
    consumerKey: 'your_consumer_key',
  
    // Enter your secret given by woocommerce
    consumerSecret: 'your_consumer_secret',
    version: "wc/v3" // Set the API version
});
module.exports = api;


Make sure to replace ‘yourstore.com‘, ‘ your_consumer_key‘, and ‘your_consumer_secret‘ with your own values.

Step 4: Make a request to the WooCommerce API. Now we can make API calls to the woocommerce API.

For Example – we can retrieve a list of products from woocommerce.

Javascript




// Make a request to the WooCommerce API to 
// retrieve a list of products
  
api.get("products")
    .then((response) => {
  
        // Log the response data to the console
        console.log(response.data);
    })
    .catch((error) => {
  
        // Log the error response data to the console
        console.log(error.response.data); 
    });


Step 5: Rather than retrieving the data we can perform several other operations like creating, updating, and deleting data using the WooCommerce API. 

For example, we can create a new product:

Javascript




// Create a new product
const productData = {
    name: 'New Product',
    regular_price: '10.00',
};
  
api.post('products', productData)
    .then((response) => {
  
        // Log the response data to the console
        console.log(response.data);
    })
    .catch((error) => {
  
        // Log the error response data to the console
        console.log(error.response.data);
    });


Output:

 

This code sends a POST request to the ‘/wp-json/wc/v3/products endpoint of the WooCommerce API with the product data object as the request body.

The final code looks like this:

Javascript




const WooCommerceRestApi = 
    require("@woocommerce/woocommerce-rest-api").default;
  
// Create a new instance of the WooCommerce API
const api = new WooCommerceRestApi({
    url: "https://yourstore.com",
  
    // Enter your api key
    consumerKey: 'your_consumer_key',
  
    // Enter your secret given by woocommerce
    consumerSecret: 'your_consumer_secret'
    version: "wc/v3" // Set the API version
});
  
// Make a request to the WooCommerce API to
// retrieve a list of products
api.get("products")
    .then((response) => {
  
        // Log the response data to the console
        console.log(response.data); 
    })
    .catch((error) => {
  
        // Log the error response data to the console
        console.log(error.response.data); 
    });
  
// Create a new product
const productData = {
    name: 'New Product',
    regular_price: '10.00',
};
  
api.post('products', productData)
    .then((response) => {
  
        // Log the response data to the console
        console.log(response.data); 
    })
    .catch((error) => {
  
        // Log the error response data to the console
        console.log(error.response.data); 
    });


Similarly, we can update and delete data by sending PUT and DELETE requests, respectively.

Conclusion

In this article, we discussed how to connect Node.js to the WooCommerce API and perform CRUD operations. By following the steps outlined in this article, you can easily retrieve, create, update, and delete data from your WooCommerce store using Node.js. The WooCommerce API provides a powerful and flexible way to interact with your store data.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads