Using the WordPress and WooCommerce REST APIs in the same Node app

I thought I’d share my solution for anyone working on a similar problem. I ended up switching to node-wpapi and using REST Autodiscovery to enable use of the WooCommerce & WordPress APIs from a single library. This can be done like so:

const wpapi = require('wpapi')

let wp = new wpapi({
  endpoint: 'http://your.wp.site/wp-json/',
  username: 'user',
  password: 'pass',
  auth: true,
  routes: routes
})
let woo = wp.namespace('wc/v2')

Where routes is a JSON representation of the REST API routes. See here for information on bootstrapping the API wrapper.

I’m then able to use both APIs side-by-side

wp.users()
  .search('foo') // Search by (for example) username
  .param('context', 'edit') // Get additional fields
  .then(result => {
    let user = result[0]
    woo.customers().id(user.id) // Get WooCommerce specific fields
    .then(customer => {
      console.log('woo.customer', customer)
    })
  })