This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Cloudways Copilot is here. Get Access to your Intelligent Hosting Assistant Today! Learn More

WooCommerce REST API: Integration, Management, and Troubleshooting Guide

Updated on October 2, 2024

16 Min Read

Running an online store often involves high costs and a lot of manual work, like regularly updating inventory or processing orders. The WooCommerce REST API helps reduce these tasks by allowing you to manage your store more efficiently through code.

With the API, you can automate processes, connect your store with other tools, and handle routine tasks without doing them manually. From updating product details to managing orders, the REST API makes it easy to perform these actions through simple HTTP requests.

In this blog post, I’ll explain how to integrate the WooCommerce REST API, test it, and use it to manage products. I’ll also cover the troubleshooting steps if you run into any issues.

What Is REST API, and How Does It Work?

A REST API is a way for software applications to communicate with each other over the Internet. It stands for Representational State Transfer and follows a set of guidelines to make integrations simple and scalable.

When a client requests information from a server using a REST API, the server sends back the current state of the requested resource in a standardized format. REST APIs use HTTP requests to perform standard database functions like CRUD records within a resource.

REST is preferred over other technologies like SOAP because it uses less bandwidth and is more suitable for internet usage.

Cloudways WooCommerce Hosting = Zero Downtime

Cloudways ensures uninterrupted service through cutting-edge technologies like NGINX, Apache, MySQL/MariaDB, delivering a seamless experience for optimal store performance.

Advantages of Using REST API in WooCommerce

REST API provides several advantages when used in WooCommerce. Some of the benefits include:

  • REST API allows for interoperability between multiple platforms.
  • It provides flexibility in application development.
  • It enables streamlined connectivity with different devices.
  • REST API allows for effortless data synchronization.
  • It makes it easy to access third-party content.

Requirements for WooCommerce REST API

To use the WooCommerce REST API, ensure the following:

  • WooCommerce 3.5+
  • WordPress 4.4+
  • Pretty permalinks enabled (Settings > Permalinks)
  • HTTPS for secure API access (recommended)
  • Postman or Insomnia for testing the WooCommerce REST API

For this tutorial, I’ll be using the latest versions of WooCommerce and WordPress. Make sure your WordPress permalinks are human-readable (Settings > Permalinks). While I’ll be using the “Post Name” option, you can select any format except “Plain” for better compatibility.

  • Go to Dashboard → Settings → Permalinks.
  • Select the Post Name option, and click the “Save Changes” button at the bottom.

How to Integrate the WooCommerce REST API?

APIs are tools that enable different software applications to communicate with each other. For example, a payment gateway like PayPal connects to your online store through APIs to securely process transactions without you building the payment functionality yourself.

The WooCommerce REST API lets you read and write data in your store, including orders, products, and customers. If you run an online clothing store, for instance, you can automate inventory updates through the API when a product sells out.

To start using the WooCommerce API, follow these steps:

Step #1: Set Up and Access the WooCommerce API

  • Once your WooCommerce site is ready, you need to generate API keys to authenticate API requests. To do this, navigate to the WooCommerce settings and click the Advanced tab, followed by REST API.

  • From here, you can generate keys for each user needing API access.

  • Now, you need to choose an API endpoint. WooCommerce provides several endpoints for accessing different data types, such as orders, products, customers, and more. Choose the endpoint that corresponds to the data you want to access.

So far, we’ve ensured WooCommerce is installed, API keys are created, and the correct endpoint is selected to access or modify data in the store.

Step #2: Creating API Keys in WooCommerce

In step 1, we set up WooCommerce and prepared the API environment. Now, in step 2, we need to generate API keys. This step is crucial because the API keys will authenticate any requests made to your store’s data—whether you’re retrieving orders, updating products, or managing customers.

