How to Connect Node.js to Woocommerce API ?
Last Updated :
24 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
How to Create API to View Logs in Node.js ?
Log files are essential for monitoring and troubleshooting applications. They provide insight into the applicationâs behaviour and help identify issues. In a Node.js application, you might want to create an API that allows you to view these logs easily. This can be particularly useful for centralize
5 min read
How To Connect Node with React?
To connect Node with React, we use React for the frontend (what users see) and Node.js for the backend (where the server logic lives). The frontend sends requests to the backend, and the backend responds with data or actions. There are many ways to connect React with Node.js, like using Axios or Fet
4 min read
How to Connect to Telnet Server from Node.js ?
Connecting to a Telnet server from Node.js involves creating a client that can communicate using the Telnet protocol. Telnet is a protocol used to establish a connection to a remote host to execute commands and transfer data. In Node.js, you can use various packages to facilitate the connection to a
4 min read
How to connect to an API in JavaScript ?
An API or Application Programming Interface is an intermediary which carries request/response data between the endpoints of a channel. We can visualize an analogy of API to that of a waiter in a restaurant. A typical waiter in a restaurant would welcome you and ask for your order. He/She confirms th
5 min read
How to Connect Node to a MongoDB Database ?
Connecting Node.js to MongoDB is a common task for backend developers working with NoSQL databases. MongoDB is a powerful, flexible, and scalable database that stores data in a JSON-like format. In this step-by-step guide, we'll walk through the entire process from setting up your development enviro
6 min read
How to Deploy Node Backend on Vercel ?
To deploy a Node.js backend to Vercel, you can follow the simple approach that involves setting up your Node.js project, configuring necessary files, and using the Vercel CLI. Vercel is known for its ease of use and automatic optimizations, making it an excellent choice for deploying full-stack appl
2 min read
How to Install Node.js on a Webserver ?
Node.js is a powerful, lightweight, and efficient runtime for executing JavaScript code on the server side. It is particularly well-suited for building scalable network applications. Installing Node.js on a web server enables developers to run server-side JavaScript, host web applications, and serve
2 min read
How to Use ChatGPT API in NodeJS?
ChatGPT is a very powerful chatbot by OpenAI that uses Natural Language Processing to interact like humans. It has become very popular among developers and is being widely used for some of its state-of-the-art features, like understanding and interpreting code, and even generating code based on text
4 min read
Node.js Connect Mysql with Node app
Node.js is a powerful platform for building server-side applications, and MySQL is a widely used relational database. Connecting these two can enable developers to build robust, data-driven applications. In this article, we'll explore how to connect a Node.js application with a MySQL database, cover
2 min read
How to Connect Mongodb Authentication by Node.js?
MongoDB is a popular NoSQL database that provides high performance, high availability, and easy scalability. In many applications, you need to connect to a MongoDB database with authentication to ensure data security. This article will guide you through the process of connecting to a MongoDB databas
3 min read