WooCommerce REST API 文档

title: "Webhooks #" post_status: publish comment_status: open taxonomy: category: - woocommerce-rest-api post_tag: - V2 - Includes - Source


Webhooks

This section lists all API that can be used to create, edit or otherwise manipulate webhooks.

Webhooks Properties

Attribute Type Description
id integer The webhook ID (post ID) read-only
name string A friendly name for the webhook, defaults to "Webhook created on <date>"
status string Webhook status, options are active (delivers payload), paused (does not deliver), or disabled (does not deliver due delivery failures). Default is active
topic string Webhook topic, e.g. coupon.updated. See the complete list
resource string Webhook resource, e.g. coupon read-only
event string Webhook event, e.g. updated read-only
hooks array WooCommerce action names associated with the webhook read-only
delivery_url string The URL where the webhook payload is delivered
secret string Secret key used to generate a hash of the delivered webhook and provided in the request headers. required write-only
created_at string UTC DateTime when the webhook was created read-only
updated_at string UTC DateTime when the webhook was last updated read-only

Delivery Properties

Attribute Type Description
id integer The delivery ID (comment ID)
duration string The delivery duration, in seconds
summary string A friendly summary of the response including the HTTP response code, message, and body
request_url string The URL where the webhook was delivered
request_headers array Array of request headers (see Request Headers Attributes)
request_body string The request body, this matches the API response for the given resource (e.g. for the coupon.updated topic, the request body would match the response for GET /coupons/{id})
response_code string The HTTP response code from the receiving server
response_message string The HTTP response message from the receiving server
response_headers array Array of the response headers from the receiving server
response_body string The response body from the receiving server
created_at string A DateTime of when the delivery was logged

Request Headers Properties

Attribute Type Description
User-Agent string The request user agent, defaults to "WooCommerce/{version} Hookshot (WordPress/{version})"
Content-Type string The request content-type, defaults to "application/json"
X-WC-Webhook-Topic string The webhook topic
X-WC-Webhook-Resource string The webhook resource
X-WC-Webhook-Event string The webhook event
X-WC-Webhook-Signature string A base64 encoded HMAC-SHA256 hash of the payload
X-WC-Webhook-ID integer The webhook's ID
X-WC-Webhook-Delivery-ID integer The delivery ID

创建 Webhook

此 API 可帮助您创建一个新的 webhook。

HTTP 请求

POST
/wc-api/v2/webhooks
curl -X POST https://example.com/wc-api/v2/webhooks \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "webhook": {
    "name": "An add to cart webhook",
    "secret": "my-super-secret-private-key",
    "topic": "action.woocommerce_add_to_cart",
    "delivery_url": "http://requestb.in/1exdwip1"
  }
}'
var data = {
  webhook: {
    name: 'An add to cart webhook',
    secret: 'my-super-secret-private-key',
    topic: 'action.woocommerce_add_to_cart',
    delivery_url: 'http://requestb.in/1exdwip1'
  }
};

WooCommerce.post('webhooks', data, function(err, data, res) {
  console.log(res);
});
data = {
    "webhook": {
        "name": "An add to cart webhook",
        "secret": "my-super-secret-private-key",
        "topic": "action.woocommerce_add_to_cart",
        "delivery_url": "http://requestb.in/1exdwip1"
    }
}

print(wcapi.post("webhooks", data).json())
<?php
$data = array(
    'webhook' => array(
        'name' => 'An add to cart webhook',
        'secret' => 'my-super-secret-private-key',
        'topic' => 'action.woocommerce_add_to_cart',
        'delivery_url' => 'http://requestb.in/1exdwip1'
    )
);

print_r($woocommerce->webhooks->create($data));
?>
data = {
  webhook: {
    name: "An add to cart webhook",
    secret: "my-super-secret-private-key",
    topic: "action.woocommerce_add_to_cart",
    delivery_url: "http://requestb.in/1exdwip1"
  }
}