Creating API keys in WooCommerce is a simple process that involves just a few steps. Here’s how to do it:

  • Log in to your WordPress dashboard and navigate to WooCommerce → Settings.

  • Click on the Advanced tab and then click on the REST API tab.

  • Click the Add Key/Create an API Key button to create a new API key.

  • Enter a description for the API key in the Description field.
  • Select the user from the User dropdown list to associate with the API key. If you don’t see the user you want to use, you may need to create a new one.
  • Choose the permissions you want to grant the API key from the Permissions dropdown list. You can choose between Read/Write, Read-only, and Write-only permissions.

  • Click the Generate API Key button to generate the API key.

  • Make sure to copy the Consumer Key and Consumer Secret values and save them somewhere secure, as you won’t be able to view them again.

And that’s it! You’ve now created API keys for your WooCommerce store, which can be used to access and interact with your store’s data programmatically.

Step #3: Testing if Rest API Works in WooCommerce

Next, we need to test whether our REST API is working correctly. Follow the steps below to validate the functionality of your REST API endpoints.

Enable the Legacy Rest API

The first step is to enable the legacy REST API. Prior to WooCommerce 9.0, you could go to Settings → Advance in WooCommerce to enable the Legacy API option. However, as of now, you’d need to install a dedicated extension called WooCommerce Legacy REST API.

Now when you go back to the Legacy API option, you’ll see a checkmark next to “The legacy REST API is enabled “.

The next step is to test it on the API Platform. I’ll test the REST API on two API testing platforms: Postman and Insomnia.

Test WooCommerce REST API on Postman

  • Postman is an API platform for building and using APIs. To test the API on Postman, you need to sign up first.
  • The requested URL we’ll test is wp-json/wc/v3/orders. For my website, the full URL looks something like this: https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/orders. Modify this to use your own site URL.
  • Once you successfully log in to Postman, follow the steps below to perform the REST API testing.
    • First, open the WooCommerce REST API document on the new tab.
    • Second, open the Postman on the other tab on your browser.
    • Go to your WooCommerce REST API document and search for the Orders.
    • Copy the code from the right side panel. Check the screenshot below:

To make it easier for you to follow this tutorial, here is the code you’re supposed to copy:

curl -X POST https://example.com/wp-json/wc/v3/orders \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "payment_method": "bacs",
  "payment_method_title": "Direct Bank Transfer",
  "set_paid": true,
  "billing": {
    "first_name": "John",
    "last_name": "Doe",
    "address_1": "969 Market",
    "address_2": "",
    "city": "San Francisco",
    "state": "CA",
    "postcode": "94103",
    "country": "US",
    "email": "[email protected]",
    "phone": "(555) 555-5555"
  },
  "shipping": {
    "first_name": "John",
    "last_name": "Doe",
    "address_1": "969 Market",
    "address_2": "",
    "city": "San Francisco",
    "state": "CA",
    "postcode": "94103",
    "country": "US"
  },
  "line_items": [
    {
      "product_id": 93,
      "quantity": 2
    },
    {
      "product_id": 22,
      "variation_id": 23,
      "quantity": 1
    }
  ],
  "shipping_lines": [
    {
      "method_id": "flat_rate",
      "method_title": "Flat Rate",
      "total": "10.00"
    }
  ]
}'
  • Once you’ve copied the code, go back to the Postman tab.
  • Click on the Collections and then Import.

  • Paste the order code that you copied from the WooCommerce REST API document and click on the Import Into Collection button.

  • Select the Request Type as GET.

  • Edit the Request URL to use your own site URL.

  • Now select the Auth Type. For this, choose Basic Auth.
  • Enter your Consumer Key and Consumer Secret Key from WooCommerce into the Username and Password fields. This is what you saved at the end of step#2, if you remember. Lastly, click on the “Send” button.

  • If you have set up your WooCommerce store properly and have enabled the REST API and generated API keys, you should see a JSON response that contains a list of products in your store. The response should look something like this:

Test WooCommerce REST API on Insomnia

The second platform that I’ll use to test REST APIs is Insomnia. To do this, you first need to install Insomnia on your system. Once it is installed, follow the below steps to perform REST API testing.

  • Click on New Collection.

  • Give a name to your collection.

  • Click on New HTTP Request.

  • Select the Request Type as GET.

  • Edit the Request URL to use your own site URL.

  • For Authorization, choose Basic Auth.

  • Enter your Consumer Key and Consumer Secret Key from WooCommerce into the Username and Password fields.

  • Click on the Send button. See the results in the JSON format at the right panel.

