The complete guide to use WooCommerce REST API
WooCommerce is a powerful eCommerce solution for WordPress. Recently, WooCommerce communities are all buzzing nowadays with the new jargon - Rest API endpoints for custom development. Indeed, this is a tremendous advantage for any apps you may build for your online business, but firstly you need to understand the REST API and How to use WooCommerce REST API.
In this article, we will introduce you to REST APIs in an easy manner, as well as how important its role is for the future of WooCommerce.
So, what are you waiting for? Let’s begin.
What is the REST API?
While WooCommerce developers are excited about the Rest API, some WooCommerce users might not even be able to understand that. It is quite depressing for something which is considered the most outstanding update in WordPress history.
REST stands for Representational State Transfer and API stands for Application Programming Interface. The only purpose lies in its ability to establish connections between different devices within a single space or medium.
How it works
REST APIs provide interoperability among different online solutions. In other words, you can have a lot of data stored in a program such as WordPress, and interact with that same data in a totally separate application (such as native mobile or desktop).
How the REST API relates to WooCommerce
When REST and API work hand-in-hand to enhance the functionality and approachability of your WooCommerce website on WordPress, then it actually makes sense. In general, it is just an interface that developers can use to access the WooCommerce store from outside the WordPress installation itself in order to access the service or functionality.
The outstanding feature of Rest API is its Universality. The WooCommerce Rest API was designed with the thought to make your WooCommerce store on WordPress easily interact with other websites and services over the Internet.
How to use WooCommerce REST API
Step 1: Go to WooCommerce settings
Then, you select the “Advanced” section and choose the REST API term. Make sure the Enable the REST API checkbox is selected, and save your changes:
Step 2: You are taken to the Key Details screen
Next, select Keys/Apps so you can generate a unique API key that will serve as the secret password when connecting to the REST API. Now, please fill in the information it requires you:
-
Add a Description.
-
Select the User you would like to generate a key for in the dropdown.
-
Select a level of access for this API key — Read access, Write access or Read/Write access.
Then, click on Generate API Key, and WooCommerce creates API keys for that user. This key generated would act as your secret password while connecting to the REST API later.
Every application using the REST API is unique and requires different calls. Rather than learning every single REST API endpoint, you can use the dedicated documentation to find what you need in any given situation. This will also help you keep up as the API receives updates.
Here are a few highlights to note when reading:
-
Check the Requirements section to make sure you have the correct versions of WooCommerce and WordPress
-
Get familiar with JSON and JSONP, the primary method of data transfer
-
Learn the error codes to debug issues faster
-
Use the provided tools and resources for even easier integration
Using the documentation, you can see how easy it is to get different information from your installation.
Step 3: Check the information after the creation
Now that keys have been generated, you should see Consumer Key and Consumer Secret keys, a QRCode, and a Revoke API Key button.
Step 4: Test if REST API works
In Postman, you need to set the fields for request type, request URL, and the settings on the authorization tab. For authorization, choose basic auth and enter your consumer key and consumer secret keys from WooCommerce into the username and password fields.
The request URL WooCommerce will test as an instance is wp-json/wc/v2/orders
. On localhost the full URL may look something like this: https://local.wordpress.dev/wp-json/wc/v2/orders
. Modify this to use your own site URL. Once done, you’ll see the JSON response from the API if all works well. You should see something like this:
Insomnia is almost identical to Postman; fill in the same fields and again use basic auth.
That’s it! It shows that the API is working well.
Step 5: Enable legacy REST API
To enable the legacy REST API within WooCommerce, go to WooCommerce > Settings > Advanced > Legacy API and tick the Enable the legacy REST API checkbox.
Common connection issues
1. Connection issue with localhost and self-signed SSL
If you’re having problems connecting to the REST API on your localhost and seeing errors like this:
The thing that you need to do is disable SSL verification. In Postman you can find this in the settings:
Also in Insomnia has this setting the preferences area:
2. Consumer key is missing
Occasionally servers may not parse the Authorization header correctly (if you see a “Consumer key is missing” error when authenticating over SSL, you have a server issue).
In this case, you may provide the consumer key/secret as query string parameters instead.
3. 401 unauthorized
When your API keys or signature is wrong, make sure that:
-
The user you generated API keys for actually has access to those resources.
-
The username when authenticating is your consumer key.
-
The password when authenticating is your consumer secret.
-
Make a new set of keys to be sure.
If all of the above problems are too hard for you to figure out and fix, getting help in the WooCommerce help center is the best solution for you. The developers will welcome to deal with it one-to-one, along with you.
Final thought
In this article, we just showed you the detailed information relating to REST API - How use WooCommerce REST API. From what you have read, we hope that your practice works well.
We really appreciate it if you need our support while you activate the REST API in your WooCommerce account!