How To Use The WooCommerce API Without Knowing How to Code

WooCommerce
How To Use The WooCommerce API Without Knowing How to Code

Learn how to get started using the WooCommerce API.

Use the WooCommerce API to add, update, and delete orders, products, customers in your online store.

Later in the tutorial, I include plenty of examples of GET and PUT requests that you can copy or modify on your own for your own use.

Let’s begin!

Additional Resources

Step 1: Enable API access in WooCommerce

To enable the the WooCommerce API, login to the backend of your WordPress site, hover over WooCommerce > Settings > Advanced.

Next toggle the “Legacy API” tab > enable > Save Changes.

Step 2: Add a API keys with Read/Write access

Next, click on the “REST API” tab > add a key with your details.

woocommerce-api-key-details

For this tutorial, I recommend to give “Read/Write” access to test GET and PUT requests.

Once finished, click the Generate API key button to view your consumer_key and consumer_secret.

woocommerce-api-consumer-keys-secret

After generating your API keys, keep these keys in a safe place as we will be using them to connect to Insomnia, our API client.

Step 3: Download Insomnia or other API client

Now is the time to use our API keys to connect to our WooCommerce site with an API client.

Download Insomnia (not an affiliate link), the API client we will use to test GET and PUT requests. You could also download Postman.

Step 4: Use your API keys to connect to Insomnia

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

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

Next, enter your consumer_key and consumer_secret for the API keys you generated on your WooCommerce dashboard.

  • username = consumer_key
  • password = consumer_secret

Step 5: Make your first GET request

To start, I always do a GET request to get all orders just to make sure my API keys are working correctly.

In the GET request, enter a GET request to view all orders:

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

You should see all orders in your store.

Are you getting the 401 error “Sorry, you are not allowed to edit this resource” error? Check out this post: How to fix common WooCommerce REST API issues.

Step 6: Make your first PUT request

Similar to the GET request, I always do a PUT to update a single order to make sure my API keys are working correctly and I have both Read and Write access.

To update an order, type in a GET request to view a single order:

https://yourdomain.com/wp-json/wc/v3/orders/{insert order ID}

For my example, the GET request was the following URL:

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

Next, click the “GET” dropdown and choose “PUT” option

Next, go to the dropdown to choose “JSON”.

Use this example to update the order status from “processing” to “completed”

{
  "status": "completed"
}

Helpful tips

  • The consumer_key should start with “ck_”
  • The consumer_secret should start with “cs_”.
  • If necessary, you always can regenerate your API keys again.

WooCommerce GET request examples

If you are having retrieving any of these examples, make sure you scroll up to how to do GET requests.

GET request to view all orders

https://yourcompany.com/wp-json/wc/v3/orders/

GET request to view a single order

https://yourcompany.com/wp-json/wc/v3/orders/{insert order ID}

For example, the order ID is 82513, so I would use the following URL path to retrieve this order:

https://yourcompany.com/wp-json/wc/v3/orders/82513/

Pro tip: Find the order ID by going to clicking on the order and looking at number after “post=”. See the screenshot below for reference.

GET request to view all products

https://yourcompany.com/wp-json/wc/v3/products/

GET request to view a single product

https://yourcompany.com/wp-json/wc/v3/products/{insert product ID}

GET request to view a single product variation

https://yourcompany.com/wp-json/wc/v3/products/{insert product ID}/variations/{insert variation ID}

GET request to view all customers

https://yourcompany.com/wp-json/wc/v3/customers/

GET request to view a single customer

https://yourcompany.com/wp-json/wc/v3/customers/{insert customer ID}

WooCommerce PUT request examples

PUT request to update price of a simple product

To update the “regular_price”, here is a example PUT request:

https://yourcompany.com/wp-json/wc/v3/products/{insert product ID}

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

{
"regular_price": "81"
}

PUT request to update price and stock quantity

To update the “regular_price” and the “stock_quantity”, here is a example PUT request:

{
 "regular_price": "81”,
 “stock_quantity": 45
}

PUT request to update product meta_data

To update product meta data, here is a example PUT request:

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

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

PUT request to update price of product variant

example url: https://yourdomain.com/wp-json/wc/v3/products/833/variations/854

To update a product or product variant’s “price” or “regular_price”, here is a example PUT request:

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

{
 "regular_price": "81"
}

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

PUT request to update customer’s name

example url: https://yourdomain.com/wp-json/wc/v3/customers/3144/

  • Replace https://yourdomain.com with your domain
  • Replace 3144 with a specific customer ID in your store.
  • Find customer ID by clicking “edit” on the specific customer in WooCommerce and looking for the value after user_id=XXXX. In the example below, the customer ID is 3144

woocommerce-api-customer-id

To update the “first_name” and “last_name”, here is a example PUT request:

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