If you get a response like this, congratulations! Your REST API is working properly. If you encounter an error, make sure to double-check your API credentials and that you have properly enabled the REST API in WooCommerce.

How WooCommerce REST API Works?

The WooCommerce REST API is based on the principles of REST (Representational State Transfer). It allows you to interact with store data (such as orders, products, and customers) using standard HTTP methods:

  • GET to retrieve data (e.g., fetching a list of orders)
  • POST to create new data (e.g., adding a new product)
  • PUT to update existing data (e.g., changing order status)
  • DELETE to remove data (e.g., deleting a customer)

Each interaction is done through endpoints, which are structured URLs representing specific resources. For example, to retrieve an order, you would use the endpoint /wp-json/wc/v3/orders/{order_id}. These endpoints use a resource-based structure, meaning each URL represents a specific type of data in the system.

The API operates on stateless communication, meaning each request from a client to the server must contain all the information the server needs to fulfill that request.

Data is usually exchanged in JSON format, making it easy to handle in web applications. Each request to the API must be authenticated, typically using API keys (which can be generated within WooCommerce) or other methods like OAuth.

The API supports various tools for testing and integration, such as Postman and Insomnia, which allow you to easily send requests and view responses.

For most users, integration with external services is simple and typically involves generating API keys, which are shared with the external service to establish a connection. Once connected, the external service can interact with your WooCommerce store using the provided API.

Managed WooCommerce Hosting For Blazing Fast Load Times

Our optimized tech stack with NGINX, Apache, MySQL/MariaDB, and more is specifically created for WooCommerce stores that want nothing but the best performance.

How to Manage Products via the REST API (Add/Edit/Delete)

In the last section, we tested the REST API on our WooCommerce with Postman and Insomnia. Now we can actually use it to manage store products.

WooCommerce uses product objects to manage products in your store. These objects have various attributes, such as price, stock_status, and grouped_products, which allow you to easily control things like stock levels, pricing, and product categorization through the API.

For example, to assign a product to a group, just use the group’s ID with the grouped_products attribute.

When managing inventory, there are two key attributes:

  • manage_stock: Set to true or false to enable or disable stock management.
  • stock_status: Options include instock, onbackorder, or outofstock to reflect the current status of a product.

Let’s walk through how to add or update product details programmatically.

Add Products With WooCommerce API

To add a product to our store using Postman, we’ll switch from the GET request method to POST. And change our endpoint URL to: https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/products

First, go to the Body tab, select Raw, and choose JSON as your format. Then, replace the existing code with the following JSON payload:

{
  "name": "Macbook Pro M3",
  "type": "simple",
  "description": "The M3 chip features 8 CPU cores, 10 GPU cores, and a 16-core neural engine for enhanced performance.",
  "short_description": "",
  "categories": [],
  "tags": [],
  "images": []
}

This JSON contains product details, such as the name, type, description, and optional fields like categories, tags, and images.

Once you’ve entered the payload, click the Send button. If the product is created successfully, you’ll receive a response with the product object.

Here’s the product added to our store:

Edit a Product With WooCommerce API

Let’s say you’re not happy with the product you added earlier and want to edit it—you can do that, too, with WooCommerce API. To do this, I’ll need the “ID” of the product we added earlier to our store. In my case, it was “id”: 28.

For this tutorial, we’ll change the name of the product we added. To do this, you just need to:

Modify the JSON like so:

{
  "name": "Macbook Air M3"
}

Notice that we didn’t include any other attributes since we just wanted to change the product name. And since we don’t want to add another product and just need to modify it, we’ll change the endpoint URL to:

https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/products<your-id-here>.

The ID in my case is 28, so the URL will look like this:

https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/products/28.

Next, change the request method to PUT and run the request. You can confirm the update was successful by checking the updated product name on your store.

Here’s the product name updated on our store:

Delete a Product With WooCommerce API

To delete a product, we’ll use the DELETE request method. We’ll now delete the Macbook Air M3 – Updated product we upted on our store. To do this, you need to:

  • Use the same endpoint URL as earlie but change the request type to DELETE. 
  • Now, with the product ID mentioned in endpoint URL, click Send.

The product will be deleted from your store like so:

How to Manage Orders With REST API (Fetch/Update Order Status)

The WooCommerce REST API streamlines order processing by automating tasks like retrieving and updating order statuses. You can easily:

  • Fetch orders and filter them by date, status, or customer.
  • Update order statuses to reflect different stages, such as “completed.”
  • Manage refunds, including handling requests and processing them automatically.

Now, let’s see how to fetch recent orders and update order status to “completed.”

Fetch Orders

For this example, we’ll fetch a recent order and then update its status to “completed”. To fetch a recent order, we need a date range, obviously, and we need to use the GET request.

Here’s how I can pull up a recent order using Postman:

  • First, I’ll update the endpoint URL to https:///wp-json/wc/v3/orders. My URL will look like this: https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/orders
  • Next, since I need to fetch a recent order ID, I’ll provide a date range and use the “after” parameter to only fetch orders after today’s date. In my case, it will be 2024-09-08T00:00:00. My URL will look like this: https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/orders?after=2024-09-08T00:00:00.

  • After modifying the URL, go to the Params tab and add the date in the Value field, as shown in the screenshot below:

  • Run the request, and you’ll see a list of recent orders from your store that match the date criteria provided.

In my case, the order ID is “id”:25, with a status of “pending.” Let’s change that to “completed”, shall we? 

Updating Order Status

To update the order status, we’ll use the PUT request method. You’ll also need to modify the endpoint URL to include the specific order ID. In this example, the order ID is 25, so the URL will be:

https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/orders/25

Next, go to the Body tab and add the following JSON:

{
  "status": "completed"
}

Running this request will update the order status to completed. Like so:

How to Manage Customers With REST API (Add/Retrieve Customers IDs)

Customer management and engagement become easier with the WooCommerce REST API.

Businesses can:

  • Create and update customer profiles.
  • Retrieve customer info like purchase history and preferences.
  • Use this data to personalize marketing, send targeted promotions, and recommend products.

This helps businesses better engage with their customers and encourage repeat purchases.

Adding a Customer

  • To add customers, you need to use the POST request method and change the endpoint URL to: https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/customers
  • Next, in the Body tab, use the following JSON to create a new customer:
{
  "email": "[email protected]",
  "first_name": "Abdul",
  "last_name": "Rehman",
  "role": "customer",
  "username": "abdul.rehman",
  "billing": {
    "first_name": "Abdul",
    "last_name": "Rehman",
    "company": "",
    "address_1": "123 Main Street",
    "address_2": "",
    "city": "Lahore",
    "state": "Punjab",
    "postcode": "54000",
    "country": "PK",
    "email": "[email protected]",
    "phone": "(555) 555-5555"
  },
  "shipping": {
    "first_name": "Abdul",
    "last_name": "Rehman",
    "company": "",
    "address_1": "123 Main Street",
    "address_2": "",
    "city": "Lahore",
    "state": "Punjab",
    "postcode": "54000",
    "country": "PK"
  }
}
  • Run the request, and you’ll receive a response with the customer details. Be sure to note the customer ID, as you’ll need it in the next section to retrieve the customer’s details.

As you can see, new customer information has been added, and the customer ID is “id”: 2.

Retrieving Customer Info by ID

Now, we’ll use the GET request method to retrieve the customer information we created earlier using its ID, which in my case is “2.”

We’ll just use the ID in our endpoint URL like so: https://<mydomain>/wp-json/wc/v3/customers/<customer-id>

My endpoint URL would look like this: https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/customers/2

Running the request will show you the customer’s details like so:

Available Endpoints in WooCommerce REST API

The WooCommerce REST API gives you access to different parts of your store through endpoints, which are specific URLs for various store functions. For example, with the /wp-json/wc/v3/products endpoint, you can retrieve, create, or update products in your store.