woocommerce.post("webhooks", data).parsed_response

JSON 响应示例:

{
  "webhook": {
    "id": 535,
    "name": "An add to cart webhook",
    "status": "active",
    "topic": "action.woocommerce_add_to_cart",
    "resource": "action",
    "event": "woocommerce_add_to_cart",
    "hooks": [
      "woocommerce_add_to_cart"
    ],
    "delivery_url": "http://requestb.in/1exdwip1",
    "created_at": "2015-01-21T13:19:58Z",
    "updated_at": "2015-01-21T13:19:58Z"
  }
}

查看 Webhook

此 API 允许您检索并查看特定的 webhook。

HTTP 请求

GET
/wc-api/v2/webhooks/<id>
curl https://example.com/wc-api/v2/webhooks/535 \
    -u consumer_key:consumer_secret
WooCommerce.get('webhooks/535', function(err, data, res) {
  console.log(res);
});
print(wcapi.get("webhooks/535").json())
<?php print_r($woocommerce->webhooks->get(535)); ?>
woocommerce.get("webhooks/535").parsed_response

JSON 响应示例:

{
  "webhook": {
    "id": 535,
    "name": "An add to cart webhook",
    "status": "active",
    "topic": "action.woocommerce_add_to_cart",
    "resource": "action",
    "event": "woocommerce_add_to_cart",
    "hooks": [
      "woocommerce_add_to_cart"
    ],
    "delivery_url": "http://requestb.in/1exdwip1",
    "created_at": "2015-01-21T13:19:58Z",
    "updated_at": "2015-01-21T13:19:58Z"
  }
}

View List Of Webhooks

This API helps you to view all the webhooks.

HTTP Request

GET
/wc-api/v2/webhooks
curl https://example.com/wc-api/v2/webhooks \
    -u consumer_key:consumer_secret
WooCommerce.get('webhooks', function(err, data, res) {
  console.log(res);
});
print(wcapi.get("webhooks").json())
<?php print_r($woocommerce->webhooks->get()); ?>
woocommerce.get("webhooks").parsed_response

JSON response example:

{
  "webhooks": [
    {
      "id": 535,
      "name": "An add to cart webhook",
      "status": "active",
      "topic": "action.woocommerce_add_to_cart",
      "resource": "action",
      "event": "woocommerce_add_to_cart",
      "hooks": [
        "woocommerce_add_to_cart"
      ],
      "delivery_url": "http://requestb.in/1exdwip1",
      "created_at": "2015-01-21T13:19:58Z",
      "updated_at": "2015-01-21T13:19:58Z"
    },
    {
      "id": 313,
      "name": "Webhook created on Jan 17, 2015 @ 11:45 AM",
      "status": "active",
      "topic": "order.created",
      "resource": "order",
      "event": "created",
      "hooks": [
        "woocommerce_checkout_order_processed",
        "woocommerce_process_shop_order_meta",
        "woocommerce_api_create_order"
      ],
      "delivery_url": "http://requestb.in/1exdwip1",
      "created_at": "2014-12-17T11:45:05Z",
      "updated_at": "2015-01-10T00:41:08Z"
    }
  ]
}

Available Filters

Filter Type Description
status string Webhooks by status. The following options are available: active or paused and disabled. Default is active

更新 Webhook

此 API 允许您修改 webhook。

HTTP 请求

PUT
/wc-api/v2/webhook/<id>
curl -X PUT https://example.com/wc-api/v2/webhook/535 \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "webhook": {
    "status": "paused"
  }
}'
var data = {
  webhook: {
    status: 'paused'
  }
}

WooCommerce.put('webhooks/535', data, function(err, data, res) {
  console.log(res);
});
data = {
    "webhook": {
        "status": "paused"
    }
}

print(wcapi.put("webhooks/535", data).json())
<?php
$data = array(
    'webhook' => array(
        'status' => 'paused'
    )
);

