docs/apis/store-api/README.md
The Store API provides public Rest API endpoints for the development of customer-facing cart, checkout, and product functionality. It follows many of the patterns used in the WordPress REST API.
In contrast to the WooCommerce REST API, the Store API is unauthenticated and does not provide access to sensitive store data or other customer information.
Example of a valid API request using cURL:
curl "https://example-store.com/wp-json/wc/store/v1/products"
Possible uses of the Store API include:
Resources in the Store API are all found within the wc/store/v1 namespace, and since this API extends the WordPress API, accessing it requires the /wp-json/ base. Currently, the only version is v1. If the version is omitted, v1 will be served.
Examples:
GET /wp-json/wc/store/v1/products
GET /wp-json/wc/store/v1/cart
The API uses JSON to serialize data. You don’t need to specify .json at the end of an API URL.
Available resources in the Store API are listed below, with links to more detailed documentation.
If collections contain many results, they may be paginated. When listing resources you can pass the following parameters:
| Parameter | Description |
|---|---|
page | Current page of the collection. Defaults to 1. |
per_page | Maximum number of items to be returned in result set. Defaults to 10. Maximum 100. |
In the example below, we list 20 products per page and return page 2.
curl "https://example-store.com/wp-json/wc/store/v1/products?page=2&per_page=20"
Additional pagination headers are also sent back with extra information.
| Header | Description |
|---|---|
X-WP-Total | The total number of items in the collection. |
X-WP-TotalPages | The total number of pages in the collection. |
Link | Contains links to other pages; next, prev, and up where applicable. |
The following table gives an overview of how the API functions generally behave.
| Request type | Description |
|---|---|
GET | Access one or more resources and return 200 OK and the result as JSON. |
POST | Return 201 Created if the resource is successfully created and return the newly created resource as JSON. |
PUT | Return 200 OK if the resource is modified successfully. The modified result is returned as JSON. |
DELETE | Returns 204 No Content if the resource was deleted successfully. |
The following table shows the possible return codes for API requests.
| Response code | Description |
|---|---|
200 OK | The request was successful, the resource(s) itself is returned as JSON. |
204 No Content | The server has successfully fulfilled the request and that there is no additional content to send in the response payload body. |
201 Created | The POST request was successful and the resource is returned as JSON. |
400 Bad Request | A required attribute of the API request is missing. |
403 Forbidden | The request is not allowed. |
404 Not Found | A resource could not be accessed, for example it doesn't exist. |
405 Method Not Allowed | The request is not supported. |
409 Conflict | The request could not be completed due to a conflict with the current state of the target resource. The current state may also be returned. |
500 Server Error | While handling the request something went wrong server-side. |
There are 3 main parts to each route in the Store API:
AbstractRoute class; this class contains shared functionality for handling requests and returning JSON responses. Routes ensure a valid response is returned and handle collections, errors, and pagination.AbstractSchema class.Typically, routes handle the following types of requests:
GET requests to read product, cart, or checkout data.POST and PUT requests to update cart and checkout data.DELETE requests to remove cart data.OPTIONS requests to retrieve the JSON schema for the current route.Please review the Store API Guiding principles. This covers our approach to development, and topics such as versioning, what data is safe to include, and how to build new routes.
The approach to extensibility within the Store API is to expose certain routes and schema to the ExtendSchema class. Documentation for contributors on this can be found here.
If a route includes the extensibility interface, 3rd party developers can use the shared ExtendSchema::class instance to register additional endpoint data and additional schema.
This differs from the traditional filter hook approach in that it is more limiting, but it reduces the likelihood of a 3rd party extension breaking routes and endpoints or overwriting returned data which other apps may rely upon.
If new schema is required, and any of the following statements are true, choose to extend the Store API rather than introducing new schema to existing Store API schemas:
If the data is sensitive (for example, a core setting that should be private), or not related to the current user (for example, looking up an order by order ID), choose to use the authenticated WC REST API.
If you're looking to add new routes and endpoints, rather than extending the Store API schema, extending the Store API is not necessary. You can instead utilize core WordPress functionality to create new routes, choosing to use the same pattern of Store API if you wish. See: