The WooCommerce API: A Quick Guide for Beginners
Table of Contents
- What is the WooCommerce API?
- Why does REST API matter for developers?
- What can you do with the API?
- How to access the WooCommerce API
- #1. Enable API access in WooCommerce
- #2. Add and generate API keys
- #3. Download an API client
- #4. Use your API keys to connect to your API client
- #5. Test it with a GET request
- #6. Test the PUT request
- Final thoughts
One of the cool things about WooCommerce is that it’s built on an open-source platform.
This means that you can work with the API to customize WooCommerce to your own needs, or even create something that can be used by many other WooCommerce sites (like we did with Checkout WC).
To get started on any sort of customization, you need to be familiar with the WooCommerce API, so here’s our quick, beginner’s guide:
What is the WooCommerce API?
WooCommerce is built for WordPress and as such, has a REST API. This means “Representational State Transfer” and allows for custom development, including interoperability between different online solutions.
What does “interoperability” mean? It means you can keep a lot of data stored somewhere like WordPress, but still interact with that data from a totally separate application. It used to be that you’d have to spend big money on developing custom APIs, but now REST APIs are in all up-to-date WordPress installations.
REST stands out among APIs because it works by manipulating textual data from one place to another without direct access to a database or user interface. It is delivered via HTTP endpoints using JavaScript Object Notation (JSON) formatting. The endpoints represent pages, posts and other types of WordPress content.
As an example, you can decouple your data from the WordPress API and use it to build native apps, or to automate data synchronization. Currently, the WordPress Rest API integration is on v3 – the table below taken from WooCommerce documentation shows API versions and their associated WooCommerce versions:
The REST API is a powerful tool, linking different platforms and languages Share on X
Why does REST API matter for developers?
The JSON formatting of the REST API allows WordPress to exchange data with any other websites or software – written in any programming language. This makes it extremely versatile for developers as they are not constrained to PHP anymore.
A key benefit is that by standardizing how applications interact with WordPress data, development on WordPress/WooCommerce will become easier and quicker for developers.
What can you do with the API?
The bottom line is you can do a lot with the API! The REST API was first introduced in 2013 and it was added to the WordPress core in 2015. It has powerful abilities to add features to WP or WooCommerce, although it often seems only advanced developers take full advantage of it.
The REST API can be integrated with themes, mobile applications, wearable technology, legacy systems and more. Some examples include:
- Develop apps based on your products/services by using the API to access internal infrastructure. (Here’s an example from Event Espresso).
- Give developers/other people the ability to build their own apps using a Developer API.
- Simmer does this for creating recipe apps.
- Syndicate content across multiple sites.
- Automate data synchronization (here’s an example from Wired).
- Create a custom WooCommerce dashboard (here’s a Cloudways example).
- Create, update or remove products in WooCommerce (see this example).
- All these examples here.
As you can tell, accessing and learning the REST API gives you flexibility and opens up new possibilities for customization and development. Next, we’ll take a look at how you can get started with the WooCommerce API…
How to access the WooCommerce API
Here are a few steps to get started with using the WooCommerce API:
#1. Enable API access in WooCommerce
This is done by logging in to the back end and going to WooCommerce>Settings>Advanced. You next go to the Legacy API tab and check the box for “enable the legacy REST API.”
#2. Add and generate API keys
First, click on the REST API tab to add a key. You’ll need to add a description, select a user and choose permissions (Read/write access is a good option to allow creating, reading, updating and deleting of WooCommerce data).
Click “generate API key” then copy your consumer key and consumer secret, to store somewhere securely. These are what you will use to connect to the WooCommerce API.
#3. Download an API client
An API client is a set of tools and protocols that helps you to bypass some operations while you are developing. It takes care of a lot of the low-level details, like making requests and handling responses, while you write your code. It’s a way to speed up development by not having to “reinvent the wheel” yourself.
Examples of API clients you can use for REST include Insomnia and Postman.
#4. Use your API keys to connect to your API client
For example, if you use Insomnia, you need to open the application and click on “new request.” Under the “basic tab” dropdown you’d click on “basic auth” then enter your consumer key and consumer secret in the following formats:
- Username = consumer key
- Password = consumer secret.
#5. Test it with a GET request
A GET request, where you request it to get all orders is a good way to test that your API key is working.
In your API client, select “get” from the dropdown and type in your web address like this: https://yourwebsite.com/wp-json/wc/v3/orders. It should start pulling through all the orders in your store. If you get a 401 “sorry you are not allowed to edit this resource” error, something is up and you need to troubleshoot – do some research on fixing WooCommerce REST API issues.
Other get request examples include:
- Pull through a particular order: https://yourcompany.com/wp-json/wc/v3/orders/{insert order ID}
- Pull through all products: https://yourcompany.com/wp-json/wc/v3/products/
- Pull through a single product: https://yourcompany.com/wp-json/wc/v3/products/{insert product ID}
- Pull through all your customers: https://yourcompany.com/wp-json/wc/v3/customers/
#6. Test the PUT request
A put request is a good way to test that you have both read and write access for your API keys. For example, are you able to update a single order from the API client? Here’s how you test that:
- Step 1. Do a Get request for a single order: https://yourcompany.com/wp-json/wc/v3/orders/{insert order ID}
- Step 2. Click the “get” dropdown and choose “put” instead. Choose JSON from the dropdown.
- Step 3. Update something about the order – for example, you might want to update the status from processing to complete. (Note: use some sort of “demo” order for this).
Final thoughts
This has been a quick beginner’s guide to the WooCommerce API and some basics of what you can do with it. One of the cool things is that it’s not difficult to get started and there are a lot of articles and papers out there with free information on how you can try different things with the API.
The great news is that the REST API has many more potential applications that developers are only just beginning to explore. It is a powerful tool and should lead to some interesting developments in the future.