Working with Webhooks
Working with webhooks
API2Cart webhook is a mechanism for getting notifications when some changes or events on shopping carts occur. For example, the order.update webhook triggers every time when the order status changes. In this case, the JSON formatted data is being sent to the specified URL.
The webhook creation
Use the webhook.create and specify the following parameters
api_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
store_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
entity = product
action = add
callback = http://exmple.com
label = super_webhook
fields = id,name
Here is the example of the request for creating a webhook https://api.api2cart.com/v1.0/webhook.create.json? api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&store_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
See more details about the parameters on our documentation.
Note: In case you are using multi store instance you can set store_id parameter to specify on which store you want create the webhook.
If the request is performed successfully and you will received the id of the webhook that you’ve just created. From now on, when new product appears on the store, you will receive webhook events on the URL, which you’ve specified in callback parameter.
The information about the events that are sent to the callback
The events that will be sent to the callback URL contain additional header with the event info.
X-Webhook-Store-Id – store id, that the event has occurred on. For non-multistore instances the returned value will always be “1”.
X-Webhook-Action – the event that occured (add, update, delete)
X-Webhook-Entity – the entity that the event occurred with (product, order, etc.)
The body contains json string with new data about the entity.
The event is considered to be successfully delivered when the callback returns status code 200. In case the status code is anything but 200, the system sends the event one more time. If this time the status code is also not 200, then it is considered to be undelivered and it will appear in the failed events log. You can check the list of failed events using account.failed_webhooks method.
NB! We store failed webhooks data for 24 hours.
What else you can do with the webhooks
Using webhook.list method you can get the list of webhooks that were created in the store via API2Cart API.
The created webhook contain the following fields:
"id" – The webhook id
"label" – The webhook name
"store_id" – The store id the webhook is created on. For non-multistore instances the returned value will always be “1”.
"active" – Webhook status
"callback" - Callback URL that the webhook will send the POST request to when the event occurs
"fields" - Fields that the webhook will send
"created_at" – Webhook creation date
"updated_at" – Last webhook update date
"entity" – The entity that the webhook is created for
"action" - The action that triggers the webhook
You can filter the webhooks according to different fields. There are the the following parameters for this:
For details see the documentation.
You can also check the number of created webhooks on the store using webhook.count method.
You can change some webhook parameters using webhook.update method. To do this, you need to fill its id.
You can change only the following parameters:
To delete the webhook use webhook.delete method and fill in the webhook id.
For more API methods jump into our documentation.