How to use the Woocommerce API

Learn how to get started using the Woocommerce API with examples of common GET and POST requests on your Woocommerce site.

The hardest part is getting started. In this post, we go over how to get started using the Woocommerce API. Below are examples of how to enable the API in Woocommerce, our preferred API client, and common GET and POST requests.

Step 1: Enable API in Woocommerce

Enable the API in Woocommerce. To do this go to Woocommerce > Settings > Advanced.

Next toggle the Legacy API tab.

Then, next click on the REST API and add user. For the sake of this tutorial, provide “Read/Write” access.

After generating your API keys, make sure to save both the consumer_key and conumser_secret in a safe place. If necessary, you always can regenerate your API consumer_key and consumer_secret again.

woocommerce-api-keys

 

Helpful tips

  • The consumer_key should start with “ck_”
  • The consumer_secret should start with “cs_”.

Step 2: Download Insomnia or other API client

After generating these API keys, go and download the API client Insomnia. See this link to download here: https://insomnia.rest/

After downloading Insominia, open the application and click “New Request”.

woocommerce-api-insomnia

Under the “Basic” tab dropdown, click on “Basic Auth” and enter following credentials:

woocommerce-api-basic-auth

username = consumer_key
password = consumer_secret.

To start, I always do a GET request just to make sure your consumer_key and consumer_key are working properly.

In the GET request, enter a test GET request: https://yoursite.com/wp-json/wc/v3/orders

woocomerce-api-get-request-orders

You should see all orders in your store.

Step 3: Woocommerce GET Requests

Orders

All orders
-https://yourdomain.com/wp-json/wc/v3/orders/

Single order
-https://yourdomain.com/wp-json/wc/v3/orders/[order id]

For example, the order ID is 3612, so I use https://yourdomain.com/wp-json/wc/v3/orders/100 to retrieve a specific order.

Products

All products
-https://yourdomain.com/wp-json/wc/v3/products/

Single product
-https://yourdomain.com/wp-json/wc/v3/products/833/

Variation product
-https://yourdomain.com/wp-json/wc/v3/products/833/variations/854

Step 4: Woocommerce POST Requests

Products

Updating price of simple product
-example url: https://yourdomain.com/wp-json/wc/v3/products/833/

To update the “price” or “regular_price”, here is a example POST request:

Click “Body” > “JSON” and enter this query

{
"regular_price": "81"
}

To update the “stock quantity” and “price”:

{
"regular_price": "81”,
“stock
}

To update meta_data:

{
"meta_data": [
{
"key": "_net_price",
"value": "40"
}
]
}

Pass the “key” as name of database field, and value as value.

Updating price of product variant:
-example url: https://yourdomain.com/wp-json/wc/v3/products/833/variations/854

To update the “price” or “regular_price”, here is a example POST request:

Click “Body” > “JSON” and enter this query

{
"regular_price": "81"
}

To update the “stock quantity” and “price”:

{
"regular_price": "81”,
“stock
}

To update meta_data:

{
"meta_data": [
{
"key": "_net_price",
"value": "40"
}
]
}

Pass the “key” as name of database field, and “value” as value.

Overall, if you have are looking for more examples, see the Woocommerce API docs at this link: https://woocommerce.github.io/woocommerce-rest-api-docs/