print_r($woocommerce->webhooks->updaste(535, $data));
?>
data = {
  webhook: {
    status: "paused"
  }
}

woocommerce.put("webhooks/535", data).parsed_response

JSON 响应示例:

{
  "webhook": {
    "id": 535,
    "name": "An add to cart webhook",
    "status": "paused",
    "topic": "action.woocommerce_add_to_cart",
    "resource": "action",
    "event": "woocommerce_add_to_cart",
    "hooks": [
      "woocommerce_add_to_cart"
    ],
    "delivery_url": "http://requestb.in/1exdwip1",
    "created_at": "2015-01-21T13:19:58Z",
    "updated_at": "2015-01-21T13:19:58Z"
  }
}

Delete A Webhook

This API helps you delete a webhook.

HTTP Request

DELETE
/wc-api/v2/webhooks/<id>
curl -X DELETE https://example.com/wc-api/v2/webhooks/535 \
    -u consumer_key:consumer_secret
WooCommerce.delete('webhooks/535', function(err, data, res) {
  console.log(res);
});
print(wcapi.delete("webhooks/535").json())
<?php print_r($woocommerce->webhooks->delete(535)); ?>
woocommerce.delete("webhooks/535").parsed_response

JSON response example:

{
  "message": "Permanently deleted webhook"
}

查看 Webhooks 数量

此 API 允许您检索所有 Webhooks 的数量。

HTTP 请求

GET
/wc-api/v2/webhooks/count
curl https://example.com/wc-api/v2/webhooks/count \
    -u consumer_key:consumer_secret
WooCommerce.get('webhooks/count', function(err, data, res) {
  console.log(res);
});
print(wcapi.get("webhooks/count").json())
<?php print_r($woocommerce->webhooks->get_count()); ?>
woocommerce.get("webhooks/count").parsed_response

JSON 响应示例:

{
  "count": 4
}

Available Filters

Filter Type Description
status string Webhooks by status. The following options are available: active or paused and disabled

查看 Webhook 投递记录

此 API 允许您检索并查看特定的 webhook 投递记录。

HTTP 请求

GET
/wc-api/v2/webhooks/<id>/deliveries/<delivery_id>
curl https://example.com/wc-api/v2/webhooks/535/deliveries/378 \
    -u consumer_key:consumer_secret
WooCommerce.get('webhooks/535/deliveries/378', function(err, data, res) {
  console.log(res);
});
print(wcapi.get("webhooks/535/deliveries/378").json())
<?php print_r($woocommerce->webhooks->get_deliveries(378)); ?>
woocommerce.get("webhooks/535/deliveries/378").parsed_response

JSON 响应示例:

{
  "webhook_delivery": {
    "id": 378,
    "duration": "0.90226",
    "summary": "HTTP 200 OK: ok",
    "request_method": "POST",
    "request_url": "http://requestb.in/125q7ns1",
    "request_headers": {
      "User-Agent": "WooCommerce/2.3.0 Hookshot (WordPress/4.1)",
      "Content-Type": "application/json",
      "X-WC-Webhook-Topic": "action.woocommerce_add_to_cart",
      "X-WC-Webhook-Resource": "action",
      "X-WC-Webhook-Event": "woocommerce_add_to_cart",
      "X-WC-Webhook-Signature": "geC1akFhCtsO7fbXz5XiGUsMsRa4Mt0IJsZ96nTaHjI=",
      "X-WC-Webhook-ID": 535,
      "X-WC-Webhook-Delivery-ID": 378
    },
    "request_body": "{\"action\":\"woocommerce_add_to_cart\",\"arg\":\"7cbbc409ec990f19c78c75bd1e06f215\"}",
    "response_code": "200",
    "response_message": "OK",
    "response_headers": {
      "connection": "close",
      "server": "gunicorn/18.0",
      "date": "Wed, 21 Jan 2015 16:22:49 GMT",
      "content-type": "text/html; charset=utf-8",
      "content-length": "2",
      "sponsored-by": "https://www.runscope.com",
      "via": "1.1 vegur"
    },
    "response_body": "ok",
    "created_at": "2015-01-21T16:26:12Z"
  }
}

查看 Webhooks 投递列表

此 API 可帮助您查看特定 Webhook 的所有投递记录。

HTTP 请求

GET
/wc-api/v2/webhooks/<id>/deliveries
curl https://example.com/wc-api/v2/webhooks/535/deliveries \
    -u consumer_key:consumer_secret
WooCommerce.get('webhooks/535/deliveries', function(err, data, res) {
  console.log(res);
});
print(wcapi.get("webhooks/535/deliveries").json())
<?php print_r($woocommerce->webhooks->get_deliveries()); ?>
woocommerce.get("webhooks/535/deliveries").parsed_response

JSON 响应示例:

{
  "webhook_deliveries": [
    {
      "id": 380,
      "duration": "0.58635",
      "summary": "HTTP 200 OK: ok",
      "request_method": "POST",
      "request_url": "http://requestb.in/125q7ns1",
      "request_headers": {
        "User-Agent": "WooCommerce/2.3.0 Hookshot (WordPress/4.1)",
        "Content-Type": "application/json",
        "X-WC-Webhook-Topic": "action.woocommerce_add_to_cart",
        "X-WC-Webhook-Resource": "action",
        "X-WC-Webhook-Event": "woocommerce_add_to_cart",
        "X-WC-Webhook-Signature": "st4egVCTwG1JMfxmxe7MZYEuj9Y6Euge4SOTNfCUCWY=",
        "X-WC-Webhook-ID": 535,
        "X-WC-Webhook-Delivery-ID": 380
      },
      "request_body": "{\"action\":\"woocommerce_add_to_cart\",\"arg\":\"c16a5320fa475530d9583c34fd356ef5\"}",
      "response_code": "200",
      "response_message": "OK",
      "response_headers": {
        "connection": "close",
        "server": "gunicorn/18.0",
        "date": "Wed, 21 Jan 2015 16:23:05 GMT",
        "content-type": "text/html; charset=utf-8",
        "content-length": "2",
        "sponsored-by": "https://www.runscope.com",
        "via": "1.1 vegur"
      },
      "response_body": "ok",
      "created_at": "2015-01-21T16:26:28Z"
    },
    {
      "id": 378,
      "duration": "0.90226",
      "summary": "HTTP 200 OK: ok",
      "request_method": "POST",
      "request_url": "http://requestb.in/125q7ns1",
      "request_headers": {
        "User-Agent": "WooCommerce/2.3.0 Hookshot (WordPress/4.1)",
        "Content-Type": "application/json",
        "X-WC-Webhook-Topic": "action.woocommerce_add_to_cart",
        "X-WC-Webhook-Resource": "action",
        "X-WC-Webhook-Event": "woocommerce_add_to_cart",
        "X-WC-Webhook-Signature": "geC1akFhCtsO7fbXz5XiGUsMsRa4Mt0IJsZ96nTaHjI=",
        "X-WC-Webhook-ID": 535,
        "X-WC-Webhook-Delivery-ID": 378
      },
      "request_body": "{\"action\":\"woocommerce_add_to_cart\",\"arg\":\"7cbbc409ec990f19c78c75bd1e06f215\"}",
      "response_code": "200",
      "response_message": "OK",
      "response_headers": {
        "connection": "close",
        "server": "gunicorn/18.0",
        "date": "Wed, 21 Jan 2015 16:22:49 GMT",
        "content-type": "text/html; charset=utf-8",
        "content-length": "2",
        "sponsored-by": "https://www.runscope.com",
        "via": "1.1 vegur"
      },
      "response_body": "ok",
      "created_at": "2015-01-21T16:26:12Z"
    }
  ]
}