You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/includes/v2/_introduction.md
+29-36
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,28 @@
1
-
# Introduction
1
+
# Introduction #
2
2
3
3
Introduced in WooCommerce 2.1, the REST API allows store data to be created, read, updated, and deleted using the JSON format.
4
4
5
-
## Requirements
5
+
## Requirements ##
6
6
7
7
You must be using WooCommerce 2.1 or newer and the REST API must be enabled under `WooCommerce > Settings`. You must enable pretty permalinks, as default permalinks will not work.
8
8
9
9
<asideclass="notice">
10
10
Many endpoints were improving with new versions of WooCommerce, so we always recommend keeping your WooCommerce updated to work properly with this documentation.
11
11
</aside>
12
12
13
-
## Schema
13
+
## Schema ##
14
14
15
15
The API is accessible via this endpoint:
16
16
17
17
`https://www.your-store.com/wc-api/v2`
18
18
19
19
You may access the API over either HTTP or HTTPS. HTTPS is recommended where possible, as authentication is simpler. The API index will declare if the site supports SSL or not.
20
20
21
-
## Version
21
+
## Version ##
22
22
23
23
The current API version is `v2` which takes a first-order position in endpoints. The `v1` endpoint is available in WooCommerce 2.1 / 2.2 / 2.3, but it will be removed in a future version.
24
24
25
-
### Differences between v1 and v2 versions
25
+
### Differences between v1 and v2 versions ###
26
26
27
27
* v1 supports XML response format, v2 only supports JSON.
28
28
* v1 does not support creating or updating (with the exception of order status) any resources, v2 supports full create/read/update/delete for all endpoints.
@@ -33,7 +33,7 @@ The current API version is `v2` which takes a first-order position in endpoints.
33
33
* v1 does not include any endpoints for getting valid order statuses, v2 includes an endpoint for listing valid order statuses (`GET /orders/statuses`).
34
34
* v2 supports the core features added in WooCommerce 2.2, primarily order refunds (via the `/orders/refunds` endpoint) and Webhooks (via the `/webhooks`).
35
35
36
-
## Requests/Responses
36
+
## Requests/Responses ##
37
37
38
38
The default response format is JSON. Requests with a message-body use plain JSON to set or update resource attributes. Successful requests will return a `200 OK` HTTP status.
39
39
@@ -49,11 +49,11 @@ Some general information about responses:
49
49
50
50
* Blank fields are generally included as `null` instead of being blank strings or omitted.
51
51
52
-
## Authentication
52
+
## Authentication ##
53
53
54
54
There are two aways to authenticate with the API, depending on whether the site supports SSL or not. Remember that the Index endpoint will indicate if the site supports SSL or not.
55
55
56
-
### Over HTTPS
56
+
### Over HTTPS ###
57
57
58
58
You may use [HTTP Basic Auth](http://en.wikipedia.org/wiki/Basic_access_authentication) by providing the API Consumer Key as the username and the API Consumer Secret as the password.
59
59
@@ -72,10 +72,11 @@ Occasionally some servers may not properly parse the Authorization header (if yo
You must use [OAuth 1.0a "one-legged" authentication](http://tools.ietf.org/html/rfc5849) to ensure API credentials cannot be intercepted. Typically you may use any standard OAuth 1.0a library in your language of choice to handle the authentication, or generate the necessary parameters by following these instructions.
77
78
78
-
#### Generating an OAuth signature
79
+
#### Generating an OAuth signature ####
79
80
80
81
1) Set the HTTP method for the request:
81
82
@@ -109,33 +110,25 @@ when encoded:
109
110
110
111
If you are having trouble generating a correct signature, you'll want to review your string to sign for errors with encoding. The [authentication source](https://github.com/woothemes/woocommerce/blob/master/includes/api/class-wc-api-authentication.php#L177) can also be helpful in understanding how to properly generate the signature.
111
112
112
-
#### OAuth Tips
113
+
#### OAuth Tips ####
113
114
114
115
* The OAuth parameters must be added as query string parameters and *not* included in the Authorization header. This is because there is no reliable cross-platform way to get the raw request headers in WordPress.
115
-
116
116
* The require parameters are: `oauth_consumer_key`, `oauth_timestamp`, `oauth_nonce`, `oauth_signature`, and `oauth_signature_method`. `oauth_version` is not required and must be omitted.
117
-
118
117
* HMAC-SHA1 or HMAC-SHA256 are the only accepted hash algorithms.
119
-
120
118
* The OAuth nonce can be any randomly generated 32 character (recommended) string that is unique to the consumer key. Read more suggestions on [generating a nonce](https://dev.twitter.com/discussions/12445) on the Twitter API forums.
121
-
122
119
* The OAuth timestamp should be the unix timestamp at the time of the request. The API will deny any requests that include a timestamp that is outside of a 15 minute window to prevent replay attacks.
123
-
124
120
* You must use the store URL provided by the index when forming the base string used for the signature, as this is what the server will use. (e.g. if the store URL includes a `www` sub-domain, you should use it for requests)
125
-
126
121
* Some OAuth libraries add an ampersand to the provided secret key before generating the signature. This does not adhere to the OAuth spec and the ampersand should be removed prior to generating the signature.
127
-
128
122
* You may test your generated signature using LinkedIn's [OAuth test console](http://developer.linkedinlabs.com/oauth-test/) -- leave the member token/secret blank.
129
-
130
123
* Twitter has great instructions on [generating a signature](https://dev.twitter.com/docs/auth/creating-signature) with OAuth 1.0a, but remember tokens are not used with this implementation.
131
-
132
124
* Note that the request body is *not* signed as per the OAuth spec, see [Google's OAuth 1.0 extension](https://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/oauth-bodyhash.html) for details on why.
125
+
* If including filter fields in your request, it saves a lot of trouble if you can order your filter fields alphabetically before submitting. Many Oauth libraries won't order subquery fields properly, resulting in invalid signatures.
133
126
134
-
## Parameters
127
+
## Parameters ##
135
128
136
129
All endpoints accept optional parameters which can be passed as an HTTP query string parameter, e.g. `GET /orders?status=completed`. There are common parameters and endpoint-specific parameters which are documented along with that endpoint.
137
130
138
-
### Filter Parameter
131
+
### Filter Parameter ###
139
132
140
133
All endpoints accept a `filter` parameter that scopes individual filters using brackets, like date filtering:
141
134
@@ -147,7 +140,7 @@ Multiple `filter` parameters can be included and intermixed with other parameter
147
140
148
141
Note that the following filters are supported for all endpoints except the `reports` endpoint, which has it's own set of filters that are documented along with that endpoint.
@@ -165,7 +158,7 @@ Note that the following filters are supported for all endpoints except the `repo
165
158
166
159
Note that Dates should be provided in [RFC3339](http://www.ietf.org/rfc/rfc3339.txt) format in UTC timezone: `YYYY-MM-DDTHH:MM:SSZ`. You may omit the time and timezone if desired.
167
160
168
-
### Fields Parameter
161
+
### Fields Parameter ###
169
162
170
163
You may limit the fields returned in the response using the `fields` parameter:
171
164
@@ -183,7 +176,7 @@ Sub-fields can't be limited for resources that have multiple structs, like an or
183
176
184
177
`GET /orders?fields=line_items.product_id`
185
178
186
-
## Pagination
179
+
## Pagination ##
187
180
188
181
Requests that return multiple items will be paginated to 10 items by default. This default can be changed by the site administrator by changing the `posts_per_page` option. Alternatively the items per page can be specifed with the `?filter[limit]` parameter:
189
182
@@ -201,7 +194,7 @@ Page number is 1-based and ommiting the `?page` parameter will return the first
201
194
202
195
The total number of resources and pages are always included in the `X-WC-Total` and `X-WC-TotalPages` HTTP headers.
203
196
204
-
## Link Header
197
+
## Link Header ##
205
198
206
199
Pagination info is included in the [Link Header](http://tools.ietf.org/html/rfc5988). It's recommended that you follow these values instead of building your own URLs where possible.
207
200
@@ -221,7 +214,7 @@ The possible `rel` values are:
221
214
|`first`| Shows the URL of the first page of results |
222
215
|`prev`| Shows the URL of the immediate previous page of results |
223
216
224
-
## Errors
217
+
## Errors ##
225
218
226
219
Occasionally you might encounter errors when accessing the API. There are four possible types:
227
220
@@ -284,7 +277,7 @@ Occasionally you might encounter errors when accessing the API. There are four p
284
277
285
278
Errors return both an appropriate HTTP status code and response object which contains a `code` and `message` attribute. If an endpoint has any custom errors, they are documented with that endpoint.
286
279
287
-
## HTTP Verbs
280
+
## HTTP Verbs ##
288
281
289
282
The API uses the appropriate HTTP verb for each action:
290
283
@@ -296,7 +289,7 @@ The API uses the appropriate HTTP verb for each action:
296
289
|`POST`| Used for creating resources |
297
290
|`DELETE`| Used for deleting resources |
298
291
299
-
## JSONP Support
292
+
## JSONP Support ##
300
293
301
294
The API supports JSONP by default. JSONP responses uses the `application/javascript` content-type. You can specify the callback using the `?_jsonp` parameter for `GET` requests to have the response wrapped in a JSON function:
Webhooks are an experimental feature in the v2 REST API. They must be managed using the REST API endpoints as a UI is not yet available. The `WC_Webhook` class manages all data storage/retrieval from the custom post type, as well as enqueuing a webhook's actions and processing/delivering/logging the webhook. On `woocommerce_init`, active webhooks are loaded and their associated hooks are added.
351
344
@@ -357,7 +350,7 @@ Each webhook has:
357
350
* secret: an optional secret key that is used to generate a HMAC-SHA256 hash of the request body so the receiver can verify authenticity of the webhook
358
351
* hooks: an array of hook names that are added and bound to the webhook for processing
359
352
360
-
### Topics
353
+
### Topics ###
361
354
362
355
The topic is a combination resource (e.g. order) and event (e.g. created) and maps to one or more hook names (e.g. `woocommerce_checkout_order_processed`). Webhooks can be created using the topic name and the appropriate hooks are automatically added.
363
356
@@ -370,7 +363,7 @@ Core topics are:
370
363
371
364
Custom topics can also be used which map to a single hook name, so for example you could add a webhook with topic `action.woocommerce_add_to_cart` that is triggered on that event. Custom topics pass the first hook argument to the payload, so in this example the `cart_item_key` would be included in the payload.
372
365
373
-
### Delivery/Payload
366
+
### Delivery/Payload ###
374
367
375
368
Delivery is done using `wp_remote_post()` (HTTP POST) and processed in the background by default using wp-cron. A few custom headers are added to the request to help the receiver process the webhook:
376
369
@@ -383,7 +376,7 @@ Delivery is done using `wp_remote_post()` (HTTP POST) and processed in the backg
383
376
384
377
The payload is JSON encoded and for API resources (coupons,customers,orders,products), the response is exactly the same as if requested via the REST API.
385
378
386
-
### Logging
379
+
### Logging ###
387
380
388
381
Requests/responses are logged as comments on the webhook custom post type. Each delivery log includes:
389
382
@@ -397,15 +390,15 @@ After 5 consecutive failed deliveries (as defined by a non HTTP 2xx response cod
397
390
398
391
Delivery logs can be fetched through the REST API endpoint or in code using `WC_Webhook::get_delivery_logs()`
399
392
400
-
### Endpoints
393
+
### Endpoints ###
401
394
402
395
See the webhook resource section.
403
396
404
-
## Troubleshooting
397
+
## Troubleshooting ##
405
398
406
399
* Nginx - Older configurations of Nginx can cause issues with the API, see [this issue](https://github.com/woothemes/woocommerce/issues/5616#issuecomment-47338737) for details
407
400
408
-
## Tools
401
+
## Tools ##
409
402
410
403
*[WooCommerce REST API Client Library](https://github.com/kloon/WooCommerce-REST-API-Client-Library) - A simple PHP client library by Gerhard Potgieter.
411
404
*[WooCommerce API Client](https://github.com/mac2000/woo-commerce-api-client) - Guzzle wrapper around WooCommerce v2 API plus missign api helpers.
0 commit comments