Webhook
A Webhook is a tool that allows you to receive a notification for a certain event. It allows you to register an https URL which will receive the event data, stored in JSON. Webhooks can be registered for the following events:
| Category | Events |
|---|---|
| App | uninstalled/suspended/resumed |
| Category | created/updated/deleted |
| Customer | created/updated/deleted |
| Order | created/updated/paid/packed/fulfilled/cancelled/custom_fields_updated/edited/pending/voided/unpacked |
| Product | created/updated/deleted |
| Product Variant | custom_fields_updated |
| Domain | updated |
| Order Custom Field | created/updated/deleted |
| Product Variant Custom Field | created/updated/deleted |
| Subscription | updated |
| Fulfillment | updated |
| Fulfillment Order | status_updated/tracking_event_created/tracking_event_updated/tracking_event_deleted |
To register for the product created event, for example, you should send product/created in the event field. Basically, you make a POST request in Webhooks endpoint providing a body with the type of webhook you want to create as well as the URL where you will be notified.
Now let's say you want to be notified whenever an order gets paid so you don't have to make scheduled calls to retrieve this information. Here you should create a Webhook with the event order/paid and the URL where you will be notified in your backend.
The app/suspended and app/resumed events refer to the suspension of API access due to lack of payment.
You are not allowed to use a localhost/tiendanube/nuvemshop domain for webhooks.
To aid in your development you can use RequestCatcher. PostCatcher is a web app that generates a unique url that you can use as a webhook endpoint and display any POST requests sent to it for you to examine.
About the Ordering and Idempotency of Messages
It's worth noticing that webhook messages are managed through a distributed system, so they are handled by multiple separate servers working in parallel to ensure quick, reliable, and efficient processing of the thousands of messages we send per minute. Because of the nature of the distributed system, we cannot ensure that the order they are being queued to be processed is the same as the order they are being processed.
Additionally, some messages may be sent multiple times with the same content. Those messages should be treated as unique. It is the responsibility of the developer integrating their service with our webhook to ensure idempotency.
Rules for Deduplication
- Messages sent with identical message bodies must be treated as unique.
- Messages sent with identical content but different message attributes must be treated as unique.
- Messages sent with different content (for example, retry counts included in the message body) must be treated as duplicates.
Properties
| Property | Explanation |
|---|---|
| id | The unique numeric identifier for the Webhook |
| url | The URL where the webhook should send the POST request when the event occurs. Must be HTTPS. |
| event | The event that will trigger the webhook |
| created_at | Date when the Webhook was created in ISO 8601 format |
| updated_at | Date when the Webhook was last updated in ISO 8601 format |
Verifying a webhook
Webhooks created through the API can be verified by calculating a digital signature.
Each Webhook request includes a x-linkedstore-hmac-sha256 header which is generated using the app's secret, along with the data sent in the request.
Note that some frameworks will change the header to HTTP_X_LINKEDSTORE_HMAC_SHA256.
See PHP example below:
<?php
define('APP_SECRET', 'secret');
function verify_webhook($data, $hmac_header) {
return $hmac_header == hash_hmac('sha256', $data, APP_SECRET);
}
$hmac_header = $_SERVER['HTTP_X_LINKEDSTORE_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
echo 'Webhook verified: ' . var_export($verified, true);
Parameters
When doing the POST request, all webhooks will send the following parameters in JSON format:
- store_id: Store from where the event originated (received as
user_idat the authentication step - event: Event's name (product/created, product/updated, etc.)
Also, every webhook will send custom parameters, as follows:
app/uninstalled
- id: App's id.
category/created - category/updated - category/deleted
- id: Category's id.
customer/created - customer/updated - customer/deleted
- id: Customer's id.
- event_launch_ts: Timestamp at which the event was launched.
order/created - order/updated - order/paid - order/packed - order/fulfilled - order/cancelled - order/custom_fields_updated - order/edited - order/pending - order/voided
- id: Order's id.
product/created - product/updated - product/deleted
- id: Product's id.
product_variant/custom_fields_updated
- id: Product's Variant id.
domain/updated
No additional parameter is sent along with this event. To get the list of domains one may refer to Store resources.
order_custom_field/created - order_custom_field/updated - order_custom_field/deleted
- id: CustomField's id.
product_variant_custom_field/created - product_variant_custom_field/updated - product_variant_custom_field/deleted
- id: CustomField's id.
subscription/updated
- concept_code: Concept.
- service_id: Service's id.
- event_launch_ts: Timestamp at which the event was launched.
fulfillment_order/status_updated
- store_id: Store's id.
- event: Event name (
fulfillment_order/status_updated). - order_id: Order's id.
- fulfillment_id: Fulfillment Order's id (ULID format).
- status: Fulfillment Order status.
fulfillment_order/tracking_event_created
- store_id: Store's id.
- event: Event name (
fulfillment_order/tracking_event_created). - order_id: Order's id.
- fulfillment_id: Fulfillment Order's id (ULID format).
- tracking_event_id: Fulfillment Order tracking event's id (ULID format).
- status: Tracking event status.
fulfillment_order/tracking_event_updated
- store_id: Store's id.
- event: Event name (
fulfillment_order/tracking_event_updated). - order_id: Order's id.
- fulfillment_id: Fulfillment Order's id (ULID format).
- tracking_event_id: Fulfillment Order tracking event's id (ULID format).
- status: Tracking event status.