Here’s a table of key endpoints along with example PHP code snippets to demonstrate their usage:

Endpoint Functionality Example Code
Products (/wp-json/wc/v3/products) Create, edit, delete, and retrieve product information. GET https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/products
Orders (/wp-json/wc/v3/orders) Access and manage order details (process, fulfill, refund). GET https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/orders
Customers (/wp-json/wc/v3/customers) Create, edit, and retrieve customer information. POST https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/customers
Coupons (/wp-json/wc/v3/coupons) Develop, manage, and track discount coupons. POST https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/coupons
Taxes (/wp-json/wc/v3/taxes) Configure and manage tax settings, including new tax rates. GET https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/taxes
Statuses (/wp-json/wc/v3/statuses) Access and manage order and product statuses. GET https://woocommerce-1146547-3986789.cloudwaysapps.com/wp-json/wc/v3/statuses/order

Fixing Common Issues of REST API

While REST API is a powerful tool for building robust and scalable applications, it has challenges.

1. SSL Verification

If you get connecting errors like Could not get any response on your localhost, you must disable SSL Verification. You can disable it from Postman platform settings.

Go to Application → Preferences in Insomnia and uncheck the validate certificates during authentication.

2. 401 Unauthorized Error

401 Unauthorized response status codes are server-related errors. Make sure that the URL is typed correctly. Make sure to place the Consumer Key and Consumer Secret properly.

The reason can be your API keys, or signature needs to be corrected. Reach out to the service provider of the endpoint you are trying to call.

3. Consumer Key Missing

Sometimes, servers may not properly pass the Authorization header, leading to an error message stating, “Consumer key is missing”, especially when authenticating over SSL.

If you encounter this issue, an alternative solution is to provide the consumer key and secret as query string parameters instead.

For example, you can include them in the URL like this:

https://example.com/wp-json/wc/v2/orders?consumer_key=XXXX&consumer_secret=XXXX.

4. Plugin Conflicts

Errors in the WooCommerce REST API can happen because of conflicts with other plugins. To troubleshoot, first, deactivate all plugins except for WooCommerce. If the error goes away, reactivate the plugins one at a time to find which one is causing the problem.

After identifying the conflicting plugin, check its documentation for any known issues with the REST API and follow any suggested solutions.

5. Server-Side Errors

Server-related issues can also lead to API errors. Start by looking at your server’s error logs for information about limits or PHP problems. Make sure your server is using at latest PHP 8.3, as older versions may not work well. If you encounter memory errors, consider increasing the PHP memory limit to handle larger requests better.

Organized Stores + Fast Hosting = Happy Customers and More $$$!

Cloudways’ optimized WooCommerce stack boosts your store’s performance and security, ensuring a seamless shopping experience for your customers.

Summary

To sum it up, the WooCommerce REST API is a great tool for anyone who wants to improve their online business. It lets you connect your store to other applications and automate your workflow. This means you can save time and effort by streamlining your processes.

Before you start, ensure you meet the requirements and generate your API keys. This will give you access to the REST API and all its features. Also, having a reliable hosting WooCommerce setup can enhance the API’s performance, ensuring smooth and efficient operations.

Q1. Does WooCommerce use REST API?

Yes, WooCommerce uses a REST API, which allows developers to interact with store data like products, orders, and customers programmatically.

Q2. What is the limit of REST API in WooCommerce?

The WooCommerce REST API has limits on the number of requests you can make in a given time frame to prevent abuse. Typically, it’s set to around 100 requests per second per IP, but this can vary based on your hosting environment.

Q3. How to integrate API with WooCommerce?

To integrate the API with WooCommerce, you’ll need to generate API keys from your WooCommerce settings. Once you have the keys, you can use them in your applications or external services to make requests to your store’s API.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Abdul Rehman

Abdul is a tech-savvy, coffee-fueled, and creatively driven marketer who loves keeping up with the latest software updates and tech gadgets. He's also a skilled technical writer who can explain complex concepts simply for a broad audience. Abdul enjoys sharing his knowledge of the Cloud industry through user manuals, documentation, and blog posts.

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour