WooCommerce REST API Development
Before writing code
Fetch live docs:
- Fetch
https://woocommerce.github.io/woocommerce-rest-api-docs/ for API reference
- Fetch
https://developer.wordpress.org/rest-api/ for WordPress REST API handbook
- Web-search
site:developer.woocommerce.com rest api extending for extension patterns
REST API Architecture
Foundation
WooCommerce REST API v3 extends the WordPress REST API:
- Base:
/wp-json/wc/v3/
- Built on
WP_REST_Controller pattern
- Supports JSON request/response
- Versioned — v3 is current; v1/v2 are legacy
Built-In Endpoints
| Resource |
Endpoint |
Methods |
| Products |
/wc/v3/products |
GET, POST, PUT, DELETE |
| Product Variations |
/wc/v3/products/{id}/variations |
GET, POST, PUT, DELETE |
| Orders |
/wc/v3/orders |
GET, POST, PUT, DELETE |
| Customers |
/wc/v3/customers |
GET, POST, PUT, DELETE |
| Coupons |
/wc/v3/coupons |
GET, POST, PUT, DELETE |
| Reports |
/wc/v3/reports |
GET |
| Settings |
/wc/v3/settings |
GET, PUT |
| Shipping Zones |
/wc/v3/shipping/zones |
GET, POST, PUT, DELETE |
| Tax Rates |
/wc/v3/taxes |
GET, POST, PUT, DELETE |
| Webhooks |
/wc/v3/webhooks |
GET, POST, PUT, DELETE |
| System Status |
/wc/v3/system_status |
GET |
Authentication
API Keys
WooCommerce generates consumer key/secret pairs:
- Over HTTPS: Pass as query params
consumer_key & consumer_secret, or HTTP Basic Auth
- Over HTTP: OAuth 1.0a signature required
- Keys have permissions:
read, write, read_write
- Generate at WooCommerce > Settings > Advanced > REST API
Application Passwords (WordPress 5.6+)
WordPress-native auth — username + application password via HTTP Basic Auth. Works for all WP REST API endpoints including WooCommerce.
Cookie/Nonce Authentication
For internal (same-site) JavaScript:
- Use
wp_create_nonce( 'wp_rest' ) — set as X-WP-Nonce header
- Automatically handled by
wp.apiFetch in WordPress scripts
Custom Endpoints
Registering Routes
Use rest_api_init action to register routes:
register_rest_route( 'my-extension/v1', '/items', $args )
- Define
methods, callback, permission_callback, args (with validate_callback and sanitize_callback)
Controller Pattern
Extend WP_REST_Controller for structured endpoints:
register_routes() — define route patterns
get_items() — handle collection GET
get_item() — handle single GET
create_item() — handle POST
update_item() — handle PUT/PATCH
delete_item() — handle DELETE
get_item_schema() — JSON Schema for the resource
get_item_permissions_check() — authorization
Extending WooCommerce Endpoints
Add fields to existing WooCommerce resources:
register_rest_field( 'product', 'my_field', $args ) — add fields to product responses
$args includes get_callback, update_callback, schema
Filtering Responses
woocommerce_rest_prepare_{post_type} — filter response before sending (e.g., woocommerce_rest_prepare_product_object)
woocommerce_rest_pre_insert_{post_type} — filter object before saving
woocommerce_rest_{post_type}_query — filter query args
Webhooks
Built-In Webhooks
WooCommerce webhooks fire on resource events:
- Topics:
order.created, order.updated, product.created, customer.created, etc.
- Configured in WooCommerce > Settings > Advanced > Webhooks
- Delivered via HTTP POST with JSON payload and signature header
Custom Webhook Topics
Filter woocommerce_valid_webhook_events and woocommerce_webhook_topic_hooks to add custom topics.
Batch Operations
POST to /wc/v3/products/batch with create, update, delete arrays to perform bulk operations in a single request.
Best Practices
- Always use
permission_callback — never leave it empty or return true for non-public endpoints
- Validate and sanitize all input parameters
- Return proper HTTP status codes (200, 201, 400, 401, 403, 404)
- Use JSON Schema for endpoint argument validation
- Use
WP_REST_Response for responses with proper status codes
- Paginate list endpoints with
per_page, page, offset
- Include
_links for HATEOAS-style discoverability
- Use webhooks for real-time integrations instead of polling
Fetch the WooCommerce REST API docs and WordPress REST API handbook for exact endpoint paths, parameters, and authentication details before implementing.
1---2name: woo-api3description: Build and consume WooCommerce REST API v3 endpoints — authentication, custom endpoints, extending existing resources, webhooks, and batch operations. Use when creating custom API endpoints or integrating external systems with WooCommerce.4---5
6# WooCommerce REST API Development
7
8## Before writing code
9
10**Fetch live docs**:
111. Fetch `https://woocommerce.github.io/woocommerce-rest-api-docs/` for API reference
122. Fetch `https://developer.wordpress.org/rest-api/` for WordPress REST API handbook
133. Web-search `site:developer.woocommerce.com rest api extending` for extension patterns
14
15## REST API Architecture
16
17### Foundation
18
19WooCommerce REST API v3 extends the WordPress REST API:
20- Base: `/wp-json/wc/v3/`
21- Built on `WP_REST_Controller` pattern
22- Supports JSON request/response
23- Versioned — v3 is current; v1/v2 are legacy
24
25### Built-In Endpoints
26
27| Resource | Endpoint | Methods |
28|----------|----------|---------|
29| Products | `/wc/v3/products` | GET, POST, PUT, DELETE |
30| Product Variations | `/wc/v3/products/{id}/variations` | GET, POST, PUT, DELETE |
31| Orders | `/wc/v3/orders` | GET, POST, PUT, DELETE |
32| Customers | `/wc/v3/customers` | GET, POST, PUT, DELETE |
33| Coupons | `/wc/v3/coupons` | GET, POST, PUT, DELETE |
34| Reports | `/wc/v3/reports` | GET |
35| Settings | `/wc/v3/settings` | GET, PUT |
36| Shipping Zones | `/wc/v3/shipping/zones` | GET, POST, PUT, DELETE |
37| Tax Rates | `/wc/v3/taxes` | GET, POST, PUT, DELETE |
38| Webhooks | `/wc/v3/webhooks` | GET, POST, PUT, DELETE |
39| System Status | `/wc/v3/system_status` | GET |
40
41## Authentication
42
43### API Keys
44
45WooCommerce generates consumer key/secret pairs:
46- **Over HTTPS**: Pass as query params `consumer_key` & `consumer_secret`, or HTTP Basic Auth
47- **Over HTTP**: OAuth 1.0a signature required
48- Keys have permissions: `read`, `write`, `read_write`
49- Generate at WooCommerce > Settings > Advanced > REST API
50
51### Application Passwords (WordPress 5.6+)
52
53WordPress-native auth — username + application password via HTTP Basic Auth. Works for all WP REST API endpoints including WooCommerce.
54
55### Cookie/Nonce Authentication
56
57For internal (same-site) JavaScript:
58- Use `wp_create_nonce( 'wp_rest' )` — set as `X-WP-Nonce` header
59- Automatically handled by `wp.apiFetch` in WordPress scripts
60
61## Custom Endpoints
62
63### Registering Routes
64
65Use `rest_api_init` action to register routes:
66- `register_rest_route( 'my-extension/v1', '/items', $args )`
67- Define `methods`, `callback`, `permission_callback`, `args` (with `validate_callback` and `sanitize_callback`)
68
69### Controller Pattern
70
71Extend `WP_REST_Controller` for structured endpoints:
72- `register_routes()` — define route patterns
73- `get_items()` — handle collection GET
74- `get_item()` — handle single GET
75- `create_item()` — handle POST
76- `update_item()` — handle PUT/PATCH
77- `delete_item()` — handle DELETE
78- `get_item_schema()` — JSON Schema for the resource
79- `get_item_permissions_check()` — authorization
80
81### Extending WooCommerce Endpoints
82
83Add fields to existing WooCommerce resources:
84- `register_rest_field( 'product', 'my_field', $args )` — add fields to product responses
85- `$args` includes `get_callback`, `update_callback`, `schema`
86
87### Filtering Responses
88
89- `woocommerce_rest_prepare_{post_type}` — filter response before sending (e.g., `woocommerce_rest_prepare_product_object`)
90- `woocommerce_rest_pre_insert_{post_type}` — filter object before saving
91- `woocommerce_rest_{post_type}_query` — filter query args
92
93## Webhooks
94
95### Built-In Webhooks
96
97WooCommerce webhooks fire on resource events:
98- Topics: `order.created`, `order.updated`, `product.created`, `customer.created`, etc.
99- Configured in WooCommerce > Settings > Advanced > Webhooks
100- Delivered via HTTP POST with JSON payload and signature header
101
102### Custom Webhook Topics
103
104Filter `woocommerce_valid_webhook_events` and `woocommerce_webhook_topic_hooks` to add custom topics.
105
106## Batch Operations
107
108POST to `/wc/v3/products/batch` with `create`, `update`, `delete` arrays to perform bulk operations in a single request.
109
110## Best Practices
111
112- Always use `permission_callback` — never leave it empty or return `true` for non-public endpoints
113- Validate and sanitize all input parameters
114- Return proper HTTP status codes (200, 201, 400, 401, 403, 404)
115- Use JSON Schema for endpoint argument validation
116- Use `WP_REST_Response` for responses with proper status codes
117- Paginate list endpoints with `per_page`, `page`, `offset`
118- Include `_links` for HATEOAS-style discoverability
119- Use webhooks for real-time integrations instead of polling
120
121Fetch the WooCommerce REST API docs and WordPress REST API handbook for exact endpoint paths, parameters, and authentication details before implementing.