{
  "first_name": "John",
  "last_name": "Doe"
}

PUT request to update customer’s shipping address

To update a customer’s shipping address, here is a example PUT request:

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

{
  "shipping": {
    "first_name": "John",
    "last_name": "Doe",
    "company": "Demo Company",
    "address_1": "110 Demo Drive",
    "address_2": "Suite 110",
    "city": "Minneapolis",
    "state": "MN",
    "postcode": "94203",
    "country": "US"
  }
}

Helpful Tips and Additional Resources

If you have any questions or looking for more examples, let me know in the comments.

I will do my best to help in a timely manner and also add additional examples to this post for future readers.

Comments

  1. Wesley Townie April 21, 2019 | 8:08 pm

    Hey thanks Simon for all the help. Do you know how I would be able to update a customers shipping address with api?

    • Simon Gondeck April 21, 2019 | 9:47 pm

      Wesley – glad you enjoyed it! I added an example at the end of the tutorial on how to update a customer’s shipping address.

  2. Ismail Kimyacioglu July 3, 2019 | 1:24 pm

    Hi Simon,

    Thanks for the tutorial. I run an online shop and would like to import bulk products from a vendor company. I asked them to provide an XML file to me so that I can easily upload via converting the XML file to a CSV with Woocommerce schema.

    However, they sent me some information with Rest API. I have been googling to understand the concept but no success, yet.

    As explained on the web, I have activated legacy API and generated ck and cs. However, I still don’t understand where I should write the vendor’s ck and cs.

    Could you please help me ?

    Thanks in advance.

    • Simon Gondeck July 3, 2019 | 2:20 pm

      Hi Ismail, can you explain in more detail what information the vendor company sent to you? For example, did your vendor company send you their consumer_key and consumer_secret? If so, the reason they would provide their keys so you could connect with the REST API on their store. In addition, have you downloaded an API client such as Insomnia or Postman to connect to the REST API?

  3. Jannath Hussain July 25, 2019 | 1:34 am

    Hi Simon,

    Just like magento api’s.. do we have a flow of rest api’s from login to placing order and logout in wordpress????

  4. jibs August 19, 2019 | 7:26 pm

    Hi Simon, helpful post. Thanks. Using Insomnia if I send the key & secret in the URL as query string parameters I am able to get it work, but as you have it described (buy using Basic Auth) I get the below response. Any ideas?

    {
      "code": "woocommerce_rest_cannot_edit",
      "message": "Sorry, you are not allowed to edit this resource.",
      "data": {
        "status": 401
      }
    }
    • Simon Gondeck August 19, 2019 | 9:24 pm

      Hi Jibs, WooCommerce mentions that occasionally some servers may not parse the Authorization header correctly. This could be due to using WP Super Cache or W3 Total Cache. Robot Ninja wrote a great post that goes over common WooCommerce REST API issues and how to fix them. Look at Issue #1 (caching plugins removing headers) and Issue #4 at this link: How to Fix Common WooCommerce REST API Issues

      Also, when you generated your API keys, are you are administrator on the site? You could also try regenerating your API keys and ensuring you have both Read/Write access but assume you have already done so.

      Are all requests returning this error? For example, are you able to do GET requests but not any PUT or POST requests?

      • jibs August 20, 2019 | 10:13 pm

        Wow, Simon. Thanks. #4 did the trick. You saved me a lot troubleshooting!

  5. Charles September 26, 2019 | 11:59 pm

    Hi Simon,
    Thanks for a good tutorial! I need to make use of the API for payment processing by another party. They have a set of variables that need to pass into the API (see below xml request) based on an order number generated earlier by a woocommerce user. I’m trying find a way to edit the endpoint json/wc/v3/orders so that the incoming request can get amount payable for the existing “on-hold” order, add a transaction id (and other variables), then allow the status of the order to be changed to “completed” with another request, confirming the amount paid. I have been able to do basic GET and POST actions with POSTMAN using Basic Auth to the mentioned endpoint. It’s just the editing of that endpoint where I need direction please.

  6. Charles September 27, 2019 | 12:01 am

    O’s sorry…the sample XML Request is as follows:

    277137345
    238262786
    3011734780
    02
    DataExchange
    13089
    2
    E03026000200569

    A

    8040.43
    0
    H
    1003010
    0000016536

    2

    1550527
    2012/08/23
    13:12:11

  7. Soha Zain October 14, 2019 | 1:24 pm

    Amazingly Great job. These two points are well covered; “Add a API keys with Read/Write access” and “Use your API keys to connect to Insomnia”. Thanks for sharing this topic of “How To Use The WooCommerce API Without Knowing How to Code”. The best part is the article has all the practical detailing! Keep sharing

Leave a reply

Your email address will not be published. Required fields are marked*