2020-09-21 19:32:44 +00:00
# WooCommerce API Client
2020-07-02 21:39:18 +00:00
2022-03-01 21:34:32 +00:00
An API client for interacting with WooCommerce installations that works both on Browser and Node environments. Here are the current and planned
2020-09-21 19:32:44 +00:00
features:
2020-07-02 21:39:18 +00:00
2022-03-01 21:41:25 +00:00
- [x] TypeScript Definitions \*
2020-09-21 19:32:44 +00:00
- [x] Axios API Client with support for OAuth & basic auth
2022-03-01 21:41:25 +00:00
- [X] Partial support to Repositories, to simplify interaction with basic data types \*
- [x] Service classes for common activities such as changing settings
2022-03-01 21:34:32 +00:00
2022-03-01 21:41:25 +00:00
\* TypeScript Definitions and Repositories are only supported for Products, and partially supported for Orders.
2022-03-01 21:34:32 +00:00
## Diferences between this and @woocommerce/woocomerce-rest-api
WooCommerce has two API clients in JavaScript for interacting with WooCommerce installation. This one, and [@woocommerce/woocomerce-rest-api ](https://www.npmjs.com/package/@woocommerce/woocommerce-rest-api ).
The main difference between them is the Repositories and the TypeScript definitions for the supported endpoints. When using Axios directly, as you can do with both libraries, you query the WooCommerce API in a raw object format, following the API documentation parameters. With Repositories, you have the parameters as properties of an object, so you have auto-complete and strict types, for instance.
2020-07-02 21:39:18 +00:00
2020-09-21 19:32:44 +00:00
## Usage
```bash
2020-09-03 19:28:17 +00:00
npm install @woocommerce/api --save-dev
2020-09-21 19:32:44 +00:00
```
2020-07-02 21:39:18 +00:00
2020-09-21 19:32:44 +00:00
Depending on what you're intending to get out of the API client there are a few different ways of using it.
2020-07-02 21:39:18 +00:00
2020-09-21 19:32:44 +00:00
### REST API
The simplest way to use the client is directly:
2020-07-02 21:39:18 +00:00
```javascript
2020-09-21 19:32:44 +00:00
import { HTTPClientFactory } from '@woocommerce/api';
// You can create an API client using the client factory with pre-configured middleware for convenience.
2020-11-04 01:04:41 +00:00
let client = HTTPClientFactory.build( 'https://example.com' )
.withBasicAuth( 'username', 'password' )
.create();
2020-09-21 19:32:44 +00:00
// You can also create an API client configured for requests using OAuth.
2020-11-04 01:04:41 +00:00
client = HTTPClientFactory.build( 'https://example.com' )
.withOAuth( 'consumer_secret', 'consumer_password' )
.create();
2020-09-21 19:32:44 +00:00
// You can then use the client to make API requests.
2021-05-22 16:06:17 +00:00
client.get( '/wc/v3/products' ).then( ( response ) => {
2020-09-21 19:32:44 +00:00
// Access the status code from the response.
2021-10-14 21:45:39 +00:00
response.status;
2020-09-21 19:32:44 +00:00
// Access the headers from the response.
response.headers;
// Access the data from the response, in this case, the products.
response.data;
2020-10-02 21:10:24 +00:00
}, ( error ) => {
// Handle errors that may have come up.
2020-09-21 19:32:44 +00:00
} );
2020-11-04 01:04:41 +00:00
2020-07-02 21:39:18 +00:00
```
2020-09-21 19:32:44 +00:00
### Repositories
2020-07-02 21:39:18 +00:00
2021-03-16 19:00:01 +00:00
As a convenience utility we've created repositories for core data types that can simplify interacting with the API:
2021-05-13 19:06:28 +00:00
#### Parent/Base Repositories
2021-03-16 19:00:01 +00:00
- `SimpleProduct`
- `ExternalProduct`
- `GroupedProduct`
- `VariableProduct`
- `Coupon`
2021-05-13 19:06:28 +00:00
- `Order`
- `SettingsGroup`
#### Child Repositories
- `ProductVariation`
- `Setting`
2021-03-16 19:00:01 +00:00
2020-09-21 19:32:44 +00:00
These repositories provide CRUD methods for ease-of-use:
```javascript
2021-03-16 19:00:01 +00:00
import { HTTPClientFactory, SimpleProduct } from '@woocommerce/api';
2020-09-21 19:32:44 +00:00
// Prepare the HTTP client that will be consumed by the repository.
// This is necessary so that it can make requests to the REST API.
2020-11-04 01:04:41 +00:00
const httpClient = HTTPClientFactory.build( 'https://example.com' )
.withBasicAuth( 'username', 'password' )
.create();
2020-09-21 19:32:44 +00:00
const repository = SimpleProduct.restRepository( httpClient );
// The repository can now be used to create models.
const product = repository.create( { name: 'Simple Product', regularPrice: '9.99' } );
// The response will be one of the models with structured properties and TypeScript support.
product.id;
2021-03-16 19:00:01 +00:00
```
#### Repository Methods
2021-05-13 19:06:28 +00:00
The following methods are available on all repositories if the corresponding method is available on the API endpoint:
2021-03-16 19:00:01 +00:00
- `create( {...properties} )` - Create a single object of the model type
- `delete( objectId )` - Delete a single object of the model type
2021-05-25 14:00:56 +00:00
- `list( {...parameters} )` - Retrieve a list of the existing objects of that model type
2021-03-16 19:00:01 +00:00
- `read( objectId )` - Read a single object of the model type
- `update( objectId, {...properties} )` - Update a single object of the model type
#### Child Repositories
2021-05-13 19:06:28 +00:00
In child model repositories, each method requires the `parentId` as the first parameter:
2021-03-16 19:00:01 +00:00
```javascript
import { HTTPClientFactory, VariableProduct, ProductVariation } from '@woocommerce/api';
const httpClient = HTTPClientFactory.build( 'https://example.com' )
.withBasicAuth( 'username', 'password' )
2021-05-13 19:06:28 +00:00
.withIndexPermalinks()
2021-03-16 19:00:01 +00:00
.create();
2020-09-21 19:32:44 +00:00
2021-03-16 19:00:01 +00:00
const productRepository = VariableProduct.restRepository( httpClient );
const variationRepository = ProductVariation.restRepository( httpClient );
const product = await productRepository.create({
"name": "Variable Product with Three Attributes",
"defaultAttributes": [
{
"id": 0,
"name": "Size",
"option": "Medium"
},
{
"id": 0,
"name": "Colour",
"option": "Blue"
}
],
"attributes": [
{
"id": 0,
"name": "Colour",
"isVisibleOnProductPage": true,
"isForVariations": true,
"options": [
"Red",
"Green",
"Blue"
],
"sortOrder": 0
},
{
"id": 0,
"name": "Size",
"isVisibleOnProductPage": true,
"isForVariations": true,
"options": [
"Small",
"Medium",
"Large"
],
"sortOrder": 0
}
]
});
const variation = await variationRepository.create( product.id, {
"regularPrice": "19.99",
"attributes": [
{
"name": "Size",
"option": "Large"
},
{
"name": "Colour",
"option": "Red"
}
]
});
2020-09-21 19:32:44 +00:00
```