How to Connect Node.js to Woocommerce API ?
Last Updated :
28 Apr, 2025
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.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
REST API Introduction REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server.
7 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read